PDA

View Full Version : PIC16F88 for 0-12v measurement



jswayze
- 31st August 2004, 14:34
Hi,

I know there's code out there for voltage measurement using RA0 as input and using the ADC (in my case, on the 16F88) - but it looks like the limit is 5 volts. What can I do to measure larger voltages, say up to 12 volts?

Also, in PBP, the LCD routine likes to use RA0-4 for LCD connections. Since I need to use RA0 as an input, how does one reconfigure the LCD routine to use different pins for the LCD output?

Thanks,

Jeff

Dwayne
- 31st August 2004, 16:59
What can I do to measure larger voltages, say up to 12 volts?

A cheap and easy way would be to use two resisters in Series, and read off the center tap.

Dwayne

jswayze
- 3rd September 2004, 05:29
Thanks... I think that will do the trick... now for my other question - please see my LCD thread

Jeff

Mark Scotford
- 5th September 2004, 00:48
As Dwayne says, two resistors will do it, just to help a little further, the resistor values should be 6K8 and 4K7 wired in series, with one end of the 4K7 connected to ground (0V). Feed the 12V signal into the opersite end of this chain and take where the two resistors meet into the ADC input of your PIC.

jswayze
- 5th September 2004, 02:12
Thanks Mark. I'm obviously a far cry from an electronics wizard, but I though a voltage divider always used the same value resistors... Why two different values, and why the ones you specified?

-Jeff

Bruce
- 5th September 2004, 04:15
The PIC A/D will only work from 0 to Vdd. If Vdd = +5V, then you need to limit +V on the A/D input-pin to +5V.

You'll need to tap into the V divider where +V on the A/D input-pin is never > +5V or Vdd.

Look here for your answer http://www.doctronics.co.uk/voltage.htm

Mark Scotford
- 5th September 2004, 21:47
Bruce is right of course, just to clarify what he is saying. You need to measure 0 - 12V and the input on the ADC measures 0 - 5V (assuming you are using 5V & 0V as your reference voltages). You must never exceed the maximum input voltage of the PIC which I presume in this case is 5V. If your resistor chain had both the same value resistor, then the output voltage would be 0 - 6V. I chose resistor values that would roughly turn a 0 - 12V signal into a 0 - 5V signal. To make the equation simpler to understand I would have suggested a 7K resistor (you are trying to drop the output voltage by 7V) instead of a 6K8 and a 5K resistor instead of a 4K7, but these values are not available, thats why I suggested the values I did, hope this helps.

Dwayne
- 5th September 2004, 21:48
Hello Js,

JS>>Why two different values, and why the ones you specified?<<

The voltage drops cross all the resisters and is equal to your Starting voltage when it is done.

For example If you have a 10 volt supply and two resisters across that 10 volts of equal value, each resister will drop 5 volts each.

if one resister is 1000 ohms, and the other is 4000 ohms, together they will drop the full 10 volts, but the 4000 ohm resister will drop 4000/5000 = 4/5*10 = 8 volts. and the little resister will drop 1000/5000 = 1/5 * 10=2 volts. Since you cannot measure anything above 5 volts, you must measure how much you drop over the 2 volt resister.

6.8 and 4.7 total 11.5 ohms

6.8/11.5 * 12volts = 7.1 volts drop
4.7/11.5 * 12volts = 4.9 volts drop

4.9 volts is under your 5 volt limit. Thus you want to measure the voltage drop across your 4.7 ohm resister. 6.8 and 4.7 ohm resisters are not hard to come by...Thus a very good choice of resisters to use.

Dwayne

Bruce
- 5th September 2004, 22:23
Yikes...!

You could use that voltage divider for a small space heater..;o]

12V/11.5 ohms leaves you with a whopping 1.043A drop across your voltage divider.

jswayze
- 5th September 2004, 22:25
Thanks fellas. Crystal clear.

FWIW, everything is working perfectly now - LCD, D/A, and results. Thanks to everyone for helping out!

-Jeff

