Measuring voltage from micro

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

Reply to
Michael C
Loading thread data ...

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?

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

Yes, that would be good practice.

Dave :)

Reply to
David L. Jones

Correct.

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

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

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

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

Reply to
Michael C

Ok, was bit confusing there for a second!

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?

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!

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!

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

Reply to
David L. Jones

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

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. Although I'm not sure the phillips chip has this.

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.

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

Thanks for the replies.

Michael

Reply to
Michael C

Use a 5.1 (or 4.7v) zener diode on the pin to keep it in the safe range. or a resistive divider before the timing resistor.

if the output is short circuit protected by design and the cap has 5V or less in it it won't do any damage,

Bye. Jasen

Reply to
Jasen Betts

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.

Bingo - guess what it's for!

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

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:

formatting link
It tells you how to make an RC ADC with 50mV resolution and

Reply to
David L. Jones

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

Reply to
Michael C

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

formatting link

Michael

Reply to
Michael C

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

Reply to
David L. Jones

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

Michael

Reply to
Michael C

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

Reply to
David L. Jones

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

Michael

Reply to
Michael C

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

Reply to
Michael C

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

Reply to
budgie

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

Reply to
Michael C

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.

Reply to
budgie

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.

Michael

Reply to
Michael C

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?

Reply to
Poxy

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.

Reply to
Michael C

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.