Voltage Divider to Read 9v Battery Voltage and blink led.

I want to read the battery voltage, and show how much the voltage is in the battery, by blinking
led on pin 13. So what should be the input voltage on pin A7? The Arduino is being run at
3.3v.

So as the Voltage goes down the input on analog pin 7, it will run A simple sketch.
I do know that 9v battery is not 9v when you get it out of the box. So want I don’t know is what voltage should the arduino read off from the voltage divider. What is correct? As of right now I have this.

V in = 9v

R1 = 10k resister
R2 = 4.7k resister

V out = 2.87v

const int ledPin =  13;
 

void setup() {
Serial.begin(9600);
}


void loop() {
int kk = analogRead(A7);
BBTime = (kk/99);


if (BBTime = 5)

for (int t=0; t < 5; t++)
{
  digitalWrite(ledPin, HIGH);
  delay(400);
  digitalWrite(ledPin, LOW);
  delay(400);
 }
 
 
 if (BBTime = 3)
 for (int t=0; t < 5; t++)
{
  digitalWrite(ledPin, HIGH);
  delay(4000);
  digitalWrite(ledPin, LOW);
  delay(4000);
 }
}

Votage D.pdf (6.93 KB)

Your voltage divider looks good.

The potential problem I see is that BBtime has to exactly equal 5 or 3, or else nothing happens. You might want to look for a range.

You may also want to test your circuit with a potentiometer and you might want to send some of those values out to the serial monitor, so you can see them (during the testing, debugging, and troubleshooting phase.)

P.S. if (BBTime = 5)

should be, if (BBTime == 5)

The 10k + 4k7 will drain the battery. Use at least 100k and 47k (maybe even higher). You can add a capacitor of 1nF to 100nF parallel to R2 to reduce ADC trouble for the Arduino.

When I look to my right, there is one of my projects with 270k for R1. It reads the voltage with 10mV accuracy. But I have also averaging in the sketch. I think R1 can even be higher.

I would use a float and calculate the actual voltage for the A7 pin and the battery.

int kk = analogRead(A7);
float voltageA7 = (float) kk / 1024.0 * 3.3;
float batteryVoltage = 1.0 / (4700.0 / (4700.0 + 10000.0)) * voltageA7;

if( batteryVoltage < 8.0)
{
  // battery is low
}
else if (batteryVoltage < 7.5)
{
  // battery is empty.
}

Peter_n: The 10k + 4k7 will drain the battery.

If it's permanently connected.

10k/4k7 won’t matter for a temp project.
It only drains 0.6mA. 1% of the power consumption of an avarage Arduino.
But it could be important if the Arduino runs 24/7 from a battery, with sleep modes etc.
This guy did a test with very high value resistor (2x 10Megohm).
measuring-the-battery-without-draining-it
Leo…

BenBenBen: if (BBTime = 5)

http://www.baldengineer.com/arduino-assignments-do-not-equal-comparisons.html

You'd probably get better blinking results if did the LED on/off with delay each time through the loop(). You could constantly update the value of delay based on the current analog reading, using Peter's code.

int delayTime=100;

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(delayTime);
  digitalWrite(ledPin, LOW);  
  delay(delayTime);

  // analogRead
 // maths
 // Peter's if statements, setting the appropriate value of delayTime
}

You might add some code to calculate the battery voltage.
For vcc = 5V
2.87 V/(5/1024)=2.87/0.0048828125 = 587.776
587.776*0.0048828125 * (9/2.87)=9

analogRead *0.015311955 = battery voltage
Let VanalogRead = 2.87
analogRead = 587.776
battery voltage = 587.776 * 0.015311955 = 8.999999

First off, thank you for all your help. I was hoping to just read the batter voltage in the (void setup(){)
So is there some way that I could read the pin (A7) and then turn it off. Read it one time (battery voltage)
and turn if off so it will save on the battery.

int kk = analogRead(A7);

void setup() { 


float voltageA7 = (float) kk / 1024.0 * 3.3;
float batteryVoltage = 1.0 / (4700.0 / (4700.0 + 10000.0)) * voltageA7;

if( batteryVoltage < 8.0)
{
  // battery is low
}
else if (batteryVoltage < 7.5)
{
  // battery is empty.
}
 

void loop() {

BenBenBen: So is there some way that I could read the pin (A7) and then turn it off.

You'd probably want to add a transistor to the voltage divider circuit, and then use another pin to turn the transistor on/off.

Removed image, after Wawa pointed out my mistake.

Yes, but not this way. When the transistor is turned off, current still flows through the 100k resistor into the Arduino pin. The lower part of the resistor would be about 5.5volt. 5volt Arduino supply plus ~0.5v pin protection diode. Anything over 10mA could kill the input.

If current was THAT important, I would stick to a 10meg/4.7meg voltage divider with a 100n cap to ground. That would use the least power. Maybe less than mosfet high side switching (mosfets leak). And be accurate enough. Leo..

An analog switch or small relay could connect and disconnect the voltage divider. If you use the N.O. contact , the analog pin would only be connected while the relay is energized . If you use a DPDT relay or two small SPST relays, on set of contacts could connect the divider to the battery and the other isolated set could connect the divider output to the analog pin. Turn on the relay, take the reading, turn it off.

A small 50mA relay would use ~80,000x the power of a 10meg/4.7meg divider. Even if you would energize the relay for one second, it would use the same power as the voltage divider would use in one day. Leo..

You got me. Do I have to sit down now ? ;D (Duh, why didn't I think of that?) (I guess that's why you get the big bucks.. ;D )

So is this the correct way to wire up the schematic?

Pic in Attachments…

Yes , but , the resistor values are 100 M ohm and 47 M ohm. A mohm is 1/1000th of 1 ohm. I’m pretty sure
you don’t want that across your 9V battery. (It would get very hot and could catch fire).

A7out = Vin * (R2/(R1+R2) = 9*(47/(47+100)= 2.87 V (as stated in your original post)

BenBenBen: So is this the correct way to wire up the schematic?

m = milli M = Mega

In engineering, details matter. Usually.

R1 = 10 Megaohms R2 = 4.7 Megaohms

Just to recap.
10Megohms is about the largest value the electronics corner store stocks, and the maximum practical value regarding the Arduino’s 100megohm? input (leak) resistance.

You need about half that value (~4.7Megohm) to ground to measure a 9volt battery with a 3.3volt Arduino.
Then you have a 1:2 ratio. 1/3 of the battery voltage on the analogue in.
That 4.7Megohm resistor could also be two 10Megohm resistors in parallel.

For 5volt Arduinos, you use TWO 10Megohm resistors. A 1:1 ratio.
Then you will have half of the battery voltage on the analogue input.

Don’t forget the 100n capacitor to ground. Otherwise these high values will give trouble.
I would use a 100n/100volt MKT capacitor instead of a 50volt ceramic.

Beware. Those resistors WILL drain the battery in uhhhhhhhh 50 years.
So no problem if you use 1Meg/470Kohm or 1Meg/1Meg instead.
Leo…

Beware. Those resistors WILL drain the battery in uhhhhhhhh 50 years.

Ok ok, no need to gloat... ;D