Voltage Dividers and Arduino
For my WiFiTank I wanted to get a readout of the battery voltage.
But the battery is a 9.6V and the charger for it charges with up to 14V, while the arduino only can handle 5V.
To get around that problem I made a voltage divider. I based it on 20V, just to be on the safe side.
The voltage divider is a 3 to 1, meaning that between netative on the battery, and the point where the arduino’s analog pin is connected, there are a 10k ohm resistor, and a 30k ohm resistor between the same point and positive on the battery. It could also be something else than the 30 and 10k, but that is what I had.
The resistors are connected like this
Where R1 is the 30k ohm resistor, and R2 is the 10k ohm resistor.
The arduino’s analoge pin is a 0-1023 range, so it had to be calculated a bit to get the right voltage.
My first attempt was
map(analogRead(sensorPin), 0, 1023, 0, 20)
- 0, 1023 – The range of the analog pin
- 0, 20 – The voltage being measured
but it didn’t have any decimals on.
Next was like this
map(nowVolt, 0, 1023, 0, 200) / 10.0
and it got it’s decimals, but was around 0,5v off and was only jumping in 0,5V steps.
To get another decimal I added some extra zero’s, and got this
map(nowVolt, 0, 1023, 0, 2000) / 100.0
and both decimals was there and working, but the voltage was still a bit off.
To get the voltage right, I tweaked the values in the map function a bit, to narrow the range a bit down to fit the resistor tolerance in my voltage divider.
map(nowVolt, 0, 1023, 0, 1933) / 100.0
and this is as close as it can be, and is actually closer than I would have dreamed about.
The finished voltage divider, made of 4x10k ohm resistors