Measuring voltage from micro

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View


I've asked this question here before and got the answer that it's possible
to do it with a resisitor and a cap by measuring how long it takes for the
pin to reach a high state. I tried this and much to my suprise it worked
extremely well first go. I just guessed values for the resistor, capacitor
and delays in software and it all worked straight off! Usually it takes me
hours or days of stuffing around to get something like this working. :-)

Anyway, my question is that it is possible to measure voltages higher than 5
volts while keeping the voltage at the pin continuously below 5 volts. But
there is an off chance of the micro crashing and leaving the pin as open
which would allow the voltage at the pin to exceed 5 volts. This can only
happen while the pin is in it's high (open) state as a low state will hold
the voltage low. Is this likely to be a damage the micro? Should I use
resistors to, say, halve the voltage and keep it at a max of 5volts?

My other question was when I switch the micro pin low to discharge the cap
it will discharge very quickly. Is that likely to be a problem? Should I use
a resistor to slow the discharge?

Thanks again,
Michael



Re: Measuring voltage from micro


Quoted text here. Click to load it

Let me get this clear - you want to measure external voltages with a
digital input on a micro?
I do hope you are using a pin which has a schmitt-input, using a
non-schmitt input is a horribly bad way to do it.

Why don't you just use a micro with an ADC and do it properly?
What accuracy do you need?

Quoted text here. Click to load it

Yes, don't overload the input. But using a resistor divider will upset
any R-C ratio.

Quoted text here. Click to load it

Yes, that would be good practice.

Dave :)


Re: Measuring voltage from micro


Quoted text here. Click to load it

Correct.


The micro doesn't have any schmitt triggers afaik. Why do you say I need
one?

Quoted text here. Click to load it

This technique is used in a lot of consumer goods. If it works ok for a
dishwasher it will work ok for me.

Quoted text here. Click to load it

+/- 2 degree C. I'm not sure what that translates to in voltage yet as I've
only tested measuring a voltage so far.

Quoted text here. Click to load it

ok.


That's not really important as it will just change the lookup table. My code
returns a value from 0 to 255. I'll use a lookup table to determine what
voltage each value refers to.

Michael



Re: Measuring voltage from micro


Quoted text here. Click to load it

Ok, was bit confusing there for a second!

Quoted text here. Click to load it

Because a normal digital input will not have a very defined threshold
voltage over which it reads "HIGH". The datasheet will have a minium
voltage, but you have no way of actually knowing unless you measure it,
and then it will vary from device to device, and with the supply votage
etc.
Not to mention that regular digital inputs are NOT designed for slow
changing inputs, in fact they hate it and can often latch up, go
metastable and just generally do horrible things to ruin your day.
Schmitt inputs are purpose designed for slow changing inputs such as
this. Read the data sheet, it will tell you so.
A schmitt input is like a comparator, it will instantly switch HIGH
when the input passes a defined threshold voltage. It won't have any
problems with slow changing input voltages. This is precisely why many
micros have schmitts on some inputs.

If you don't have a schmitt input then you are going about this the
wrong way.
Exactly what micro are you using?

Quoted text here. Click to load it

How can you possibly say that when you don't fully understand the
application and the requirements?
And it will only work for a schmitt input!

Quoted text here. Click to load it

I assume you are using something like a LM35 temp sensor which gives a
linar output with temperature like 10mV/degC?
In which case you will need an ADC, this method will not have the
accuracy you need for +/-2 deg C
The tolerance on your capacitor alone will likely throw things out of
whack, let along the threshold voltage tolerance.
What temperature range are you tesing over?, and does your circuit
change with this temperature as well? - if it does then add on the
tempco factor for the cap and threshold voltage. Yucko!

Quoted text here. Click to load it

What is the driving factor forcing you to use a digital input to do
this?
Why not simply use another model of your micro which has a proper ADC
built in and do the job properly with real accuracy you can guarantee?
Or why not use an external ADC?

Is this a one-off or a commercial thing?

Dave :)


Re: Measuring voltage from micro


Quoted text here. Click to load it

What do you define as slow? I think it's taking 500us to trigger at the
most.

Quoted text here. Click to load it

The atmel 89c2051 or the phillips p89c668. I'm using the phillips for the
prototype because it can be programmed in circuit.

Quoted text here. Click to load it

Actually I think the micro does have 2 pins that will act like this. You can
supply one of the pins with a predifed voltage and the other will be high if
it's above and low if it's below that voltage. Although I'm not sure the
phillips chip has this.

Quoted text here. Click to load it

This is for thermo fans in a car so the sensor will be a regular car temp
sensor. The sensor will change resistance with temp so I'll be able to
tailor the voltage to my liking.

Quoted text here. Click to load it

I was going to also measure the supply current as changes in this will
likely be reflected in the temp sensor, so this would probably take care of
any temp effects in the caps.

Quoted text here. Click to load it

It was easy, I had a cap and a resistor at home :-)

Quoted text here. Click to load it

Two off really. A friend asked for it and I'm going to use the same thing on
my project car. I might end up making a few others if it works well enough.

Quoted text here. Click to load it

Being that this is a hobby thing I'm not overly keen on changing processors.
I might need a new flash programmer and I'm not keen to move away from 8051
as I can't be stuffed learning a new micro :-)

Quoted text here. Click to load it

Using an external ADC is probably the perfect solution as I can keep my
code, flash programmer, partial circuit board design, prototype etc. :-) Can
you suggest one? I'd probably prefer an I2C one as I've got one other I2C
chip, although I've got plenty of spare pins so it's not critical. I tried
searching but the sheer range had me confused :-)

Thanks for the replies.

Michael



