Arduino BQ24295 battery charger library (DIY generator part 14)

This post is going to be a short one about my Arduino battery charger (based on BQ24295 IC) library. As it was written earlier, the portable DIY generator has a battery charging circuit. Its main component – BQ24295 IC is not a simple Li-Ion/Li-Po charger. Its working parameters or status readings can be transferred through I2C from/to an external host MCU. So, to make this charger easily usable – a library was written.

Links to project’s all posts

  1. VCA822 Gain Amplifier Circuit
  2. LM7171 Offset Circuit
  3. Gain and Offset Control Filter Circuit
  4. Dual 5V Power Supply
  5. Dual 12V TPS65131 Power Supply
  6. Battery Charging Circuit with BQ24295
  7. Basic WEB Interface
  8. IPS Capacitive LCD on an ESP32
  9. IPS LCD, ESP32 with eSPI library and Touch screen
  10. Final PCB Design for the DIY Waveform Generator
  11. Custom Design PCBs and How To Get Them Manufactured
  12. Soldering the PCB
  13. AD9833 Library and Further Output Noise Reduction
  14. Arduino BQ24295 Battery Charger Library (this post)
  15. LCD GUI with LVGL on ESP-32
  16. 3D Printed Enclosure
  17. Finished DIY generator

The Charging IC

BQ24295 has hefty number of configurable parameters. It has total 11 eight-bit registers, three of them are read only with fault, status and part number/revision information. Others are read/write registers with settings such as pre-charge, termination and normal charging current values, max battery voltage, output (system) voltage configuration, interrupt settings etc.

The Library

The library you can find on GitHub. The main idea was to make all read/write registers write only from user perspective. What I mean is that those registers or their parts can be easily written by using the library, but they don’t have the same extensive list of functions to read values from them. There is one function getRegster which reads all 8 bits from a register and it is up to the user to know from which register he need to read a value, check if that value is correct and/or do additional logic with that information.

Those registers which are read-only, has specific functions for reading full 8-bit values or only its part. The logic behind it is that writable registers keep parameters which user writes himself and knows what those values are, while read-only registers keep dynamic information (faults, statuses) which needs to be easily readable at any time.

This library also uses Wire standard Arduino library to allow host-device communication through I2C port.


Here is a simple usage example with IC’s initialization, setup and status/fault reads:

#include <Arduino.h>
#include "bq24295.h"

void setup(){
  BQ24295_IC charger(15, 0, 100000); // creates charger object and initializes I2C interface
                                     // with SDA pin = 15, SCK = 0 and clock = 100 kHz

  charger.setFastChargeCurrent(0b101000); // sets battery charging current;
  charger.setPreChargeCurrent(0b0010); // sets charging current which is used to 
                                       // pre-charge an empty battery;

  charger.setFastChargeTimer(0); // sets fats charger timer to 5hrs;
  charger.setBoostVoltage(0b1000); // sets boost voltage;
  charger.enableOTG(false); // disables OTG;
  int intrev = charger.getRevisionNumber(); // gets chip revision number;

void loop(){}

The library has more functionality that shown in the code above. For more information I would suggest looking through the library files – there are additional comments/documentation on each function inside a header file. Also, BQ24295 datasheet has more details about all the registers and their values/parameters/settings.


BQ24294 Arduino library.

BQ24295 datasheet.

BQ24295 hardware circuit and usage in the DIY generator.

Subscribe to a newsletter!

Was this page helpful?