IotIntermediate

Bluetooth Battery Charger Cutoff System

Develop a battery charging control system with Bluetooth monitoring and automatic charging cutoff.

Technologies Used

ArduinoBluetooth ModuleBatteryRelay ModuleVoltage Sensor

Bluetooth-Based Automatic Battery Charger Cutoff System

1. Introduction

This project uses an Arduino and a Bluetooth serial module to control a battery charger automatically.

A computer or another Bluetooth-connected device sends the battery percentage to the Arduino. Based on the received percentage, the Arduino switches a relay ON or OFF.

The program uses two battery limits:

  • Below 30%: Charger ON.
  • At or above 90%: Charger OFF.
  • From 30% to below 90%: Keep the previous relay state.

If Bluetooth communication stops for more than one minute after it has started, the program switches the charger OFF.

Important: This code does not measure battery voltage or percentage itself. It only receives a percentage value over Bluetooth. The sending device must calculate or obtain the battery percentage and transmit it as text followed by a newline.

2. Objectives

  • Receive battery percentage through Bluetooth serial communication.
  • Automatically turn the charger ON when the percentage is below 30%.
  • Turn the charger OFF when the percentage reaches 90%.
  • Keep the previous charger state between the two limits.
  • Switch the charger OFF if Bluetooth data is not received for 60 seconds after communication has started.

3. Required Components

ComponentQuantityPurpose
Arduino Uno or compatible board1Main controller
Bluetooth serial module (HC-05/HC-06)1Receives battery percentage
Relay module1Controls charger power
Jumper wiresAs neededConnections
Battery charger and battery1 eachCharging setup
Computer or sending device1Sends battery percentage

4. Pin Connections

4.1 Bluetooth Module to Arduino

Bluetooth ModuleArduino
TXDDigital Pin 2 (SoftwareSerial RX)
RXDDigital Pin 3 (SoftwareSerial TX)
GNDGND
VCCSuitable supply for the module

The code creates:

SoftwareSerial BTserial(2, 3);

Here:

  • Pin 2 is the Arduino RX pin.
  • Pin 3 is the Arduino TX pin.

Therefore, connect:

  • Bluetooth TXD to Arduino pin 2.
  • Bluetooth RXD to Arduino pin 3.

Voltage Warning: Many Bluetooth modules use 3.3V logic on RXD. Check the exact module specifications. If its RXD pin is not 5V tolerant, use a suitable voltage divider or level shifter between Arduino pin 3 and Bluetooth RXD.

4.2 Relay Module to Arduino

Relay ModuleArduino
INDigital Pin 13
VCCAs specified by relay module
GNDGND

The relay contacts are placed in the charger circuit according to the relay module's wiring diagram and the charger's electrical requirements.

5. Important Program Settings

Constant / VariableValueMeaning
BT_RECEIVE_TIMEOUT60000 msBluetooth data timeout (60 seconds)
BAUD9600Serial communication speed
relay13Relay control pin
MIN_CUT_OFF_BATTERY_PCT30Below this value, charger switches ON
MAX_CUT_OFF_BATTERY_PCT90At or above this value, charger switches OFF
startedfalseIndicates whether Bluetooth data has been received
lastreceivedTimeInMili0Time when the most recent message was received
relayStateLOWInitial relay output state

6. Complete Arduino Code

#include <SoftwareSerial.h> #define BT_RECEIVE_TIMEOUT 60000 #define BAUD 9600 #define relay 13 #define MIN_CUT_OFF_BATTERY_PCT 30 #define MAX_CUT_OFF_BATTERY_PCT 90 String receivedString; SoftwareSerial BTserial(2, 3); // RX | TX bool started = false; long lastreceivedTimeInMili = 0; bool relayState = LOW; void setup() { BTserial.begin(BAUD); pinMode(relay, OUTPUT); digitalWrite(relay, relayState); } void loop() { receivedString = ""; if (BTserial.available() > 0) { started = true; receivedString = BTserial.readStringUntil('\n'); lastreceivedTimeInMili = millis(); } // Keep charger on until the first Bluetooth message is received. if (!started) { digitalWrite(relay, LOW); return; } // Turn charger off if no Bluetooth data arrives for 60 seconds. if ((millis() - lastreceivedTimeInMili) > BT_RECEIVE_TIMEOUT) { digitalWrite(relay, HIGH); return; } if (receivedString != "") { int batteryPercentage = receivedString.toInt(); if (batteryPercentage >= MAX_CUT_OFF_BATTERY_PCT) { relayState = HIGH; } else if (batteryPercentage < MIN_CUT_OFF_BATTERY_PCT) { relayState = LOW; } digitalWrite(relay, relayState); } }

