Maker Pro
Maker Pro

Measuring voltage from micro

M

Michael C

Jan 1, 1970
0
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
 
D

David L. Jones

Jan 1, 1970
0
Michael said:
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. :)

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?
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?

Yes, don't overload the input. But using a resistor divider will upset
any R-C ratio.
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?

Yes, that would be good practice.

Dave :)
 
M

Michael C

Jan 1, 1970
0
David L. Jones said:
Let me get this clear - you want to measure external voltages with a
digital input on a micro?
Correct.

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.

The micro doesn't have any schmitt triggers afaik. Why do you say I need
one?
Why don't you just use a micro with an ADC and do it properly?

This technique is used in a lot of consumer goods. If it works ok for a
dishwasher it will work ok for me.
What accuracy do you need?

+/- 2 degree C. I'm not sure what that translates to in voltage yet as I've
only tested measuring a voltage so far.
Yes, don't overload the input.
ok.

But using a resistor divider will upset
any R-C ratio.

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
 
D

David L. Jones

Jan 1, 1970
0
Michael said:

Ok, was bit confusing there for a second!
The micro doesn't have any schmitt triggers afaik. Why do you say I need
one?

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?
This technique is used in a lot of consumer goods. If it works ok for a
dishwasher it will work ok for me.

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!
+/- 2 degree C. I'm not sure what that translates to in voltage yet as I've
only tested measuring a voltage so far.

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!
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.

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 :)
 
M

Michael C

Jan 1, 1970
0
David L. Jones said:
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.

What do you define as slow? I think it's taking 500us to trigger at the
most.
If you don't have a schmitt input then you are going about this the
wrong way.
Exactly what micro are you using?

The atmel 89c2051 or the phillips p89c668. I'm using the phillips for the
prototype because it can be programmed in circuit.
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.

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.
I assume you are using something like a LM35 temp sensor which gives a
linar output with temperature like 10mV/degC?

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.
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!

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.
What is the driving factor forcing you to use a digital input to do
this?

It was easy, I had a cap and a resistor at home :)
Is this a one-off or a commercial thing?

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.
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?

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 :)
Or why not use an external ADC?

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
 
D

David L. Jones

Jan 1, 1970
0
Michael said:
What do you define as slow? I think it's taking 500us to trigger at the
most.

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.
The atmel 89c2051 or the phillips p89c668. I'm using the phillips for the
prototype because it can be programmed in circuit.


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.

Bingo - guess what it's for!
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.


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.

Ridiculously complex. Just get an ADC with a voltage reference and your
problems are solved.
It was easy, I had a cap and a resistor at home :)


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.


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 :)


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 :)

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 :)
 
M

Michael C

Jan 1, 1970
0
David L. Jones said:
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.

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
 
M

Michael C

Jan 1, 1970
0
David L. Jones said:
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.

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
 
D

David L. Jones

Jan 1, 1970
0
Michael said:
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

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 :)
 
M

Michael C

Jan 1, 1970
0
David L. Jones said:
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

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

Michael
 
D

David L. Jones

Jan 1, 1970
0
Michael said:
Is it possible to use the 5V from the 7805 that's powering everything?

Michael

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 :)
 
M

Michael C

Jan 1, 1970
0
David L. Jones said:
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.

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

Michael
 
M

Michael C

Jan 1, 1970
0
David L. Jones said:
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.

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
 
B

budgie

Jan 1, 1970
0
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.

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
 
M

Michael C

Jan 1, 1970
0
budgie said:
It shouldn't be rocket science to build some hysteresis into either the
software
or the input circuit to avoid that cycling.

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
 
B

budgie

Jan 1, 1970
0
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.

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.
 
M

Michael C

Jan 1, 1970
0
budgie said:
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

That's true, the temp has stayed the same.
- only
your reading has - and you need to have that masked with enough hysteresis
to
offset that "drop" plus an operating hysteresis.

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
 
P

Poxy

Jan 1, 1970
0
Michael said:
That's true, the temp has stayed the same.


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.

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?
 
M

Michael C

Jan 1, 1970
0
Poxy said:
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?

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.
 
B

budgie

Jan 1, 1970
0
That's true, the temp has stayed the same.


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.

Not really. "when relay on, add 5C to reading, plus the 2C hysteresis"
 
Top