Reading VOLTAGE?

RogerTango

Senior Member
I am still new to the PICAXE (OMG, where have I been? I just now discovered them! They have been around for a long time!), I got two 14M chips from SparkFun, and boy... Im having a blast!

Ive already dreamed up two dozen projects I can build with them!

One thing Id like to do, is build a battery voltage minder. I want to be able to read the voltage of the battery using the 14M chip.

One thing though.. Ive go not clue how to do that!

Can someone advance of what I need to interface say a car battery to a 14M chip and read the voltage, including a short snippit of BASIC code?

Many thanks!!
Andrew
 

BeanieBots

Moderator
There are several things you need to know first.
The first is that any PICAXE input can only cope with a voltage which is equal or less than its power supply voltage.
A typical PICAXE would be 5v, so to measure a 12v car battery, you will need a potential divider (or something more complex) to bring it down to 5v.

The second VERY important thing to know is that the PICAXE uses its own power supply as a reference against which it measures voltages. That means that any reading you take will be a function of whatever voltage your PICAXE is driven at.

To read a voltage on a pin in software, have a read of the command "ReadADC".

There's a few more things which can catch you out, but have a read and then think about it all for a while. If you have any more questions, then ask away.
 

Dippy

Moderator
"Can someone advance of what I need to interface say a car battery to a 14M chip and read the voltage, including a short snippit of BASIC code?"

- yes, if you are more specific.

As BB has said, the PICAXE will require a fixed (e.g.) 5V supply.
This 'interface'; Is the PICAXE to be powered by the 12V car battery in addition to reading it's voltage? Or will it have a separate supply.

Will the battery every be connected to a car's charging circuit while the PICAXE is conencted to it?

As BB has said, the PICAXE volt supply should be rock-steady.

So, tell us what you want to do.....exactly. Do a drawing. Post an image using the 'manage attachments' button.

