Debug / Voltage Problem

dolszews

New Member
I am new to PICAXE and have run into a problem using the debug feature with my 08M. To do my testing/programming, I am using the AXE021. I am trying to read the voltage off a Li-ion battery which outputs about 4.2V in its current charged state. To power my 08M I am using an old cell phone charger that outputs a very constant 5.142V. From what I understand, when using the readadc/readadc10 command, the chip scales the bit value from 0=0V and 255 or 1023 equal to the voltage of the power supply, in this case 5.142V. After doing the math, I come out that the chip should read either ~210 bits for 8-bit or ~860 bits for 10-bit. Now starts the problem.

1. After programming the 08M using the PICAXE Programming Editor, the debug screen opens and the variable I assigned for the input value fluctuates wildly from about 50-200 bits when reading in 8-bit, and between about 300-700 bits when reading in 10-bit. This is when the Li-ion is DISCONNECTED. When connected, the value maxes out at 255/1023. This is still with the AXE021 jumper in program mode.

2. When I switch the AXE021 jumper to out mode, I cannot establish a connection with my PC to run in debug mode, so I cannot vouch for the values in this mode.

I cannot tell if my problem is rooted with my inability to debug when the board is in "out" mode, or if it is something else. Any help with this matter will be greatly appreciated. Thanks!
 

Dippy

Moderator
Hi.

(Assuming PCB construction and components are OK...)

1. Try powering the AXE021 board with 3xAA batteries (3 x 1.5V cells) to see if that improves stability.
2. When the pin is disconnected ('floating') you will get strange ADC results. Try connectingyour ADC pin to Gnd to see if ADC results are Zero - if not you've done something wrong.
3. A cell-phone charger is likely to be switched-mode which may be noisy. It may appear as stable with your average DMM but may be quite wobbly if viewed on an oscilloscope.
4. How have you connected your Li-ion battery? Have you connected battery -ve to -ve/Gnd of PICAXE AXE021 board?
5. Post your code and a diagram showing your connections.
 

dolszews

New Member
Thanks for your response.

1. Try powering the AXE021 board with 3xAA batteries (3 x 1.5V cells) to see if that improves stability.
Done. Outputs ~4.7V no problem.

2. When the pin is disconnected ('floating') you will get strange ADC results. Try connectingyour ADC pin to Gnd to see if ADC results are Zero - if not you've done something wrong.
I connected to ADC pin to ground and the debug immediately went to zero.

3. A cell-phone charger is likely to be switched-mode which may be noisy. It may appear as stable with your average DMM but may be quite wobbly if viewed on an oscilloscope.
I did not know that. Very good to use in the future. Thank you.

4. How have you connected your Li-ion battery? Have you connected battery -ve to -ve/Gnd of PICAXE AXE021 board?
Before I answer this, let me say that I found something I may have had wrong . I found this page and things started to make sense. I see how R2 connects the ADC pin to ground. When I added R2=5.6k to my circuit, connected the v+ from the Li-ion to the ADC pin and v- from the Li-ion to the ground of the picaxe circuit, the debug value was only a few digits off from my calculation, which is good enough for me. However, how do you calculate the value of the resistor to use in the place of R2? Is there a way to optimize it, or do you just need a certain amount to resistance between the ADC pin and ground?

Thanks for the help Dippy! Your comment about having the ADC pin floating pointed me in the right direction.
 

BeanieBots

Moderator
If the voltage you are measuring is less than the PICAXE supply voltage, then you do not need R2.
R2 is only required to drop the voltage seen at the ADC pin to below the supply rail.

V_ADC_pin = V_source*R2/(R1+R2).

Also, for reliable readings, the total source impedance must not be greater than about 10k.

Z_source = R1*R2/(R1+R2). (as seen by the ADC pin)
 

Dippy

Moderator
And the PICAXE supply voltage should be rocky steady and dead smooth with decoupling on PICAXE V+/Gnd . It really does make a difference. Remember, the Vsupply is your ADC 'reference'.
 

dolszews

New Member
If the voltage you are measuring is less than the PICAXE supply voltage, then you do not need R2.
R2 is only required to drop the voltage seen at the ADC pin to below the supply rail.