jswayze
- 6th September 2004, 02:01
Hmm... don't want heat the inside of my plane :)

Theoretically I could use very small value resistors as long as their ratio worked out correctly, right? How about two low-resistance trim pots to create the perfect 1.404 ratio?

Melanie
- 6th September 2004, 10:27
Uggghhhh... NOT low value Resistors for a Potential Divider measuring VOLTAGE... you need the highest value ones you can get away with... The lower the value, the more current consumption and the more heat. The opposite is true is you want to measure CUURENT (which is not the case in this instance).

Take an example R1 and R2 connected in Series. The top-end of R1 connects to the Supply to be measured. The bottom-end of R2 connects to Vss (0v). The junction of R1/R2 connects to the PIC's ADC pin.

The Recommended ADC input impedance is around 10K (unless it's a 12F which is around 2K), so R2 is recommended at that value (a decoupling capacitor say 100nF is also good accross R2 to kill any noice that might upset our readings). R1 will now depend on the voltage to be measured.

If you select 10K for R1 and for R2, then you will get a Reduction Ratio of R2/R1+R2 ie (10000/(20000) which is 1/2 or 0.5. 12v in will give you 6v out (12 * 0.5), which is not enough reduction for our ADC's 5v maximum. Trying 15K for R1, we get 10000/25000 which is 0.4... 12v input x 0.4 = 4.8v reduction which is now in-range for our ADC.

Always plan to measure your maximum voltage. Is your fully charged Battery 12v? You might find it's a little higher. To calculate the maximum voltage that we can measure, work backwards... 5v (our +VRef) / 0.4 (the reduction in our 15K R1 example) = 12.5v (maximum voltage we can measure).

Finally, use High Tollerance 1% or better Resistors. No point in trying to measure microvolts when crappy Resistors could give you half a volt error. And if you want some better precision, REF-02 or other Precision Voltage Reference won't tie you to the wide tollerances of the usual 5v Regulator chips.

But then again, if you're only worried that your R/C plane is running out of juice and in danger of hospitalising some innocent bystander, you don't exactly need laboratory precision.

Melanie

NavMicroSystems
- 6th September 2004, 12:49
Originally posted by jswayze
Hmm... don't want heat the inside of my plane :)

Theoretically I could use very small value resistors as long as their ratio worked out correctly, right? How about two low-resistance trim pots to create the perfect 1.404 ratio?

You don't need two POTs, one would do, and if this one POT is a 10 turn precision trimmer (50 kOhms) you can set any ratio you want.



---------- INPUT
|
|
---
| |
to ADC ----|---|-->
| |
---
|
|
---------- GND

Melanie
- 6th September 2004, 13:32
You then run the risk of adjusting the POT beyond the maximum permitted voltage input to the ADC. Apart from the fact that the cheapest POT costs around 8 cents (US$) plus some 36 cents per minute in 'average' labour to calibrate, whilst Resistors are less than 1 cent each.

NavMicroSystems
- 6th September 2004, 13:48
Melanie,
you are absolutely right, the POT solution is nothing to be used in "mass-production"
but I think it is a good solution for a prototype.
For "overvoltage protection" one could have an extra resistor in series with the input.

regards:

Ralph

jswayze
- 6th September 2004, 14:03
Yes, of course - a POT is already a voltage divider... ugh...

Anyhow, that seems like a good solution. If I do go into "production" I could calibrate each one by hand and drop some epoxy on the POT to seal it.

Thanks again, what an incredibly helpful group!

-Jeff

jswayze
- 6th September 2004, 20:44
Melanie,

I was re-reading the thred and realized that I missed your post re: low-value resistors. UGH is right. Of course you want high-value. Electronics is not my day job : )

The actual application for this is for use with Lithium-Polymer batteries in R/C aircraft. Apparently Li-Po cells can be damaged if they're drawn below 3 volts, so the trick is to monitor their voltage in real-time so you know where you're at. Most electronic speed controls now contain a cutoff circuit that kills the motor when the battery goes below 3v per cell, but depending on your plane and what you're doing when the motor cuts off, this may not be a good thing. With mine I'll be able to see the trend in voltage - perhaps even chart it using a bar graph on the LCD along with an estimation of time left before 3v. I might even add some beeps to make it really fancy :)

