EEVblog Electronics Community Forum

Electronics => Beginners => Topic started by: newtothis on February 15, 2015, 11:46:46 pm

Title: arduino battery tester behaving oddly
Post by: newtothis on February 15, 2015, 11:46:46 pm
Hi,

this might not be the right forum, but I get a 504 gateway time-out when trying to enter the arduino site so I would try ask you anyway, as I suspect you can help as other arduino question has been solved.

I just received a VISduino - Uno R3, (it was the cheapest on ebay), and I just made a battery tester to try it out.
It does so perfectly when connected to a battery, but when the wires hang Loose unconnected they suddenly shift from no battery to readings suggesting that they are connected to a battery, and after a while they shift back - the shifting seems to be sequentiel so it goes through the stages 0.8>1.4>1.4-1.6>1.6 and vice versa at battery power. at USB power it seems to skip between 1.6 and 0.8 mostly without the inbetween steps.
shorting the  wires directly or adding a resistor caused it to go to OFF LED instantaneously.
 The setup is from this page http://nostarch.s3.amazonaws.com/arduino_project6.pdf (http://nostarch.s3.amazonaws.com/arduino_project6.pdf) - just added a no battery connected LED in the code.
My question is if the code is flawed, Do I have some issue with ground or the board is bad/broken?

Hope some of you can point me in the right directon

Thanks in advance

Code:
// 1.5V battery tester
#define newLED 2 // green LED 'new'
#define okLED 4 // yellow LED 'ok'
#define oldLED 6 // red LED 'old'
#define offLED 8 // white LED 'no battery connected'
int analogValue = 0;
float voltage = 0;
int ledDelay = 1000;
void setup()
{
pinMode(newLED, OUTPUT);
pinMode(okLED, OUTPUT);
pinMode(oldLED, OUTPUT);
pinMode(offLED, OUTPUT);
}
void loop()
{
analogValue = analogRead(0);
voltage = 0.0048*analogValue;
if ( voltage >= 1.6 )
{
digitalWrite(newLED, HIGH);
delay(ledDelay);
digitalWrite(newLED, LOW);
}
else if ( voltage < 1.6 && voltage > 1.4 )
{
digitalWrite(okLED, HIGH);
delay(ledDelay);
digitalWrite(okLED, LOW);
}
else if ( voltage < 1.4 && voltage > 0.8)
{
digitalWrite(oldLED, HIGH);
delay(ledDelay);
digitalWrite(oldLED, LOW);
}
else if ( voltage <= 0.8 )
{
digitalWrite(offLED, HIGH);
delay(ledDelay);
digitalWrite(offLED, LOW);
}
}
Title: Re: arduino battery tester behaving oddly
Post by: Psi on February 15, 2015, 11:57:05 pm
The pin is probably floating and picking up noise in the air when no battery is connected
Add a 1meg resistor to ground on the ADC input. This will keep the input at zero when no battery is connected
Title: Re: arduino battery tester behaving oddly
Post by: Seekonk on February 16, 2015, 09:36:58 am
1M is probably a little high.  They say 10K, but 100K works well in most cases.  A .1 uf capacitor to common will make it more stable and get rid of AC noise.  Something to be aware of is ghosting.  As an experiment look at the value of the channel before and after the one used in the program and find the resistor that corrects this issue.
Title: Re: arduino battery tester behaving oddly
Post by: Psi on February 16, 2015, 10:21:00 am
i wasn't sure what size/type battery he was measuring. Any pulldown resistor will be a load across the battery and will drain it over time. 1Meg is enough to work as a functional pulldown and will draw very little current.
Title: Re: arduino battery tester behaving oddly
Post by: newtothis on February 16, 2015, 11:47:53 am
Hello,

and thank you for the help. It is a 1.5v battery tester.
I tried both a 1M ohm resistor and a 0.1µF ceramic capacitor (104) from the plus to minus rail on the breadboard, giving the current an alternative path. The resistor solves the problem - thank you

but the capacitor stops the erratic behaviour, but causes it to stay at the last LED after the battery is removed - so if the battery is good, then the good LED is kept on after the battery is removed, untill I measure something new but I might have placed it wrong.
Title: Re: arduino battery tester behaving oddly
Post by: Zero999 on February 16, 2015, 04:40:16 pm
If the capacitor is placed across the battery test probes then it's not surprising that this is happening. The capacitor is storing the charge so the voltage is held up, even when the battery is removed. Using a lower value capacitor or resistor should make the time constant so small it isn't noticeable.
Title: Re: arduino battery tester behaving oddly
Post by: Seekonk on February 16, 2015, 04:57:54 pm
That reminds me of another thing I did with the uno.  I was driving a FET directly with an output pin.  I noticed the FET continued on after it should have turned off.  Looked for a logic error and couldn't find one.  I had done a quick software edit and forgot to declare the pin as an output.  Turns out you can momentarily turn a pin into an output, then it reverts to a floating input.  FET gate capacitance  of 100pf is enough to keep it on for a minute or more.
Title: Re: arduino battery tester behaving oddly
Post by: jamesd168 on February 16, 2015, 09:05:08 pm
That remi (http://mastechpowersupply.com/variable-dc-power-supply.html)nds me of another thing I di (http://mastechpowersupply.com/linear-power-supply.html)d with the uno.  I was driving a FET directly with an output pin.  I noticed the FET continued on after it should have turned off.  Looked for a logic error and couldn't find one.  I had done a qui (http://mastechpowersupply.com/switching-power-supply.html)ck software edit and forgot to decl (http://mastechpowersupply.com/variable-dc-power-supply/power-supply-for-charging-lithium-batteries.html)are the pin as an output.  Turns out you can momentarily turn a pin into an output, then it reverts to a floating input.  FET gate capacitance  of 100pf is enough to keep it on for a minute or more.


interesting to know that.
Title: Re: arduino battery tester behaving oddly
Post by: Psi on February 16, 2015, 11:31:57 pm
I you want to add a cap with the 1meg, use a 1-10nf
It will discharge pretty fast. Like 100ms
Title: Re: arduino battery tester behaving oddly
Post by: dannyf on February 17, 2015, 01:09:51 am
Quote
Turns out you can momentarily turn a pin into an output, then it reverts to a floating input.

That's how people emulate OD output on a push-pull pin: output a '0' and turn the pin into output to output a '0'; turn the pin into input to output a '1', with a pull-up resistor.

Handy for software i2c.