arduino battery tester behaving oddly - Page 1
Forum section: electronics / beginners

author: newtothis
author_rank: Contributor
timestamp: February 15, 2015, 11:46:46 pm
content: 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 pagehttp://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 directonThanks in advanceCode:// 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.{digitalWrite(oldLED, HIGH);delay(ledDelay);digitalWrite(oldLED, LOW);}else if ( voltage <= 0.8 ){digitalWrite(offLED, HIGH);delay(ledDelay);digitalWrite(offLED, LOW);}}

author: Psi
author_rank: Super Contributor
timestamp: February 15, 2015, 11:57:05 pm
content: The pin is probably floating and picking up noise in the air when no battery is connectedAdd a 1meg resistor to ground on the ADC input. This will keep the input at zero when no battery is connected

author: Seekonk
author_rank: Super Contributor
timestamp: February 16, 2015, 09:36:58 am
content: 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.

author: Psi
author_rank: Super Contributor
timestamp: February 16, 2015, 10:21:00 am
content: 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.

author: newtothis
author_rank: Contributor
timestamp: February 16, 2015, 11:47:53 am
content: 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 youbut 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.

author: Zero999
author_rank: Super Contributor
timestamp: February 16, 2015, 04:40:16 pm
content: 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.

author: Seekonk
author_rank: Super Contributor
timestamp: February 16, 2015, 04:57:54 pm
content: 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.

author: jamesd168
author_rank: Contributor
timestamp: February 16, 2015, 09:05:08 pm
content: Quote from: Seekonk on February 16, 2015, 04:57:54 pminteresting to know that.

author: Psi
author_rank: Super Contributor
timestamp: February 16, 2015, 11:31:57 pm
content: I you want to add a cap with the 1meg, use a 1-10nfIt will discharge pretty fast. Like 100ms

author: dannyf
author_rank: Super Contributor
timestamp: February 17, 2015, 01:09:51 am
content: QuoteThat'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.