Voltage monitor ?

flak

New Member
Looking for some advice on how to do the following if anyone can help ill be grateful.

Basically im trying to figure out how to use a picaxe to measure the voltage from a 12v battery and convert the adc value to its equivalent voltage value ready to be displayed on a lcd or whatever.The picaxe im using is the 08m under a stable 5v reg supply drawn from the 12v.

So far ive calculated the voltage divider required to output 5v on the adc pin is r1 10k and r2 7.1k its the next bit which is tricky.If 12vin then 255 adc and 6vin then 127 how do I translate and scale that to a actual vin voltage value ?
 

BeanieBots

Moderator
First off, I'd use ReadADC10 to get better resolution.
That would give you 1023 for 12v.
You could then multiply by 59 which will still fit in a word. (60357).
Divide that by 500 to give 120 which you could then split into units and tenths or divide by 5000 to give just whole volts.
 

geoff07

Senior Member
If your input range 0-255 represents 0-12v then simply divide the analogue reading 255 by 12 which gets you a scale factor - steps/volt in this case 21.25. Multiply the scale factor by 100 to keep it in whole numbers - 2125 - use word variables as they can go up to 65335, bytes only to 255. Actual reading = input * 100 / scale factor.

But actually 12v lead-acid batteries can go over 14 volts, so I would adjust the max scale to say 15v to allow for overcharged batteries and avoid toasting the chip input. Plus you might use the high resolution input to get a scale of 0-1023 and so a bit more precision. And to be really adventurous, use a moving coil meter and drive it from PWM. Then you can have a compressed scale. I did this recently and have an old moving coil meter monitoring a car battery with a scale of 10-15 volts.
 
Last edited:

Dippy

Moderator
It's just algebra - in fact your description is the start of answering your own question.

Take BB's advice.

Just one thing.
Your resistor calcs are OK for a perfect 12 volt battery.
Does it really never get greater than 12 ?

I'd allow a bit of headroom - you can afford to if using READADC10.
Maybe reduce R2 to 6K2 or 5K6.

For best ADCing it's a good idea to have a decoupling cap (e.g. 47nF ceramic) on PICAXE V+ and Gnd pins - close to I.C. as possible.
Power supply should be rock steady and smooth.
 

John West

Senior Member
It's also not a bad idea to have an ~ .1uF capacitor at the junction of the voltage divider (from there to ground.) Add both the divider resistor to ground and the capacitor to ground as near as reasonable to the PICAXE.

It helps filter out any unwanted electrical noise and keeps the voltage steady during the sample, improving the accuracy and reliability of the reading.
 

hippy

Ex-Staff (retired)
The full equation is -

Volts = ( Nadc / Nmax ) * Vpsu

Where -

Nadc is the value returned by READADC or READADC10
Nmax is the max value ever returned by READADC (255) or READADC10 (1023)
Vpsu is the PICAXE supply voltage.

In most cases the equation is re-arranged so the division comes last the multiply first -

Volts = Nadc * Vpsu / Nmax

and often 'Vpsu/Nmax' is determined, and then a means to apply multiplication for that constant is found, which is what BeanieBots and Geoff07 describe.
 

flak

New Member
Silly question but how would i monitor the voltage though adc of the picaxe v+ supply if that is used as a reference by the picaxe for all adc readings ?
 

hippy

Ex-Staff (retired)
You need to monitor a known and constant voltage. As the supply goes up the value read will fall, as the supply goes down it will rise. You can then determine what the supply voltage is from the reading from this refenernce voltage.
 

BeanieBots

Moderator
Further to hippy's comments, you can use anything that does not follow a linear relationship between voltage and current. That is, a simple resistor/resistor potential divider will NOT work because a resistor's voltage is exactly proportional to its current, however, that is not true for a diode, a zener or even an LED, so any of those in a divider will make it possible to measure the PICAXE power rail. You'll need to use a lookup table if you want a voltage reading but a simple solution if you just want a 'trip' value or similar.
 

flak

New Member
How can i make the adjustments to the code so that i display one voltage to two decimal places and the other higher resolution to 4 places ?


Code:
symbol adcreading = b0'from the vdivider mesuring the 12v batt
symbol adcreading10 = w4 'from the vdivider measuring the 12v batt higher res
symbol vreading = b1 'voltage reading up to 2 decimal places 
symbol vreading10  = w5 'voltage reading up to 4 decimal places 
symbol vin = b2 
let vin = 5 'ref voltage

