Chapter 7
Battery and Battery Charge
Meter with Arduino
In this chapter, I will explain how we can make a battery and battery charge meter. We will do it through the assembly of a circuit with Arduino.
It is usually very typical that we have at home different electrical devices that use batteries. Sometimes, we doubt whether the device is broken or the batteries have run out.
Thanks to this project, this will no longer be a problem. The battery and battery charge meter will give you the solution.
What will you learn in this chapter?
-
The objective of the battery and battery charge meter with the Arduino board
-
Arduino components that we will use
-
Riding the circuit with Arduino
-
Programming the battery and battery charge meter with Arduino
-
Arduino native code
The Objective of the Battery and Battery Charge Meter with the Arduino Board
We will use Arduino to read the voltage supplied by a battery through an analog input. Depending on this voltage, we will light an LED of one color. If the battery is new, a green LED will light. If the battery is not new, but part of its energy has been consumed, we will light a yellow LED. Finally, if the battery is worn out or does not supply enough voltage, we will turn on a red LED.
We must be very careful with the type of battery and battery we are going to measure. It is very dangerous to supply more than 5V to the analog pins of Arduino. If what we want is to measure batteries, the most typical, we can only do it with AA, AAA, C, and D batteries. They are the ones used in television controls, toys, and even to feed Arduino.
Much eye with a 9V battery, the squares. As I said before, these batteries far exceed the 5V limit. We must also be careful with the batteries as it will depend on the voltage they supply. Check before connecting that it really is less than or equal to 5V.
Measuring more than 5V for some analog input can damage the board.
Arduino Components that We Will Use
Let's see what kind of components we need for this circuit.
-
Arduino UNO or any Arduino board
-
Protoboard where we will connect the components
-
Cables for connection between components and board
-
3 220 Ω resistors
-
1 10 kΩ resistor
-
1 red 5mm LED
-
1 yellow 5 mm LED
-
1 5mm green LED
As you can see, it is a very simple circuit. With three LEDs and four resistors, it is enough to build a battery or battery charge meter with Arduino.
Riding the Circuit with Arduino
Once all the information is collected, we go to the assembly. In the following image, I show you how you should connect the different components. Pay attention, especially in the resistances.
Let's see how the components have been connected. The first is the
LEDs. Each one is connected in series with a resistance of 220 Ω to extend their useful life. The green LED is connected to pin 2, the yellow LED is connected to pin 3, and the red LED is connected to pin 4. This is important to remember when we see the programming.
To measure the battery, have placed a resistance pull-down. What this type of resistance does is maintain a low logical state, that is, at 0V. It is important to use this type of resistor since, and when we do not have the battery or battery connected to measure, we have an undetermined state at the analog pin input, which causes it to oscillate and maybe until some LED is lit. You can try to remove this resistance, and you will see the result.
The positive pole of the battery is connected to the pull-down resistor and to the analog input A0. The other end of the earth's resistance. Finally, the negative pole of the battery must be connected to the Arduino earth.
Beware of reversing polarities that is, positive with negative or vice versa.
It is more than advisable that all components have the same reference to ground. All must be connected to the same Arduino GND pin.
Programming the Battery and Battery Charge Meter with Arduino
Now play the logic and programming part. The first thing to do is to raise the problem or algorithm we want to achieve. Once we are clear, we can start programming. As I always say, an algorithm is a sequence of ordered steps that we must follow to achieve an objective. In this case, our goal is to measure the charge of a battery or battery with Arduino.
Algorithm
In this section, I will detail the steps that we must follow without writing a line of code; we will do that later when we are clear about
what we have to do.
-
Read the analog pin where the battery is connected
-
We calculate the voltage for the value you have given us
-
We evaluate the voltage
a)
If it is greater than or equal to the maximum threshold
-
We turn the green LED
b)
If it is less than the maximum threshold and greater than the average threshold
-
We light a yellow LED
c)
If it is less than the average threshold and greater than the minimum threshold
-
We turn on the red LED
d)
The rest of the cases
-
Does not light any LED
-
We turn off all LEDs
Analyzing the algorithm that we are going to implement, we conclude that we are going to use three thresholds:
-
The maximum threshold: will indicate that the battery is fully charged.
-
Average threshold: from this threshold to the maximum threshold, the battery has been used but still has power.
-
Minimum threshold: from this threshold to the average threshold, the battery does not supply enough power. Below this threshold, we interpret that there is no battery connected.
Arduino Native Code
We are going to see the code of the battery charge meter with Arduino in parts.
Variables and Constants
We will use the constants to store the pins where we will connect the
LEDs and the analog pin where we will connect the battery. The thresholds will be variable, although we could also use constants. Finally, we will declare three variables to store the value returned by the analog pin, the voltage on that pin, and the waiting time for the LEDs to flash.
We declare the constants with the pins where we connect the LEDs and the analog pin.
// Pins for the LEDs
#define LEDGREEN 2
#define LEDYELLOW 3
#define LEDRED 4
#define ANALOGBATTERY 0
The next thing is to declare the variables and thresholds. The latter will depend on the type of battery. In this case, I am going to do the tests with an AA battery
// Variables
int analogValue = 0;
float voltage = 0;
int ledDelay = 800;
// Thresholds
maximum float = 1.6;
medium float = 1.4;
minimum float = 0.3;
Setup Function
In the setup function, we initialize the serial monitor and put the LED pins in output mode.
void setup () {
// We start the serial monitor
Serial.begin (9600)
;
// LED pins in output mode
pinMode (GREENLED, OUTPUT);
pinMode (YELLOWLED, OUTPUT);
pinMode (REDLED, OUTPUT);
}
Loop Function
We begin the loop function that will be repeated continuously. The first thing is to read the analog pin and store it in the variable Analog Value.
void loop () {
// We read analog input value
analogValue = analogRead (ANALOGBATTERY);
We calculate the voltage. It is a simple rule of 3. If 5V is 1024, with five dividing by 1024 and multiplying it by the value given by the analog pin, we already have the voltage. As simple as that. To verify that everything is fine, we show it by the serial monitor.
// We get the voltage
voltage = 0.0048 * analogValue;
Serial.print ("Voltage:");
Serial.println (voltage);
In the next part we will decide which LEDs we should turn on. We do this through the conditional structures if.
// Depending on the voltage we show an LED or another
if (voltage> = maximum)
{
digitalWrite (GREENLED, HIGH);
delay (ledDelay);
digitalWrite (GREENLED, LOW);
}
else if (voltage <maximum && voltage> medium
)
{
digitalWrite (YELLOWLED, HIGH);
delay (ledDelay);
digitalWrite (YELLOWLED, LOW);
}
else if (voltage <medium && voltage> minimum)
{
digitalWrite (REDLED, HIGH);
delay (ledDelay);
digitalWrite (REDLED, LOW);
}
Finally, we turn off all the LEDs. Thus we begin in the next iteration with the initial state all turned off. As we are talking about very little time, microseconds, this will not affect the operation.
// We turn off all LEDs
digitalWrite (GREENLED, LOW);
digitalWrite (YELLOWLED, LOW);
digitalWrite (REDLED, LOW);
}
Full Code
Then I leave the complete code, so you do not have to copy part by part. It is not very good practice to copy and paste without reading the above. If you really want to learn, follow all the steps, and try to write the code yourself. Only then will you learn to program with Arduino?
// Pins for the LEDs
#define LED GREEN 2
#define LED YELLOW 3
#define LED RED 4
#define ANALOGBATTERY 0
// Variables
int analogValue = 0;
float voltage = 0;
int ledDelay = 800;
// Thresholds
maximum float = 1.6;
medium float = 1.4;
minimum float = 0.3;
void setup () {
// We start the serial monitor
Serial.begin (9600);
// LED pins in output mode
pinMode (GREENLED, OUTPUT);
pinMode (YELLOWLED, OUTPUT);
pinMode (REDLED, OUTPUT);
}
void loop () {
// We read analog input value
analogValue = analogRead (ANALOGBATTERY);
// We get the voltage
voltage = 0.0048 * analogValue;
Serial.print ("Voltage:");
Serial.println (voltage);
// Depending on the voltage we show an LED or another
if (voltage> = maximum)
{
digitalWrite (GREEN LED, HIGH);
delay (ledDelay);
digitalWrite (GREEN LED, LOW)
;
}
else if (voltage <maximum && voltage> medium)
{
digitalWrite (YELLOWLED, HIGH);
delay (ledDelay);
digitalWrite (YELLOWLED, LOW);
}
else if (voltage <medium && voltage> minimum)
{
digitalWrite (REDLED, HIGH);
delay (ledDelay);
digitalWrite (REDLED, LOW);
}
// We turn off all LEDs
digitalWrite (GREENLED, LOW);
digitalWrite (YELLOWLED, LOW);
digitalWrite (REDLED, LOW);
}