Thanks for your patience with me, and thanks greatly for your time. I do understand how valuable it is.

Jeff

Dwayne
- 7th September 2004, 17:00
Hello Bruce,

Bruce>>You could use that voltage divider for a small space heater..;o]

12V/11.5 ohms leaves you with a whopping 1.043A drop across your voltage divider.<<

<chuckle> I left the "K" off of my resisters, because I was explaining why Mark chose the values that he did. I got tired of typing in "Zeros". When the explaination was why he chose the two values instead of two equal resistors <g>.


My goof.

Dwayne

Dwayne
- 7th September 2004, 17:18
Hello Js

JS>>Theoretically I could use very small value resistors as long as their ratio worked out correctly, right? How about two low-resistance trim pots to create the perfect 1.404 ratio?<<

Well... not really...

Current = Voltage divided by resistance.
c= v/r

if your e=12, and your r = 1, your current is equivalent to

c=12/1 = 12 amps. That will drain most batteries in nothing flat.

If your Resistance is very high... (lets say 1 million ohmes)

c= 12/1,000,000

c=.000012 Which is very little current. batteries will last almost forever.

So, you choose the Resistance that is the highest, that will work reliably with your PIC. I have never "tested" the high end range of the PIC (nor the low end). But since you are using this on a airplane... go for higher resistance. The more resistance the less current, and that means longer battery life.

Now my disclaimer... I used "C" for current, and v for voltage... Usually it is "I"...for current and E for voltage in this equation...but live goes on...<g>

Dwayne

Dwayne
- 7th September 2004, 18:02
Hello Js,

Js>>With mine I'll be able to see the trend in voltage - perhaps even chart it using a bar graph on the LCD along with an estimation of time left before 3v. I might even add some beeps to make it really fancy <<

Hey Js...I flew RC's for many many years... Was on a show team in real airplanes and well as RC planes. My favorite preformance was landing a airplane upside down and taking off upside down (this was a RC bird). It was a Stuntmaster with a Rosse 45 on it. 2HP enigine. My realtime favorite was a 200+HP Pitt special... It was smaller than a cessna 150, but had more power by far. It was a two seater...suicide stick kind.

What you could do, is run when the voltage goes below 5 volts, kill your engine survo. We used to run 4 1.4 volt to 1.5 volt Nicads for the receiver. That is equivalent to about 5.6 to 6 volts. Thus, if you can check your voltage, and kill your engine servo if it falls below a certain value.

What do they use nowadays for voltage in a airplane? 4 Nicads still? What frequency do they fly on?

I still have my Tube stepup...It was a R/C "pulsator" the rudder pulsated back and forth, and you push a button once to go right, Push it again to level off, push it again to go left. And if you didn't have enough turns in your rubber band to do enough turning, you were stuck in whatever you were last doing..<g> Then graduated to 27mhz... Then went to the Ham Bands. I only have a few 72 mhz radios. Then the went to 77 mhz? (or do I have them switched around). I do't know what they fly now. ON the public bands, I flew Green and white. 77.640? Its been to many years <g>

Dwayne

jswayze
- 7th September 2004, 18:18
Sounds like you've done some REAL flyin! I'm pretty low-end at this point, but I'm learning fast. My problem is that I tend to get into something (ie, RC airplanes), learn "just enough" about them, then start doing modifications and upgrades that are out of scope of the original hobby. At least here I'm trying to meld the RC hobby with the PIC/electonics hobby. I'm a mechanical engineer by training, so most of this stuff comes fairly easy (though at the expense of my free time...)

