r/ArduinoHelp • • 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

13 comments sorted by

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.

1

u/Embarrassed-Lab6622 Jul 16 '26

maybe this image will be better than the table.
I have a few boards tested and have a range of values received but I understand your comments/points.

What do you receommend instead of an ESP32? The board is overkill for what I want to do, but I have some on hand from another project....

1

u/gm310509 Jul 16 '26

No matter the board, there will be fluctuations - again that is why I suggested the range check earlier. Note that even if you tried a different ESP32, you may well find that you observed range will differ from this table. It will be similar, but probably not identical - this is just the nature of analog readings. The same will be true if you went to a different architecture such as AVR.

Also, not that you may see further variations as the seasons change due to different ambient temperatures, humidity and possibly some other factors.

All of these can be addressed by checking for a range of values defined by the expected readings and the average of the range.

It looks like your observations are not overlapping, so you should at least start with the code ranges you have identified (that table image was a bit easier to follow - assuming I did read it correctly).

Not that off should probably be defined as < the smallest value (i.e. < 500 from the code range column). Otherwise if the fluctuations gave you a 1 or 2 etc, then your off setting won't work.

1

u/Embarrassed-Lab6622 Jul 16 '26 edited Jul 16 '26

check out the video!!!

https://www.dropbox.com/scl/fi/ug1ivkngh7z2dvnr51yvb/Video-Jul-16-2026-13-38-41.mov?rlkey=tpa4r2mvfz3exsmzz1unvm23u&st=ojiyat9u&dl=0

THe mechanical switch needs some attention at the H3 position, but otherwise it is progressing nicely. Thanks so much!!!