Go Down

### Topic: 12V batteri voltage meter (Read 611 times)previous topic - next topic

#### 16759

##### Apr 20, 2018, 07:27 pmLast Edit: Apr 20, 2018, 07:28 pm by 16759
Hi!

I want to measure (and logg) the voltage of a 12V car batteri with an Arduino UNO powered directly from the same batteri. I am using a voltage divider to get the voltage within the range of the analog port (A0) but I get an measurement error of more then 1 volt(10-15%)... So what am I missing?

Please se attached image for reference of the schematic.

So I make an analog read, divide with 1023 and multiply by 5 to get the voltage over R2.

Then I calculate Uout as:
Uout = ((R1+R2)/ R2) * Uin

R1=10kOhm (0.1% offset)
R2=4,7kOhm (0.1% offset)

Since I am powering the UNO from ~12 supply I imaging that the built in linear regulator should be able to keep the internal reference at 5V. Is this an acceptable assumption?

#### groundFungus

#1
##### Apr 20, 2018, 09:45 pmLast Edit: Apr 20, 2018, 09:45 pm by groundFungus
Code: [Select]
`Uin =AnalogRead()*5/1023;  // will do integer math`
That calculation will return an integer data type even if Uin is declared as float.
Code: [Select]
`Uin =AnalogRead()*5.0/1023.0;  // the .0 makes the compiler do floating point math`
Will return a float.

As for the rest, it depends on what data types that the variables are.  This is why we request that you post the whole sketch.  Then we won't have to guess.

#### Idahowalker

#2
##### Apr 21, 2018, 03:21 amLast Edit: Apr 21, 2018, 03:25 am by Idahowalker
For better results measure the 5V of the Arduino. I get 4.977V so I use 1023/4.977

FDivisorFactor = 1+(R1/R2)

fVoltIn = (A0_Value / fAD_Factor) * fDivisorFactor

I do not allow the voltages to be present on the AO and A1 pins all the time to reduce the battery consumption of current flow through my voltage divider. I use a MOSFET to gate the voltages on and off. I have a delay of .09 before I take a A0 reading.

Gives me readings of .005 off my volt meter

Actually measure the resistance of the resistors you are using for the voltage divider.

Put a 103 (.1uF) 50V cap into the Aref and ground for better A/D stability.

#### Wawa

#3
##### Apr 21, 2018, 05:36 am
For better results measure the 5V of the Arduino. I get 4.977V so I use 1023/4.977

I use a MOSFET to gate the voltages on and off.

I have a delay of .09 before I take a A0 reading.

Gives me readings of .005 off my volt meter
Does it stay 4.977volt when e.g. a LED is blinking?

Delays do just what they say. Nothing happens during a delay, so why use it.

So 12-15volt with a resolution of 0.005volt (3000 values) from a 10-bit A/D (1024 values).
Leo..

#### wvmarle

#4
##### Apr 21, 2018, 09:05 pm
A more stable measurement will be against the internal fixed 1V reference. That reference is very stable regardless of what Vcc really is (and Vcc is not always that stable).

You need of course a voltage divider that's bringing down the voltage to <1V, and you will have to calibrate this reference as the actual value can be up to 10% off.
Quality of answers is related to the quality of questions. Good questions will get good answers. Useless answers are a sign of a poor question.

#### Wawa

#5
##### Apr 21, 2018, 10:27 pm
Still want to see that diagram with the mosfet.
So far nobody has managed to switch off a voltage divider with a single mosfet when default Aref was used.
(it can be done with 1.1volt Aref)
Leo..

#### Idahowalker

#6
##### Apr 22, 2018, 09:25 pmLast Edit: Apr 23, 2018, 01:36 am by Idahowalker
Voltage to be measured to a N (corrected) channel MOSFET drain. I use 2N7000's.

Arduino pin, your choice, to one side of a 330 ohm resistor, other side of 330 ohm resistor to gate of MOFFET. Junction of gate of MOSFET and 330 ohm resistor place a 10k resistor to ground.

Source of MOSFET to top of resistor divider network resistor R1, bottom of R1 connected to R2, Bottom of R2 connected to ground. Junction of R1/R2 to AX pin.

Input of 330 ohm resistor to an Arduino pin to gate on/off voltage to be measured.

