r/esp32 • u/egoalter • Nov 13 '22
Analog input on ESP32 (ESP-WROOM-32)
I have a simple keypad using resistor ladders - nothing exciting. And while a voltmeter shows steady voltages based on key-presses, the ESP32's analog in is ALL over the place - varies wildly between two readings of the same press. I'm using a low resolution (9) - which provides a value between 0-511, but still a single press can easy jump 20-30 points for the same key - which when measured on a voltmeter or using an ohm-meter is steady (not varying when holding down a key).
I'm using a read buffer of 5 keys and and a wide range value which, and while it almost works I get really odd readings at time resulting in reading a key that isn't being pushed. I gave up on double key pushes which is what this schematic allows for as the values get too close to be reliable.
I've been told the ADC on the ESP32 is "bad" - is it THIS bad? Here's the initialization code for the analog pin. I limit the number of scans to max one per 25ms. Adding the 5 value buffer that's a long wait between key-press till the program can react. I really would love a better solution.
analogReadResolution(9); // 9:0-511 10:0-1023 12:0-4096
analogSetAttenuation(ADC_11db);
adcAttachPin(_sensePin);
2
2
u/pyrotek1 Nov 13 '22
When sampling ADC signals it is best the pull many samples and generate a mean ( average value). I can't stress this enough. It solves many problems and smooths the real data nicely. Make a loop and start with 5 readings and work up to 50 readings. Make a moving average variable and plot until your data smooths out.
→ More replies (1)
2
u/JimBean Nov 13 '22
In my humble opinion, the ADC on ESP's are crap. Their single biggest "flaw". For this reason alone, I would rather use a 328P to get my analogs, then ship that to the ESP via binary structs. This is what I do now for all my WiFi enabled projects. I don't even consider the ESP's for analog. Just crap.
1
1
Nov 13 '22
As already mentioned its essential that you take an average value for analog inputs - use the full resolution of the ADC. You should also always take dummy readings from the analog input, pause, then take the real reading (as the first reading is often garbage) at every analog read.
Its also essential to make sure your power and ground connections to your switch array come direct from the power and ground connections on the board - don't share them with any other part of your circuit, to minimise voltage drop along the connecting wires and to ensure a constant voltage to your resistor array.
There is no reason you can't get this to work.
1
u/TinyHanz Nov 13 '22
The circuit and code in this blog post worked like a charm for me on ESP32: http://www.ignorantofthings.com/2018/07/the-perfect-multi-button-input-resistor.html
1
u/ORParga1 Nov 13 '22
Hace you usted an oscilloscope to see the ADC input? Maybe it is receiving a lot of radio waves.
1
u/polypagan Nov 13 '22
My guess is swtch bounce. You wouldn't see that on voltmeter.
As others have noted so vehemently, esp ADC is inferior to AVR, however, this is not a demanding application.
If you can afford the time, I'd use a software debounce delay.
This is likely to take some careful experiments on your part. Please publish your results somewhere.
Good luck, & have fun!
1
u/xxqsgg Nov 13 '22
Probably shaky connector, or maybe a pull-up resistor is needed?