I'm trying to figure out how to convert my voltage divider to determine the voltage on the battery. I have an ESP32 board and 4AA NiMH (4.8V) batteries as an external power source.
That doesn't seem right, but divide the ADC reading by 538 (1766/3.3).
The 12-bit ADC should max-out at 4095, and with a 3.3V reference you should read 4095 at 3.3V.
Your voltage divider calculations are right. But, it's usually a good idea to give yourself some "headroom" in case the battery happens to go over 4.8V.
So I adjusted the resistance to half the input. I now have two 4.7K resistors, and I set a max input voltage of 6. So now my reference voltage is now 3V.
With the updated resistors, I'm getting raw values of 2967 - 2979. I took my DMM, and I measured 5.19V coming straight from the source (the 4AA NiMH).
I'm a bit lost on how to take the 2967 and get 5.19V?
Maybe I'm overthinking this... I changed my calculation to this...
voltage = voltageRaw * (3.3 / 4095.0) * 2;
Since I'm halving the input voltage (two 4.7K resistors), then I now get an input voltage of 4.81. It's not quite 5.19 (which is what I get on my DMM). Am I missing a calculation?
To calibrate your voltage divider, measure the battery voltage with a multimeter, and calculate a scale factor such that the Arduino output agrees with the multimeter reading.