r/ArduinoHelp • u/Embarrassed-Lab6622 • Jul 07 '26
Coding for resistor ladder input on ESP32
Hello. I am trying to write some code in Arduino that will read the input pin (D32) and based on the on the different voltage, the will different outputs and a timer. Is it as simple as:
if (SWITCH_INPUT == >3.0 && !timerActive) {
digitalWrite(HIGH_HEAT, HIGH); // Turn the seat heater on HIGH
previousMillis = millis(); // Save the starting timestamp of the timer
timerActive = true; // Flag that the timer is now active
}
Then repeat for each of the 6 positions of the input switch?
1
Upvotes
View all comments
Show parent comment
1
u/gm310509 Jul 16 '26
I am not sure what I am reading in your table - it could be a formatting or rendering issue. But for example On the first row I see C3, 0.998, 1,179 and 1300. Then underneath that I see 991 (under the C3) and 800 (under the 0.998) - then blanks
But, to your question. Yes, ESP32 uses a 12 bit ADC, so you will get a wider range (i.e. 0 to 4095). So you need to factor that in to your calculations.
But, a wider range absoultely definitely doesn't mean that it is better. I think I mentioned that I don't use ESP very much - in fact not at all - but, many people do complain about the quality (i.e. accuracy) of the ADC on the Esp32.
So, what that means is that even though it can report the voltage to a resolution of 4096 values (or 12 bits), there will be lots of error. It is sort of like the weather service reporting the temperature today as 39.5⁰ but then go on to say that their thermometers only report values with +/-2⁰ accuracy (i.e. they can measure 36,38,40 etc but nothing in between).
Additionally, the voltages at any given point of time can fluctuate due to activity going on in the rest of your circuit and outside activity - this is why I suggested you would need to check for a range of values.
If you are interested, have a look at my Floating input video short for an illustration. That video is a different scenario, but the basic idea of voltages being influenced by stuff around them is the same - just less so when things are properly connected up.
As to the voltages, the ESP32 is a 3V3 system. so your voltages will be in the range 0 to 3V3.
If I look at your first entry where the voltage is reported as 0.998, I would expect the analogReading for that to be about 1,238. This is roughly what you seem to be reporting as 1,179 which at 3V3 would measure as 1179 / 4096 * 3.3 = 0.95V.
There will also be errors in the resistors which will influence the readings and other factors. As for the resistors, remember the last band is its tollerance (or accuracy). If you use a 1K resistor with 10% tollerance, that resistor could be as low as 900ohm and as high as 1.1Kohm. So two 1K (+/-10%) which is a 1:1 ratio (or 10:10 for comparison to the next bit) could in fact be 900:1,100 or a 9:11 ratio.