Can I change a pin's designation on the ESP32S3 to either analog (A) or digital (D) with an "if" statement in the loop?

I’m not sure if this is the correct forum.
I am using the XIAO ESP32S3 to monitor sensors and send info to Adafruit.io via MQTT.
My problem is I am one pin short, so I am hoping to use multitasking with one pin (pin3).
But one of the sensors is a digital (D3) ON/OFF sensor where the other is an analog (A3) variable sensor. I am wondering if I can use an if/else if statement to change the designation of the pin in the void loop() of my code. This is what I am proposing.

//Create a global string variable 
String pin = "ON";
//designate a float to pin
float waterInPin = A3;
//designate the pin state
pinMode(waterInPin,INPUT);
//create if/else if statement in setup
void setup(){
if (pin == "ON"){
float waterInPinB = A3;
}
else if (pin== "OFF"){
  float waterInPinB = D3;
}
}
//what I propose in the loop.
void loop(){
//designate String pin as ON
pin = "ON";//makes pin analog A3
delay(500);
//turn on the power to the analog sensor
digitalWrite (Plev,HIGH); //I have already designated this pin Plev and set MODE to OUTPUT
//read the A3 pin value
float waterSensorInValue = analogRead(waterInPin);
//turn off the power to the analog sensor
digitalWrite (Plev,LOW);
//set the String pin value to OFF
pin = "OFF";//makes the pin digital D3
// wait a half second and turn on the digital sensor
delay(500);
digitalWrite(Phi,HIGH); //as before I have already designated and set the MODE for this pin.
//read the value of the digital pin
int val = digitalRead(waterInPin);
//turn off power to digital sensor
digitalWrite(Phi,LOW);
 }

After this the ESP32S3 sends the data to Adafruit.io via MQTT and then the loop starts over and the String pin is reset to “ON” and so pin 3 is Analog again.

I have tried to figure out how to designate CODE but not sure how. Tried to look up on line with no results.

Does it seem like this is possible and work work without damaging anything?
I would really like anyone input.
Thanks

I j
know the pin mode is usually defined in startup, but i dont see why you couldnt change it on the fly … i have never really tried it

Hi there,

No, you cannot directly change a pin’s designation on an ESP32S3 between analog (A) and digital (D) using an “if” statement within your loop; the pin configuration needs to be set once at the beginning of your code, and changing it dynamically within the loop is not possible because the hardware itself determines the pin function based on how it’s configured.

Explanation:

  • Pin Configuration:

When you define a pin as analog or digital in your code, you are essentially telling the ESP32 how to interpret the signals on that pin. This setting is applied at the hardware level and cannot be easily altered on the fly.

  • "pinMode" Function:

In Arduino and ESP32 coding, the pinMode() function is used to set a pin’s direction (input or output) and mode (digital or analog). This function should be called once at the start of your program to configure the pin appropriately.

What you can do instead:

  • Conditional Logic based on Pin Reading:

If you need to switch between analog and digital functionality based on a condition, you can use an “if” statement to read the value from a different pin that determines the mode, and then use the appropriate analog or digital reading function based on that condition.

  • Separate Pins for Different Modes:

For more flexibility, consider using dedicated pins for analog and digital functions if your application requires frequent switching between modes.

Example (using conditional logic based on a control pin):

Code

int controlPin = 2; // Pin to determine analog or digital mode
int sensorPin = A0;  // Pin that can be used as analog or digital

void setup() {
  pinMode(controlPin, INPUT);
  pinMode(sensorPin, INPUT); // Set sensor pin as input initially
}

void loop() {
  if (digitalRead(controlPin) == HIGH) {
    // Analog mode
    int analogValue = analogRead(sensorPin); 
  } else {
    // Digital mode
    int digitalValue = digitalRead(sensorPin); 
  }
}

HTH

GL :slight_smile: PJ :v:

i think what he wants to do is what chat said was conditional logic… i dont think it is necessary to query a pin to change the pin mode… like i said, i never really tried it, but i am pretty sure you can modify the pin modes on the fly ( not to disagree of course)

Hi there,

Maybe on others you can but Definitely NOT on ESP32…and The OP main point he’s one pin Short? I don’t see how with the S3 plenty of IO to tap into, but in HIS scenario it’s a no GO.
Setup ONLY runs ONCE…SO that should tell you something as to why.
SO, It’s NOT supported.

HTH
GL :slight_smile: PJ :v:

yeah i am not exactly sure what sensors he is trying to use I am going to try to write a test code using a pot

for 1 minute i will try to read the pot as analog… then alternate one minute read as digital and see

1 Like

Plus, it’s better to change your variable pin from String to bool in order to use less memory :grinning:

2 Likes