Anyhow, 6-7 cell NiCd batteries were the start of electric R/C, then came NiMh which had better capacities and weight. Now the latest thing is Lithium-Polymer (or LiPo) that has an incredible energy density. The discharge rate isn't as high as the regular batteries, but it's getting better all the time. The other downsides of LiPo's is that you can't overcharge them (they explode) and you can't run them down below 3 volts or you'll damage them - hence my voltage monitor project.

Transmission is at 72MHz for air, and I think it's 75MHz for ground vehicles. Many of the new radios are digital, and in my case, fully programmable as well.

Check out www.rcgroups.com for some great discussion and info on the "state of the art" of electronic flight!

-Jeff

Dwayne
- 7th September 2004, 19:47
Hello Js,

Js>>Sounds like you've done some REAL flyin! <<

Can't get any better <g>... except for the cost. I am rated as multi-land-complex (briefed). I don't fly anymore... Maybe one time a year or two times a year. I will sometimes get a call from a friend or two, and take to the air. I lost my medical, when I went deaf. In aviation, you have two licenses (per se) you have your license that "rates" you, and you have your medical. If you lose your medical, you have not lost your license, but you can't fly because it takes both the medical and license to fly legally. So, nomatter how they "candy" up the idea you don't lose your license.... you lose it <g>.


Js>>I'm pretty low-end at this point, but I'm learning fast. My problem is that I tend to get into something (ie, RC airplanes), learn "just enough" about them, then start doing modifications and upgrades that are out of scope of the original hobby. At least here I'm trying to meld the RC hobby with the PIC/electonics hobby. I'm a mechanical engineer by training, so most of this stuff comes fairly easy (though at the expense of my free time...)<<

But this is what is important and fun. And it is because of things like this, my Tag line exists. I enjoy playing in different areas of electronics, and when I see a improvement, I go for it. Its called positive thinking, with a little bit of fun, wit, and adventure to learn.

Js>>Anyhow, 6-7 cell NiCd batteries were the start of electric R/C,<<

Oh!... We are talking Battery planes!... I was thinking Gas powered planes <g>. I think I only saw 1 electric plane fly. They were just coming out when I stopped flying R/C. They were greatly underpowered, and you flew at about 2 MPH over stall speed. (or it seemed).

Js>>The other downsides of LiPo's is that you can't overcharge them (they explode) and you can't run them down below 3 volts or you'll damage them - hence my voltage monitor project.<<

Excellent... How long does it take to charge? and How long of a flight?

Js>>Transmission is at 72MHz for air, and I think it's 75MHz for ground vehicles. Many of the new radios are digital, and in my case, fully programmable as well.<<

Ok, then Green and white was a different frequency. I switched to Ham bands (50 mhz range) because noone had Ham licenses then...You had to pass Code tests and stuff <g>. Thus I and only 1 other person flew 50 mhz. Very nice frequency.

Js>>Check out www.rcgroups.com for some great discussion and info on the "state of the art" of electronic flight!<<

I shall...

By the way...Why don't you use that same PIC chip to monitor your Receiver battery too? And when it gets low, kill your motor? Imagine 1 pic chip to help you save your bird, and save your battery at the same time....Nothing like losing a receiver in mid flight...<g>

I remember one time, I was flying a R/C P51 Mustang...it was a .40 engine... I was doing manuevers with it at the airfield...I was doing some loops, and at the very top of the loops, you must do two things to make them look perfect....

1. Cut power.
2. Let off the elevator.

When doing this, two things happen. 1. It puts less stress on the airplane coming out of the dive (Less power). 2. It "softens" the top part of the loop, so that the elevator doesn't "Kick the nose" down, and make it look like a Egg Loop. Well, coming at the top of my loop, I cut power... and no power was cut...I checked my elevator, and no response... I put down my transmitter and started walking while the bird was still flying. My friends thought I was nuts. But I knew....The bird did exactly what I thought it would.... full power at a 30 degree angle into the ground, as it came out of the loop. Needless to say...the bird didn't survive the crash...I was hurt, because I had close to 1000 hours on that bird. I guess it was close to retirement anyhow...I just would have liked to retire it a different way.

Dwayne