Lastly, what is your level of electronics and coding knowledge (I'm guessing not-a-lot??)
Is this a school project?
 

RogerTango

Senior Member
BB, please expand on this sir.

I will be powering the voltage monitor off a power source OTHER than the battery being monitored.

I can simply use a resistor in series with the battery being monitored, if I am correct please advise. What value resister would you suggest to drop 14 volts down to 5 volts for monitoring?

Can you show me a code snippit of how to read the voltage on ping using READADC?

In the mean while, I will be reading up on READADC command. One reason I sound like a noob is... because I am a noob! HA HA!

Many thanks for your helpful reply,
Andrew
 

RogerTango

Senior Member
Dippy,
Thanks for replying.

My experience with the PICAXE is beginner, and I must admit my electronics is basic. I can build circuits, but I cannot design them from concept. I am good at "kludging" circuits together to build a device.

The project is more along the lines of ham radio. Many times I like to operate portable, but reading a meter can be difficult. The voltage drops while transmitting with a radio is hard to follow with either a analog or digital VOM. So, if I can build an interface that can "measure the voltage", it would be a BIG help!

In concept this would be used to measure the voltage 2 ways, one is with the battery at rest, and one with battery under load. How do I measure the battery under load? I just send a sig to the chip off the key circuit of the radio, and when it measures voltage with the pin HIGH, then I have captured under load voltage. (When ever I key the radio, the PICAXE will know that is the under load reading (Icom 706))

I can output the data back to the laptop via a serial cable, or... have LEDs to indicate voltage level (varying flash rate, etc..).

The only thing left is, to discover the critical low voltage to halt putting a draw on the battery to prevent damage!

I am brainstorming several ideas, this just happens to be one of them. Another project at hand is to interface a standard charger into a "smart charger" by allowing the PICAXE to check battery voltage and decide if the charger should be applied. That project is later, after I learn more!

Thanks,
Andrew
 

premelec

Senior Member
Note that to get full accuracy for the ADC input the source resistance of the thing you are reading should be 10K or less ohms... e.g. for your '12' volt battery and a 5volt REGULATED PICAXE supply you might use 10K from the ADC pin to V- and 20K IN SERIES from the battery to that ADC pin - this would give you 1/3 the battery voltage [which in practice could be way over '12' volts] at the the ADC input so full scale reading [e.g READADC10 =1023] = 15 volts battery voltage... In this case the input source resistance is effectively 10K in parallel with the 20K = r1*r2/(r1+r2) = 6.667K well below the 10K maximum... [note they ARE in series but the tap between them is effecively in parallel].
 

RogerTango

Senior Member
premelec,
Thanks very much. Is it possible to sketch that out for me please? I cannot visualize what you are saying. Thank you for your patience and understanding.

Andrew
 

BeanieBots

Moderator
What premelec is saying is that a 250k won't be any good for you.
The 'gmoon' example shows a divider made from 10k and 5k. That's fine but you won't easily find a 5k resistor. Your best bet is to use 10k and 4k7.

So, let's say your PICAXE is powered by a 5v regulator and you use a divider of 10k & 4k7 and the battery voltage is 12.5v

The divider will present a voltage to the PICAXE of:-
12.5*4.7/(10+4.7)=4v (or ever so slightly less)

The PICAXE splits the reference voltage (5v in our example) into 255 segments. So the "number of segments" for 4v would be 255*4/5=204

So, the following code would put the value 204 into variable "b0" if a potential of 12.5v is applied to input 1 on a PICAXE powered from 5v.

"ReadADC 1,b0".

Hope that's clear.
You can then use what you learned in your other thread to send that value back to your computer. Have fun.
 

alband

Senior Member
You got two 14M's right?

BeanieBots: does a PICAXE provide a PWM in reference to its supply voltage in any way?
If not, I think you could do this:
Have one chip sending a constant output of 4V say, using PWM and some analogue circuity to turn the PWM signal into a good DC supply. This should always be the same despite its input voltage.
Then have the second chip pick up that voltage and compare that to whatever you want to measure.

If you only have one chip, perhaps you could put the PWN output into the same chip's ADC pin?
 

BeanieBots

Moderator
Alband,
Suggest you start with a formula for converting PWM to voltage. (TIP: Vcc will be in it)
In other words, your proposal DOUBLES the reference voltage problem!
 
Last edited:

alband

Senior Member
Oh:eek:
Is there any way a PIC can give some kind of set output irrespective of VCC?
That's all that's needed.
 

BeanieBots

Moderator
Don't know. I'd have to read all datasheets for all PICs to answer that!
Bottom line, with ANY measurement, you need a reference.
All PICAXE chips use Vcc as the reference for ADC.
All PICAXE chips use Vcc as the voltage for PWM.
 

RogerTango

Senior Member
BB, your explanation makes a LOT of sense, even if Im an 'electronics idiot'. Thank you very much for the time and thought needed to simply that for me.

One more thing, I am still having trouble visualizing how all this will wire together. How do the 10K and 4K7 wire with the battery to be monitored, and the 14M? One last thing... I am assuming the (-) of the battery to be tested needs to go to zero (0v) on the chip. (A quickie sketch would be very much appreciated!)

Is it the SAME schematic as posted by gmoon here?
http://www.instructables.com/community/PICAXE-battery-voltage-monitor/


I apologize if I frustrate anyone with my elementary questions, my knowledge is somewhat limited. All my knowledge has been as a hobbyist, I have no formal education or training.

Learning is fun, you guys are fantastic help!

Andrew
 
Last edited:

BeanieBots

Moderator
Battery 0v to PICAXE 0v.
Battery positive to one end of 10k.
Other end of 10k to 4k7
Other end of 4k7 to 0v.
Join of 10k & 4k7 to ADC input pin of your choice.
 

RogerTango

Senior Member
BB, thanks.... I figured that was correct, but before I toast a chip, I wanted to verify.

Okay... here goes the Battery Monitor project!!!! :):):)

Andrew
 

hippy

