Readadc behaviour

Kiwi Bruv

Member
I have an intermittent problem.

I have a transmitter powered with 2 AA cells. It transmits temperature and the battery voltage as measured by an analogue input once a minute.

The batteries are actually giving 4.9V so I set a software alarm threshold of 4.5V to tell me when the batteries are low.

Randomly, usually several days apart I get the low batt message. I accept the alarm and it may come back once or twice quite quickly (within a few minutes) but then will run fine for a few more days.

Whenever I measure the voltage it's fine. Th picaxe is sleeping for 50 s out of a minute.

I thought it might be temp related, but it can happen at any time of the day it seems.

Am I seeing true dips in battery voltage or is it function of the ADC and waking from sleep mode? Or do I just need to be less of a cheapskate and regulate the supply?
 

BeanieBots

Moderator
Can you give a few more details about how you measure the battery voltage? (circuit description).
When you "test" the battery, is it on or off load?
What sort of temperature range does it experience?
 

Kiwi Bruv

Member
+ve rail voltage is connected straight in to ADC2 (pin 1?) on the 18x chip.

When I measure the battery it's under load, but very small current I think although I haven't measured. The circuit has the recommended temp sensor, humidity sensor and LDR. I think the load will be all LDR which is connected with 100K resistor through to 0V, as the axe is sleeping mostly and the humidity and temp sensors don't draw much current.

Temp range is normal diurnal, about 19 deg to 4 deg.

Just a thought, batteries are in a box and secured in the dome of a converted solar garden light. The whole thing is out of direct sun so can't get overheated. The board however is indirectly exposed to the air and I haven't protected it yet. Side question - how do I do this, spray laquer over everything or remove the chips first?

I suppose also it could be a loose connection between the screw connector on the board and the battery box lead.

I could change the software to transmit the ADC reading to see what's happening.
 

BeanieBots

Moderator
Aha! You cannot measure battery voltage like that. The adc will return a reading of close to maximum irrespective of what the battery voltage is. This is because the adc reference is also connected to the power supply. Assuming you are using readadc (readadc10 has the same issue but the numbers are different), you will get a number back that is N 256th's of the suppply rail. Thus, with a supply rail of 5v and a signal of 2.5v you get half (2.5/5) of the maximum (256) = 128. If you decrease the power rail voltage and keep the signal the same, the reading will go up. If you decrease the signal and keep the power voltage the same, the reading will go down. If you change the signal AND the power voltage by the same ratio, the reading will remain the same.
To measure the PICAXE's own power rail voltage, you must generate your own reference voltage to compare it against. A very simple and cheap method is to use the 0.6v voltage drop of a diode or even the volt drop of an LED. (there are plenty of precision 2.5v references available but too expensive and overkill for what you need).
Make a potential divider circuit looking at your power rail but replace one of the resistors with a diode (pointy end towards 0v). It does not matter which resistor you replace but the effect will be very different. If you replace the bottom resistor, the adc will see a constant 0.6v. For a 5v rail, you get a reading of (0.6/5)*256 = 30. At 2.5v this will become (0.6/2.5)*256 = 61. If you replace the top resistor, you will get ((5-0.6)/5)*256 = 225. At 2.5v this will become ((2.5-0.6)/2.5)*256 = 194.

Not explained very well but I hope it was clear enough to make sense.
 

Michael 2727

Senior Member
Use better battery terminals, eg, gold plated.
If the unit is out in the weather, heat changes may be causing the batteries to
expand contract enough to upset poor contacting terminals.
 

Kiwi Bruv

Member
Beaniebots.

I used a 1.8M resistor (because I want to keep the power consumption down) and a signal diode. I'm getting 0.275 V at the junction of the res and diode, but the AD is seeing nothing.

Software is the same and it was reading before with 4.5 V connected straight. Is the res too high. Should I change one or both?
 

Kiwi Bruv

Member
Beanie Bots. Iused a 1.8M resistor and a signal diode. This is giving me 0.275V at the ADC pin but the variable assigned is reading 0. I haven't changed the software and it was working before reading 250 (4.9V)

Is the res too high? I used it because I don't want to run the battery down by monitoring it!

Steve
 

BeanieBots

Moderator
1.8M is much too high for a PICAXE input.
The 'book' says that an analog input should 'see' an impedance of less than 2k5. In practice, you can get away with much higher than that but accuracy will be compramised.
Let me know if this is still unclear because it very important that you understand how the ADC and power rail voltage are linked together. An ADC normally has a reference voltage that it compares to the voltage it is measuring. In the case of a PICAXE, the reference voltage is the power rail. Thus, if the power rail is halved, the voltage measured will be seen as having doubled. Do you see why it CANNOT be used to measure its own supply without doing something clever such as a diode?
In your application I would start with a value of around 47k. That will draw less than 0.1mA.
So why is the diode essential?
The PICAXE analog input is NOT a 0v to 5v input. It is a 0v to supply rail input. If you used say a two resistor divider where both resistors where the same value, it would ALWAYS read 128 (half supply rail) no matter what the supply voltage was. With no divider at all (direct connection to supply rail) it will ALWAYS read close to full scale, namely 255. By including a diode in the divider, it makes it non-linear. When it is the lower part of the divider, the voltage across the diode remains almost constant at around 0.6v no matter what voltage the PICAXE is powered with. Thus the adc sees a different RATIO of voltages between its reference (the power rail) and its input (the voltage across the diode).
Read my earlier post again and go through the sums a few times and it might suddenly become clear. The key thing (and I'll repeat it again) is that the analog inputs are NOT 0v to 5v, they are 0v to Supply Rail. Thus, it is impossible to read to its own power rail either directly or via a simple resistor divider.
The advantage this has is that if a PICAXE is used to measure a potiometer, it will give an accurate indication of the position of the pot no matter what the supply voltage. The disadvantage is that when trying to use a PICAXE as a voltmeter, the power supply voltage plays a large part in the reading that you get and must therefore be regulated to better than the voltage you are trying to measure.

Edited by - beaniebots on 9/18/2005 5:43:12 PM
 

Kiwi Bruv

Member
Thanks Beanie bots. I now understand why a simple pot div won't work.

I was thinking about another application where 12V is reduced to 4V in a potential div in order to read the battery voltage.

This works because the ADC supply V is always 5V but the divider voltage can change, whereas in the other scenario the ADC supply V and the measured value will change by the same amount.

I understand now that these are different situations.

Thanks heaps.
 

BeanieBots

Moderator
Try using different diodes in the divider and/or LEDs. Anything that will give a non-linear response to voltage changes. The unfortunate side effect is that diode junctions also make quite good temperature sensors, particularly when driven at very low currents. Try a few variations out and try in each position of the divider until you get an acceptable response. The exact response will be dependant on each diode so it is difficult to give a precise circuit/formula but if you assume a constant 0.5v for a diode or 1.2v for an LED it will get you close. Use debug or similar to then determine the actual values returned for a range of battery voltages.

Edited by - beaniebots on 9/19/2005 6:51:20 AM
 
Top