main:

readadc 1,adcreading
readadc10 1,adcreading10

let vreading = adcreading / 255 * vin
let vreading10 = adcreading10/ 1024 * vin

sertxd("voltage: ",#vreading," voltage(10): "#vreading10,13,10)



pause 2000
goto main
 

Dippy

Moderator
4 decimal places of accuracy? Umm... that could be a bit tricky.

Maybe you can post a rough schematic of your circuit.
After all, you mention that you have a regulator in Post#1 and now you imply the Supply voltage is variable (or, more accurately, I infer dah-di-dah).

I suggest you leave the code until you have a circuit design that can justify even 2 decimal places of accuracy.

Do you understand what hippy and Beaniebots have said?

And if you want 'best' accuracy can you see the EXTRA efforts required for the refernce?
Clue: Diodes/LEDs Vf varies with current...

Have you read up on the usage of Voltage References with PICAXEs?
Some you can attach and use it directly as the ADC reference.
Or you can use it to generate an arithmetical correction 'constant' to compensate for Vsupply variations.
 

flak

New Member
*added schematic *

The 18m2 is regulated to 5v yes thats totally separate to what im planning on measuring which is a 12v dc supply though a v divider to adc port.
 

Attachments

westaust55

Moderator
How can i make the adjustments to the code so that i display one voltage to two decimal places and the other higher resolution to 4 places ?
read up on the use of the BINTOASCII command as the primary function to extract digits to insert a decimal point.

You will then need to do the maths to effect multipliers of 100 and 10000 for 2 and 4 decimal places. 4 decimal places will be tricky and you need to consider if it is really required.
 
Last edited:

Dippy

Moderator
Oh, I see. I was thrown off course by post #7.
If your 5V supply is super-steady then you have no need to measure it surely?
Some regulators are excellent.

PS. For good ADCing you should (in addition to any other good practices) fit decoupling cap/caps to the PICAXE power pins as close as poss to the I.C itself.
And, as mentioned by others, a small decouple cap on the ADC input may help in noisy circuits.
 

hippy

Ex-Staff (retired)
How can i make the adjustments to the code so that i display one voltage to two decimal places and the other higher resolution to 4 places ?
If your supply / voltage reference is accurate, your resistors accurate, or you can calibrate out any inaccuracies, it's really a question of performing the maths in such a way that it retains the same accuracy.

Whether PICAXE native 16-bit integer maths can deliver that I don't know, but there would be a number of other methods of doing the maths; larger bit-sized, BCD or byte-per-digit numbers. If someone hasn't already got the solution it would require working out from first principles.

A PICAXE at 5V using READADC10 can notionally read an analogue voltage to greater than 1/1000th of 5V accuracy but you'd have to check the Microchip datasheets for details of actual accuracy. There are other external hardware solutions which can potentially give better accuracy.
 

MartinM57

Moderator
4 digit resolution is easy - you just have to jump through the maths hoops.

IMHO even 1 (decimal) digit accuracy in 12v is a pretty tough requirement for the average hobbyist. Any more than that and you would probably have to calibrate the/each unit with something like a http://www.gellerlabs.com/SVR Series.htm to be able to realistically claim any particular accuracy (and then watch out big time for temperature effects etc).
 

marks

Senior Member
Hi flak,
heres an example you may be interested at looking at.
http://www.picaxeforum.co.uk/showthread.php?t=16081
using a 5,12v supply for the picaxe makes the maths a lot easier!


i think with a 10 bit adc 2 decimal places is all youll really get

my example reads up to 50.1 v (resolution .05) so i only display 1 decimal place

4 decimal places i think youll need a higher bit adc
 

Dippy

Moderator
At the end of the day:
More ADC bits allows higher resolution.
But it requires a stable reference (and better electronic skill) to give higher accuracy.

You will need time/money/skill to get 4 digits accuracy.
But you only need a big display to get 4 digits shown.

And what will you calibrate it with? (as alluded to by Martin above).
.. a 5 quid Ho Phong Ebay Multimeter?

With your circuit, get yourself a good quality 5V regulator and be happy with ONE decimal place. Display more if you want to of course.;)
 
Top