Thanks for the input. I have a non-contact liquid level sensor which is digital and a potentiometer which is analog. I also have a temperature sensor which is analog using DallasTemperature.h and OneWire.h which is analog but it has a resistor between it’s power pin and sensor pin so I don’t think I could use it with the potentiometer. because of the resistor. I am going to try it and see. I await cgwaltney’s experiment.

I think that my idea will work as it seem to turn on and off power pins and get readings. However a new glitch has arisen. The esp32s3 voltage for each pin is 3.3 to 3.6vdc but my contactless liquid sensors work on 5 - 12 vdc. so I would have to use a couple get the sensors to work and I don’t know if it is worth it. I was hoping to use deep sleep to increase battery time significantly. We’ll see. I do some more experiments and let you know. Thanks for all the help.

im not sure you are going to be able to run all these sensors on the same pin… do you have a schematic of your layout… in any event i am still interested to see if it is possable

I am trying to find some old code that works i can recycle

what do you mean by non contact… you talking ultrasound… ? can you post a link to product… just curous

void setup()
{
    Serial.begin(9600);
}

void loop()
{   
	//Analog Read Loop
    pinMode(A0, INPUT);// Set Pin Mode to Analog Input
    int sensor_analog = analogRead(A0);
    Serial.println("The Sensor Analog Value is: ");
    Serial.println(sensor_analog);

    delay(500);

	//Digital Read Loop
    pinMode(D0, INPUT);// Set Pin Mode to Digital Input
    bool sensor_digital = digitalRead(D0);
    Serial.println("The Sensor Digital Value is: ");
    Serial.println(sensor_digital);

    delay(500);
}

Ok this code works and give analog and digital reads, but you have to change the pinmode each time you want to change the reading

12:01:21.863 → The Sensor Analog Value is:
12:01:21.863 → 1040

12:01:22.362 → The Sensor Digital Value is:
12:01:22.362 → 0

12:01:22.879 → The Sensor Analog Value is:
12:01:22.879 → 2063

12:01:23.394 → The Sensor Digital Value is:
12:01:23.394 → 0

12:01:23.884 → The Sensor Analog Value is:
12:01:23.884 → 2665

12:01:24.386 → The Sensor Digital Value is:
12:01:24.386 → 1

12:01:24.869 → The Sensor Analog Value is:
12:01:24.869 → 3515

12:01:25.369 → The Sensor Digital Value is:
12:01:25.369 → 1

12:01:27.896 → The Sensor Analog Value is:
12:01:27.896 → 4095

12:01:28.422 → The Sensor Digital Value is:
12:01:28.422 → 1

I am thinking you could also use programming logic to change an analog read to a digital result

also you may want to check out this device

see how they get more sensors by shifting the data and clock pins

you cant do the if loop in the setup… you need to do it in the loop

Hi there,

So none of that will Work BTW…LOL
He wants to power the Dallas with Parasitic power, You can’t turn it on then take an ANALOG reading from the same pin. The reading will be all 1’s. I wouldn’t bet the bank on the accuracy either.
The Library also won’t and doesn’t work that way… BACK to the LAB fella’s :face_with_peeking_eye:

HTH
GL :slight_smile: PJ :v:

maybe with a couple diodes and filter it will , YMMV

I said I can’t do the temperature sensor. I am doing a potentiometer and a non- contact liquid level sensor. Here is a link to the sensor.
non-contact liquid level sensor.

Here is a link to an image of my schematic.
Link to schematic

Also, can anyone recommend a small 3vdc spst relay to supply 5vdc to the liquid level sensors?

I dont think you really need a relay… the sensor may work on 3v3 dospite the data sheet… i would try it or power from the XIAO 5v pin

also level shifter for example

i think you are trying to use too many data pins as power pins
what is the process workflow you are trying to achieve?

what temp sensor are you using? maybe you should use IIC sensors

also DHT11 outputs a DIGITAL SIGNAL not an ANALOG SIGNAL

I have one power pin for the temperature sensor which is a DS18B20 liquid temperature sensor. A second power pin for the potentiometer. Two more (one each) for the non-contact liquid sensors. One that senses when liquid in not present and the other that senses when it is. I have two more power pins that drive the relay to connect disconnect the solar panel to the battery for charging. One pin is used to monitor the battery charge and there are five sensor pins. The temperature sensor, two non-contact liquid sensors, and two potentiometer sensors.
I have run the liquid sensors on 5vdc but when I try to run them with the ESP32S3 3.3vdc they do not come on. So I am going to have to use a relay per sensor.
The work flow is: I turn on and read the various sensors in succession, then I connect to internet, and Adafruit.io to upload the readings.
I am looking at using an 817-HV-3S-KT relay.

my opinion is you switch to a 5v unit like arduino

and combine power pins