I put in delay because I found the AX pin requires a little time for voltage to become stable on the pin. You don't have to *shrug*.

Arduino pin used, output a 1, to gate on MOSFET, to allow voltage to be measure to be felt at the top of the voltage divider, than measure voltage with an analog read.

You can do an analog read without gating on, an experiment, MOSFET to find that the AX pin will return a 0 volts measured.

Quote

The ADC reports a ratiometric value. This means that the ADC assumes 5V is 1023 and anything less than 5V will be a ratio between 5V and 1023.

https://learn.sparkfun.com/tutorials/analog-to-digital-conversion

By gating off/on the voltage to be measured to the voltage divider, the voltage divider is only drawing battery power when being read, instead of drawing battery power continuously.

#### Wawa

#7
##### Apr 23, 2018, 12:59 am
Voltage to be measured to a P channel MOSFET drain. I use 2N7000's.

Arduino pin, your choice, to one side of a 330 ohm resistor, other side of 330 ohm resistor to gate of MOFFET. Junction of gate of MOSFET and 330 ohm resistor place a 10k resistor to ground.

Source of MOSFET to top of resistor divider network resistor R1, bottom of R1 connected to R2, Bottom of R2 connected to ground. Junction of R1/R2 to AX pin.

Input of 330 ohm resistor to an Arduino pin to gate on/off voltage to be measured.

I put in delay because I found the AX pin requires a little time for voltage to become stable on the pin. You don't have to *shrug*.

Arduino pin used, output a 1, to gate on MOSFET, to allow voltage to be measure to be felt at the top of the voltage divider, than measure voltage with an analog read.

You can do an analog read without gating on, an experiment, MOSFET to find that the AX pin will return a 0 volts measured.

By gating off/on the voltage to be measured to the voltage divider, the voltage divider is only drawing battery power when being read, instead of drawing battery power continuously.
The 2N7000 is n-channel.

I get the 0/5 volt switching voltage at the gate, with 330ohm series and 100k bleed to ground.
Those resistors are not really needed, because the gate charge of this fet is very low, and bleed during bootup is not destructive.
Gate can be connected directly to the Arduino switch pin.

With the divider between source and ground, and tap to an analogue input, you can indeed turn the divider OFF.
But you can't turn it ON. At least not fully.

You have described a source follower.
Gate must at least be Vth(gs) higher (~2.5volt) than source, for the fet to turn on.
With 5volt max on the gate, the top of the divider (R1) will never see more than about 2.5volt.
Sorry, but this circuit can not work the way you think it does.
You will have some switched voltage on the analogue pin, but not a representation of what's on the drain of the fet.

This circuit can be modified easilly.
Move R1 (top of divider) to the drain of the mosfet (between 'voltage to measure' and drain).
Then connect source to R2 and analogue pin.
Divider R1,R2 needs recalculating for 1volt at the analogue pin.
And the pin has to be measured with 1.1volt Aref enabled in setup().

Now the mosfet switches the divider on/off not at the top, but in the middle, where they (R1,R2) are joined.
It can now do so, because source is always <=1volt (Vgs > 4volt).
1.1volt Aref is also more suitable (stable/VCC independent) for voltage measurements.
Leo..

#### Idahowalker

#8
##### Apr 23, 2018, 02:04 am
I bread boarded the circuit with the divider on top, split, and on the bottom of the MOSFET. I took measurements and compared the results with the software produced results. I found, with the voltage divider below the MOSFET, the circuit works quite well. Well enough to put the circuit on a PCB. Thanks for your feedback.

#### Wawa

#9
##### Apr 23, 2018, 02:55 am
I found, with the voltage divider below the MOSFET, the circuit works quite well. Well enough to put the circuit on a PCB.
Sorry for taking the time to explain.
Time for you to study the workings/limitations of a source follower (and bjt emitter follower).
An n-channel fet source can never be higher than "gate - Vg(th)", independent of what's on the drain,
as the emitter of an NPN transistor can never be higher than "base - BE diode", independent of what's on the collector.
Good luck with the rest of your education.
Leo..

#### 16759

#10
##### Apr 24, 2018, 05:10 pm
Thanks for the feedback. I now get satisfactory readings.

Go Up