Re: Measuring voltage from micro


Quoted text here. Click to load it

Hundreds of ns is a typical maximum risetime figure for digital logic.
500us is HUGE.
A lot of micros will latch the input data so the chance of
metastability is large.

Quoted text here. Click to load it

Bingo - guess what it's for!

Quoted text here. Click to load it

Ridiculously complex. Just get an ADC with a voltage reference and your
problems are solved.

Quoted text here. Click to load it

I2C ADC's are harder to find, most are SPI/Microwire. The usual Aussie
suppliers don't have an I2C one in DIP that I am aware of.
Most also do not have an internal reference, they reference the value
from the power pin.
But you can use a voltage reference to power it.
The TLC549 is a typical low cost 8bit ADC without reference.

Just found this link:
http://www.atmel.com/dyn/resources/prod_documents/DOC0524.PDF
It tells you how to make an RC ADC with 50mV resolution and <0.1V
accuracy on the AT89C2051 using the internal compartor.
It also explains component variations as I mentioned.

Unless you do the calculations to show that you can get the required
accuracy with the RC solution then go with the external ADC and
reference.

Dave :)


Re: Measuring voltage from micro


Quoted text here. Click to load it

Thanks Dave, I'll be offline for a couple of days but will have a look when
I get back. Thanks for all your help.

Regards,
Michael



Re: Measuring voltage from micro


Quoted text here. Click to load it

I ended up ordering the PCF8591 from futurlec. It's got I2C and 4 analog
inputs so I've got a couple extra just in case. Shipping is 1 to 2 weeks
though :(

http://www.futurlec.com.au/Philips/PCF8591P.jsp

Michael



Re: Measuring voltage from micro


Quoted text here. Click to load it

Cool.
You'll need a reference for that. Something like a 2.5V LM336 from
Jaycar will do the trick.
Then just use a divider on the input if you need to measure greater
than 2.5V

Dave :)


Re: Measuring voltage from micro


Quoted text here. Click to load it

Is it possible to use the 5V from the 7805 that's powering everything?

Michael



Re: Measuring voltage from micro



Michael C wrote:
Quoted text here. Click to load it

Only if you don't care about the +/-5% tolerance!
You could measure it and factor it into your program, but that is poor
engineering!
Tempco of the 7805 is about 1mV/degC from memory, so fairly small in
the scheme of things. So absolute tolerance is the killer.
If you are using a proper ADC then use a proper reference and be done
with it, they cost peanuts.
There is little reason to kludge up something like this, do it right
and you will have an accurate system without any heartache.

Dave :)


Re: Measuring voltage from micro


Quoted text here. Click to load it

No worries, the cost of an extra part or 2 isn't important, I just like to
know why :-)

Michael



Re: Measuring voltage from micro


Quoted text here. Click to load it

BTW, you were absolutely correct. Now that I'm getting a little further
along in the project it's apparent the RC temp circuit is no good. When I
get to a certain temp I switch on a relay, but switching on the relay causes
the measurement to change and drop back below the threshold temp, which
causes the relay to go off, then the temp rises turning the relay back on.
The relay was going on and off about 100 times a second at first. At least
it's been a good chance to fine tune the software against poor inputs :-)
Hopefully I'll have the ADC soon.

Michael



Re: Measuring voltage from micro



Quoted text here. Click to load it

It shouldn't be rocket science to build some hysteresis into either the software
or the input circuit to avoid that cycling.

MX & HNY to all

Re: Measuring voltage from micro


Quoted text here. Click to load it

So far I've added a delay of approx half second between sampling, then
averaging over the last 8 samples and then a user configurable hysteresis.
All of these made a big improvement but they still didn't solve the problem.
If I set the threshold temp to 90*C and then set the temp to 92*C the relay
will turn on, but then the relay turning on will cause the temp measurement
to drop to say 85*C and the relay will turn off. Then the temp measurement
will go back up to 92*C etc. The end result is the relay just oscillates
more slowly. At first I was trying to work out how to stop that but after I
while I realised that's really what it should do if the measured temp is
cycling. I think I've got the software side of it working ok I just need to
improve the temp measurement circuit.

Michael



Re: Measuring voltage from micro



Quoted text here. Click to load it

You say the temp drops 92->85 (say) when the relay pulls in.  If this is the
result of the pull-in itself, as distinct from what the relay drives (i.e. it is
related to a volt-drop issue etc) then the temp itself hasn't dropped - only
your reading has - and you need to have that masked with enough hysteresis to
offset that "drop" plus an operating hysteresis.

Re: Measuring voltage from micro


Quoted text here. Click to load it

That's true, the temp has stayed the same.

Quoted text here. Click to load it

The problem then is that big hysteresis will get in the way if the temp
really does drop from 92 to 85, I really need a more accurate measurement.

Michael



Re: Measuring voltage from micro


Quoted text here. Click to load it

Assuming you know when the relay is energised, and you know the
corresponding change in voltage when that happens, can't you just compensate
for that in code with some kind of offset applied to temp readings?




Re: Measuring voltage from micro


Quoted text here. Click to load it

That is possible but I think that should be done after I've gotten to temp
measuring circuit working properly. And then it should only be done if
necessary. I could see some side effects such as different relays being
installed which causes the temp to change a different amount.


Quoted text here. Click to load it



Re: Measuring voltage from micro


Quoted text here. Click to load it

One alternative might be to use a smarter temeprature sensor, such as a
DS18S20 that delivers the temperature as a digitally rather than as a
voltage. You'd need to implement a 1-wire bus, but there seem to be
implementations available for most micros which would make it pretty easy to
integrate into your existing code.

http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2815




Site Timeline