7. Code Explanation

7.1 Include the SoftwareSerial Library

#include <SoftwareSerial.h>

This library allows the Arduino to communicate with a Bluetooth module using ordinary digital pins instead of the board's main hardware serial connection.

7.2 Define the Settings

#define BT_RECEIVE_TIMEOUT 60000 #define BAUD 9600 #define relay 13 #define MIN_CUT_OFF_BATTERY_PCT 30 #define MAX_CUT_OFF_BATTERY_PCT 90

These definitions set:

  • Bluetooth communication timeout.
  • Serial communication speed.
  • Relay control pin.
  • Minimum battery percentage limit.
  • Maximum battery percentage limit.

60000 milliseconds equals 60 seconds.

7.3 Create the Bluetooth Serial Connection

SoftwareSerial BTserial(2, 3);

The first pin is the Arduino RX pin (2), and the second is the Arduino TX pin (3).

The Bluetooth module's TXD connects to Arduino pin 2, while its RXD connects to Arduino pin 3.

7.4 Set Up the Arduino

void setup() { BTserial.begin(BAUD); pinMode(relay, OUTPUT); digitalWrite(relay, relayState); }
  • Starts Bluetooth serial communication at 9600 baud.
  • Sets pin 13 as an output.
  • Sets the initial relay output to LOW.

7.5 Read the Bluetooth Message

if (BTserial.available() > 0) { started = true; receivedString = BTserial.readStringUntil('\n'); lastreceivedTimeInMili = millis(); }
  • BTserial.available() checks whether data has arrived.
  • readStringUntil('\n') reads text until a newline character is received.
  • started = true records that Bluetooth communication has begun.
  • millis() records the time since the Arduino started running.

The sending device should transmit messages such as:

45 90

Each message must end with a newline character.

7.6 Initial Condition

if (!started) { digitalWrite(relay, LOW); return; }

Before the first Bluetooth message arrives, the program sets the relay output to LOW and exits the current loop iteration.

In this program's intended relay logic, LOW means the charger is ON.

7.7 Bluetooth Timeout

if ((millis() - lastreceivedTimeInMili) > BT_RECEIVE_TIMEOUT) { digitalWrite(relay, HIGH); return; }

If more than 60 seconds pass without a new Bluetooth message, the program sets the relay output to HIGH and exits the loop.

This is intended to turn the charger OFF if the computer sleeps or the Bluetooth connection stops sending data.

7.8 Convert the Received Value

int batteryPercentage = receivedString.toInt();

This converts the received text into an integer.

For example:

  • Text "75" becomes integer 75.
  • Text "90" becomes integer 90.

7.9 Apply the Battery Limits

if (batteryPercentage >= MAX_CUT_OFF_BATTERY_PCT) { relayState = HIGH; } else if (batteryPercentage < MIN_CUT_OFF_BATTERY_PCT) { relayState = LOW; }

The program applies these rules:

  • 90% or higher: Set relay output to HIGH (charger intended OFF).
  • Below 30%: Set relay output to LOW (charger intended ON).
  • 30% to below 90%: Keep the previous relay state.

This behavior is called hysteresis. It helps prevent the charger from rapidly switching ON and OFF around one threshold.

7.10 Update the Relay

digitalWrite(relay, relayState);

This writes the stored relay state to Arduino digital pin 13.

