Esp32 and Atmega328 voltage measurement - Page 1
Forum section: electronics / general

author: Fertan
author_rank: Newbie
timestamp: July 07, 2025, 09:23:09 pm
content: Hi, ı am trying to measure voltage with some mcus and ı am not succesfull at it. I am using in-built adc and trying to measure voltage with a voltage divider. İ am not getting results that are linear in adc reading and also my adc readings are not stable. My voltage divider is one 10k and one 1k resistor ı am not if that is a problem. I tried to calibrate the voltage divider ratio by measuring the resistors and also taking an actual measurement from outside using a multimeter. I want a stable reading, ı have seen li-po charges that use a mcu and have really stable voltage reading. I am willing to use an op-amp if necessary but it should not be. Before you answer: Thanks.

author: hans
author_rank: Super Contributor
timestamp: July 08, 2025, 07:20:10 am
content: I presume the 1k resistor is between ADC pin and GND, and 10k between the input voltage and ADC pin?Have you also tried paralleling a capacitor (say 1nF, but you can use 100nF or whatever at hand) parallel to the 1k resistor?

author: tooki
author_rank: Super Contributor
timestamp: July 08, 2025, 07:27:10 am
content: Beware that the standard ESP32 ADC is not particularly good, and exhibits some nonlinearity. (The ESP32-S3, and possibly others, have a better ADC.)And what do you consider “unstable”? It is normal for there to be some noise.

author: Fertan
author_rank: Newbie
timestamp: July 08, 2025, 07:40:59 pm
content: Quote from: hans on July 08, 2025, 07:20:10 amYes, ı have tried that. It is not changing anything.Quote from: tooki on July 08, 2025, 07:27:10 amYou are right, ı have seen that linearity problem but it is not really a big problem for me because ı am measuring voltage between 22-25.4 so it is not the main problem. I consider a lot of different readings unstable. It says 25.4, then suddenly changes to 26.7, then suddenly 25.7 then stays at the 25.5 for a while, then stays at the 25.4 for a while then suddenly becomes 22.5 sand it goes on like this. This problem ı had with arduino too. They are not measuring stable enough. I do not want a lot of digits .1 is enough for me. I want to meausure voltage like a lipo-charger. ı want to meausure 6s,5s,4s... voltages and substract one from another to get individual cell voltages. It doesnt have to be that accurate, it is just for a display.

author: ledtester
author_rank: Super Contributor
timestamp: July 09, 2025, 07:47:30 pm
content: How are you powering your MCUs, USB or batteries or ...?Which voltage reference are you using for your ADC? External? Internal?This video has some good points and suggestions to keep in mind when using ADCs:- an external reference may be noisy- your program's value for either an external or internal voltage reference may be inaccurate- noise can be picked up by connecting wires- oversampling can be used to reduce noise3 Tricks for getting a Better ADC for FREE! (Arduino)https://youtu.be/ZL-2DphjAaoIt also shows a battery giving a very stable voltage reading as measured by an Arduino ADC.Is there something different from your setup?This video:#10 Tutorial: Make the Arduino Analog Readings more precise - Andreas Spiesshttps://youtu.be/xI_qU2auVx8talks about using an internal reference for more stability.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: July 10, 2025, 11:21:10 am
content: Quote from: Fertan on July 08, 2025, 07:40:59 pmThose numbers do not mean anything to us. When debugging ADC circuits, there is a clear division between the frontend and the "internal" part (I.e. ADC hardware, firmware). As long as you are debugging, remove all conversion factors, multiplication, calibration etc, and work with the raw ADC values. From your values I am guessing that you have one or two bits of noise, and this is sort of expected. To get better results you have to look carefully at the whole design. What is your voltage reference (uC Vcc?) PCB layout (Just forget those last bits on a breadboard, you need a GND plane, and a good one too!)And when the hardware works, you can apply both neat and dirty tricks in software. There are various ways of filtering (from simple moving average to Kalman), and you can build in a hysteresis.--------------------------Long ago I also experimented with one of the application notes for the AVR's. I got pretty stable results (even on a breadboard, but no external circuitry, so no voltage fluctuations) and the ADC value was stable with maybe only the lsb toggling. Once I got that working (with a simple potentiometer and capacitor for voltage stabilisation), I gave a quick turn to the potentiometer (or switched in another resistor, I forgot the details). Result was I could plot a graph of the RC constant, and the steps of the ADC were clearly visible (with a tiny bit of noise). Then I added averaging, and this did not work very well. I still mostly had the toggle in the LSB. So the ADC had a lot of consecutive values of 100, 100, 100, 100, (which average out to 100), or it had 101, 101, 101, 101 (which average out to 101).And that is when I took the next step from the application note. I added 2 or 3 bits of noise. I took the RC voltage form an oscillator build around a NE555, and added a small portion of that (with AC coupling) to the ADC pin. The raw ADC values were now something like: 98, 101, 103, 99, ... but when averaged out, the value is 100.25 and this smoothing worked very nicely, the intermediate values calculated over the Potentiometer + capacitor input followed the theoretical exponential curve very nicely.

author: Psi
author_rank: Super Contributor
timestamp: July 30, 2025, 10:22:27 am
content: Make sure you are reading the right ADC and the right ADC channel.There are so many ESP32 boards out there will different pin numbering and/or wrong silkscreen text. Especially if you got the board from aliexpress.It's easy to think you are reading one pin but that pin is not the pin you think it is.You can try to output some digital data on that pin to see it's the pin you think it is first. eg flash an LED.Or just dump all the data from all the ADC channels and see if any match up to what you're doing.It's pretty normal for analog data on one ADC pin to couple onto others if they are floating. So sometimes you can be reading the wrong pin and get data that looks somewhat expected in that it changes in relation to your analog signal but is really noisy or behaves very strangely

author: hfleming
author_rank: Regular Contributor
timestamp: July 30, 2025, 01:18:05 pm
content: On the ATMega is is also important to program the A/D to use the correct voltage reference. You can program it to use either AVcc (not knowing your hardware circuit, that can be quite noisy unless AVcc is decoupled properly. If you use an Arduino, no, AVcc is not decoupled properly. From what I can remember it is just coupled to Vcc), or if your Vref pin only has a decoupling capacitor connected to it, use the internal reference. Read the datasheet, where it talks about the ADC voltage reference, and go from there.

author: Peabody
author_rank: Super Contributor
timestamp: July 30, 2025, 02:28:22 pm
content: On the 328P, you can set up the ADC to measure the internal 1.1V bandgap voltage with reference to Vcc. Normally the bandgap would be the reference voltage, but it can also be the voltage being measured. So if you do ADC on the bandgap with reference to Vcc, you should get a steady reading. However, if your Vcc is moving around, you might not. The bandgap itself varies from part to part, but I think is stable for any specific MCU. Anyway, this should test whether your Vcc and your setup in general are stable.Off topic, but if you are powering the 328P directly from a LIPO battery, with no regulator, this lets you measure the battery voltage with no divider or any external parts.The other thing is that with the divider, the voltage you are measuring at the ADC is 0.090909 times the 25V at the top. For a 328P running at 5V, a difference of 1 out of 1024 ADC reading is about 50mV at the top. And of course more at 3.3V Vcc. So there's a limit to how accurate this is going to be. But you should still be able to get steady readings to the nearest 0.1V if both the input and Vcc are stable.