Ex-Staff (retired)
Is there any way a PIC can give some kind of set output irrespective of VCC?
Probably. As long as an output voltage can be generated and its level adjusted that can be compared against a fixed reference and adjusted appropriately.

Okay... here goes the Battery Monitor project!!!! :):):)
Don't forget to have a search through the forum and completed project sections and even fire up your favourite search engine because you'll find a lot of interesting discussions on how batteries work, the concepts of charging, discharge curves and all manner of things useful, particularly where people have done similar things themselves.
 

moxhamj

New Member
My favourite battery voltage divider is very simple. A 20k 1% resistor in series with a 10k 1% resistor. It divides by three, so 15V is 5V. A car battery should never go that high (unless it was grossly overcharged).

Use a 5V regulator. Eg a LP2950 to power the picaxe.

Connect the ground of the picaxe circuit to the negative of the battery - so you have a common ground.

Use readadc10 - it is more accurate.

Then do a bit of maths to convert to a voltage.
 

westaust55

Moderator
Keep in mind that if you are measuring the voltage from a standard car type battery with a nominal voltage of 12 Volts,

That:
1. A fully charged battery will in fact be around 13.8 Volts,
2. A car alternator will typically output at 14.2 Volts - the slight increase is needed to "force" the current into the battery,
3. If using some separate charger that could have pulse width modulated (PWM) charging schemes with voltages up to 17/18 volts and maybe even up to 21/24 volts.
4. When a battery is first disconnected from a charger it has a slightly higher (surface charge) voltage

So firstly you need to know the actual charged battery voltage correctly and if the PICAXE will be connected during charging then what type of charger will be used.

Once you have this sorted out then you can correctly size the resistors for a potential divider and may need to look at more complex circuitry if PWM charging is involved.
 

moxhamj

New Member
westaust55 - good points. I didn't think about a PWM charger - mine is linear. So if the charger is pwm, use a voltage divider, and put a smoothing cap between the ADC input and ground. Eg 1uF.

PWM charging - that gets complicated.
Sampling volts and estimating % charge = very complicated - you need temperature compensation for a start.

For small batteries, ie SLAs, I like the UC3906. Futurlec have them for $6.90. And Altronics I think still sell the complete kit which charges up to 500mA. I've a few of those around the place keeping batteries happy. And now I can neatly begin and end a post with almost the same words by stating that Altronics are in Western Australia.
 
Last edited:

RogerTango

Senior Member
So far, I have decided to sample the batteries voltage and control the connection of the charger to the battery via a relay, the actual battery voltage will be sampled for a yet to be determined time after the charger has been disconnected. If the battery voltage is still too low, the charger is applied for another yet to be determined amount of time and then sampled in the same method as outlined above.

This way, you never measure the voltage of the charger and battery together.

This is all in the concept stage.

There are other projects which I need to read battery voltages for, will detail them in time.


Thanks,
Andrew
 

moxhamj

New Member
That is quite a good way of doing it. I think there are even graphs out there that do link capacity to volts if you wait a certain time. But I'm not sure what "a certain time" is. I've played with solar charging where the best time to sample is just before the sun comes up - then it has been resting all night. Is this a mains powered charger or solar?

You can always float at 13.8V once it has charged and that is a simple 317 circuit.

Relays are a good solution and interface easily with picaxe.

File attached as per PM.
 

Attachments

Last edited:

westaust55

Moderator
Ideally you should also wait a short period after the battery is disconnected from a charger (so the surface charge dissipates) and you read the voltage relating more to the "State of Charge" (SOC). Same applies when disconnecting from a heavy load as the battery voltage will recover slightly.

Some suggest that the "short period" should be 20 - 30 minutes, and I have in the past even read of times like an hour.
 

Dippy

Moderator
Though it depends on the application whether you have the time to let it settle.

It would be a bit daft in a well used solar system to waste 30 minutes of sun power every few hours.
 

BeanieBots

Moderator
Worth spending a few minutes having a look at my "Battery Capacity Meter" in the finished projects section. I'm currently working on a version which will include charging for use in a solar situation.
 
Top