V_ADC_pin = V_source*R2/(R1+R2).

Also, for reliable readings, the total source impedance must not be greater than about 10k.

Z_source = R1*R2/(R1+R2). (as seen by the ADC pin)
Just reviewing this post and I want to clarify a few things. What setup is the correct way of connecting things? A or B?

A)

Sample Voltage
|
R1
|
---------ADC
|
|
|
GND

B)

Sample Voltage
|
|
|
---------ADC
|
R2
|
GND

Currently I have my circuit setup like B and it works fine. However, when reading through the quoted text, BeanieBots implies that setup A is correct? What is the practical difference between the two?
 

Dippy

Moderator
"However, when reading through the quoted text, BeanieBots implies that setup A is correct?"

No he doesn't. What BB is saying is that if the voltage you are measuring is LESS than the PICAXE supply voltage then FORGET (i.e. dump/remove/delete/exterminate/eat/throwaway) R2.

A voltage divider (= potential divider or pot-div) is typically used when the source voltage is too high to be applied to the PICAXE pins. BB kindly provided the little equation for you to do some calcs.

Your diagram A shows the ADC connected directly to ground-zero (=grounded), that would give an ADC result of Zero - which is not very clever/useful/meaningful/sensible.

Your diagram B will obviously work. However, if B is a low value it will act as a pot div to the source impedance (in the loosest sense of the meaning) - thus affecting reading.

Summary.
IF the voltage you are measuring is LESS than the PICAXE supply voltage then you can directly connect it, or if you want a bit of 'safety' then just put a small res in series.
Source ---Res---ADC. Res << Source Imp.

IF the voltage you are measuring is GREATER than the PICAXE supply voltage, or greater than 5V nom. max then you MUST use a voltage/potential divider circuit. The values of R1 and R2 can be calculated based on the highest voltage likely to be applied. Impedance to ADC is another issue for another day...
Have a look at BB's equation he provided for you.

This is pretty basic stuff and should be remembered for future apps.
 
Last edited:

BeanieBots

Moderator
What Dippy says ^^^
You were right to ask. How else will you learn?
However, this is very basic stuff. You really must get to fully understand resistor combinations and how to calculate them if you ever want to process analogue signals.
If in doubt, always go back to the absolute basic Ohm's law. All else stems from that even if not immediately obvious.

You don't even need to remember the equations once you know how/why they work.
Ohm's Law states: V=IR. (that's the only one to remember)
Resistors in series, just add the values.
Resistors in parallel, 1/R = 1/R1 + 1/R2
A little bit of algebra gives R=R1*R2/(R1+R2) which I find easier to work with when not using a calculator.

So, for a potential divider.
We want to know the voltage across the lower resistor (R2).
Hence, we need to know the current flowing through it.
Ohm's Law tells that it is V/(R1+R2)
Voltage is given by V=IR.
So we can easily see that to get the voltage we must multiply the current (which we now have an eqaution for) by R2.
Very quickly we arrive at V_at_junction = V_at_top*R2/(R1+R2)

For the source impedance it's a fraction more tricky.
We need to know the equivalent resistance that the ADC input will 'see'.
When calculating impedances (resistances for DC work), you don't need to worry about any voltages that might be across them. If we assume that the voltage being measured has no output resistance, we can therefore (when we forget about the voltage) assume it is a dead short.
If you draw that out again, you end up with some unknown voltage with two resistors in parallel in series with it.

But we know how to calculate the unknown voltage and we know how to calculate two resistors in parallel. Thus we know how to calculate a potential divider to look like a new voltage with a resistor in series.

This is known as the Thevenin Equivalent. (hope I spelled that right).

The series resistance is often ignored because the PICAXE input impedance is very high and therefore will not produce any further potential divider action. However, above a certain value and other undesirable effects come into play.

For a PICAXE, you should not allow the series impedance to exceed about 10k.

Hence, although a potential divider will give the same (mathematical) result if you use say two 1k resistors or two 1M resistors, the actual result will not be the same because the latter would have too much series impedance for the PICAXE to give reliable results.

Hope that helps rather than hinders but it is important stuff.
 
Top