8. Working Principle

  1. The Arduino starts and initializes Bluetooth communication.
  2. The relay output is initially set to LOW, which this program intends to mean charger ON.
  3. A computer or other device sends the battery percentage through Bluetooth.
  4. The Arduino reads the percentage as text and converts it into an integer.
  5. If the percentage is below 30%, the Arduino sets the relay output LOW.
  6. If the percentage is 90% or higher, it sets the relay output HIGH.
  7. Between 30% and below 90%, it keeps the previous relay state.
  8. If no new Bluetooth message arrives for more than 60 seconds after communication has started, it sets the relay output HIGH.

9. Example Operation

Received PercentageRelay OutputIntended Charger Action
No Bluetooth message received yetLOWON
20%LOWON
29%LOWON
30%Previous stateNo change
55%Previous stateNo change
89%Previous stateNo change
90%HIGHOFF
95%HIGHOFF
No message for over 60 seconds after communication beganHIGHOFF

Note: The result in the 30%–89% range depends on the state previously set by a low or high reading.

10. Data Format Sent Over Bluetooth

The sending device should transmit a numeric percentage followed by a newline character.

Examples

25 65 90

Each value must end with \n (newline), because the Arduino uses:

readStringUntil('\n');

11. Important Improvements and Safety Notes

  1. Verify Relay Polarity: The program assumes LOW turns the charger ON and HIGH turns it OFF. Relay modules differ; some are active-LOW and others are active-HIGH. Test the module safely and adjust the logic if necessary.

  2. Validate Incoming Data: String.toInt() returns 0 when the text does not begin with a valid number. In this program, malformed text could therefore be interpreted as 0% and turn the charger ON. A safer version should reject messages that are not valid integers from 0 to 100.

  3. Check the Timeout Design: Before the first Bluetooth message, the code keeps the charger ON indefinitely. The 60-second timeout only becomes active after the first message. Consider whether this is the safe startup behavior for your battery and charger.

  4. Keep the Timeout State Consistent: On timeout, the code writes HIGH directly but does not update relayState. If valid Bluetooth data arrives again, the stored state may be written back. A safer implementation should explicitly update relayState when timing out and define how communication recovery should work.

  5. Use a Proper Battery Management System: Do not rely on this Arduino program as the only protection against overcharging, over-discharging, overheating or battery faults. Use a suitable charger and battery protection/BMS designed for the battery chemistry.

  6. Electrical Safety: Mains voltage can cause fatal electric shock or fire. Use a properly rated, enclosed relay module and suitable isolation, fusing and wiring. Have mains-side connections designed and checked by a qualified person. Never handle exposed live wiring.

  7. Verify the Battery Percentage Source: Do not assume the computer's battery percentage is the battery being charged. Confirm that the sending software is reporting the correct battery and that its readings are reliable.

12. Advantages

  • Automatic charger control based on received battery percentage.
  • Wireless communication with a Bluetooth serial module.
  • Two thresholds reduce unnecessary relay switching.
  • Timeout behavior can switch the charger OFF when communication stops.
  • Uses a simple Arduino program and a relay module.

13. Limitations

  • The Arduino does not measure battery percentage directly.
  • The system depends on the sending device and Bluetooth connection.
  • The initial state keeps the charger ON until the first message arrives.
  • Invalid input is not safely validated in the original code.
  • The program is not a replacement for a battery-management or charging-protection circuit.

14. Conclusion

The Bluetooth-Based Automatic Battery Charger Cut-Off System receives a battery percentage from another device and controls a charger through a relay.

It switches the charger ON below 30%, switches it OFF at 90% or above, and retains the previous state between those limits. A 60-second communication timeout is also included.

For real battery charging, input validation, correct relay polarity, a suitable BMS and electrical safety protections are essential.

15. Revision Questions

  1. What is the purpose of the SoftwareSerial library?
  2. Which Arduino pins are used for Bluetooth communication?
  3. What does BT_RECEIVE_TIMEOUT represent?
  4. At what battery percentage does the program switch the charger OFF?
  5. What happens when the battery percentage is between 30% and 90%?
  6. Why does the program use readStringUntil('\n')?
  7. What is the purpose of millis()?
  8. Why should incoming Bluetooth data be validated?
  9. What is hysteresis, and how does it help this project?
  10. Why is a battery-management system still necessary?