SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By g_von
I have a simple project setup to learn how to use the battery babysitter. For the most part, the project works. Here's what I've designed:

Battery baby sitter hooked up to an Arduino Nano, an OLED screen and a tri-color LED.
The battery baby sitter is hooled up to a 1000mAh LiPo battery. The LED is programmed to shine green for 90% to 100% charge, blink blue for 20% to 90% charge, shine yellow for 10% to 20% charge, and shine red for 0% to 10% charge. The OLED display simply displays the % charge so I can roughly gauge when to expect the LED to change status.

Everything works fine except after the battery runs out. When I plug in a USB cable to the battery babysitter to charge the battery again, I noticed that the battery doesn't charge as long as the Battery Babysitter Vout is connected to the Arduino Nano Vin. And also, the project simply doesn't power up will the USB charge cable is connected.

I attached a picture of the setup and the code.
Code: Select all
BQ27441 Library Basic Example
Jim Lindblom @ SparkFun Electronics
May 9, 2016

Demonstrates how to set up the BQ27441 and read state-of-charge (soc),
battery voltage, average current, remaining capacity, average power, and
state-of-health (soh).

After uploading, open up the serial monitor to 115200 baud to view your 
battery's stats.

Hardware Resources:
- Arduino Development Board
- SparkFun Battery Babysitter

Development environment specifics:
Arduino 1.6.7
SparkFun Battery Babysitter v1.0
Arduino Uno (any 'duino should do)
// SDA goes to D2
// SCL goes to D1
#include <SparkFunBQ27441.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

//define pins
#define D0 16
#define D1 5 // I2C Bus SCL (clock)
#define D2 4 // I2C Bus SDA (data)
#define D3 0
#define D4 2 // Same as "LED_BUILTIN", but inverted logic
#define D5 14 // SPI Bus SCK (clock)
#define D6 12 // SPI Bus MISO 
#define D7 13 // SPI Bus MOSI
#define D8 15 // SPI Bus SS (CS)
#define D9 3 // RX0 (Serial console)
#define D10 1 // TX0 (Serial console)

#define OLED_MOSI  2   //D1 on display [2 is D4 on ESP2866]
#define OLED_CLK   0   //D0 on dispale [0 is D3 on ESP2866]
#define OLED_DC    14  //[14 is D5 on ESP2866]
#define OLED_CS    10 // [10 is SD3 on ESP2866]
#define OLED_RESET 16 // [16 is D0 on ESP2866]

// Set BATTERY_CAPACITY to the design capacity of your battery.
const unsigned int BATTERY_CAPACITY = 1000; // e.g. 850mAh battery
int pinD6 = D6; // RED LED
int pinD7 = D7; // GREEN LED
int pinD8 = D8; // BLUE LED
unsigned long timer_prev = 0;
unsigned long timer_curr= 0;
int LEDstate = HIGH;


void setupBQ27441(void)
  // Use lipo.begin() to initialize the BQ27441-G1A and confirm that it's
  // connected and communicating.
  if (!lipo.begin()) // begin() will return true if communication is successful
	// If communication fails, print an error message and loop forever.
    Serial.println("Error: Unable to communicate with BQ27441.");
    Serial.println("  Check wiring and try again.");
    Serial.println("  (Battery must be plugged into Battery Babysitter!)");
    while (1) ;
  Serial.println("Connected to BQ27441!");
  // Uset lipo.setCapacity(BATTERY_CAPACITY) to set the design capacity
  // of your battery.

void printBatteryStats()
  // Read battery stats from the BQ27441-G1A
  unsigned int soc = lipo.soc();  // Read state-of-charge (%)
  unsigned int volts = lipo.voltage(); // Read battery voltage (mV)
  int current = lipo.current(AVG); // Read average current (mA)
  unsigned int fullCapacity = lipo.capacity(FULL); // Read full capacity (mAh)
  unsigned int capacity = lipo.capacity(REMAIN); // Read remaining capacity (mAh)
  int power = lipo.power(); // Read average power draw (mW)
  int health = lipo.soh(); // Read state-of-health (%)

  // Now print out those values:
  String toPrint = String(soc) + "% | ";
  toPrint += String(volts) + " mV | ";
  toPrint += String(current) + " mA | ";
  toPrint += String(capacity) + " / ";
  toPrint += String(fullCapacity) + " mAh | ";
  toPrint += String(power) + " mW | ";
  toPrint += String(health) + "%";
  display.print("Charge %: ");

void setup()
  pinMode(pinD8, OUTPUT); //D6 is RED
  pinMode(pinD6, OUTPUT); //D7 is GREEN
  pinMode(pinD7, OUTPUT); //D8 is BLUE


void loop() 
  unsigned int soc = lipo.soc();

if (soc <= 100 && soc >= 90){
  digitalWrite(pinD7, LOW);
  digitalWrite(pinD6, HIGH);
  digitalWrite(pinD8, HIGH);
else if (soc < 90 && soc >= 20){
  //digitalWrite(pinD8, HIGH);
  digitalWrite(pinD7, HIGH);
  digitalWrite(pinD6, HIGH);
  timer_curr = millis();
  if(timer_curr - timer_prev >= 2000){
      timer_prev = timer_curr;

      if (LEDstate == HIGH){
        LEDstate = LOW;
        LEDstate = HIGH;

      digitalWrite(pinD8, LEDstate);
else if (soc < 20 && soc >= 10){
  digitalWrite(pinD7, LOW);
  digitalWrite(pinD6, LOW);
  digitalWrite(pinD8, HIGH);

else if (soc < 10){
  digitalWrite(pinD7, HIGH);
  digitalWrite(pinD6, LOW);
  digitalWrite(pinD8, HIGH);
By paulvha
Your issue is caused by a lack of power from the USB port. Normally a USB2.0 delivers 500mA, but that can be set different . Are you connected to windows or Linux system?
By g_von
paulvha wrote: Wed Jan 24, 2018 8:07 am Your issue is caused by a lack of power from the USB port. Normally a USB2.0 delivers 500mA, but that can be set different . Are you connected to windows or Linux system?
Hi paulvha,
After months I just noticed that my post was approved!

I'm connected to a Windows Surface Pro. FYI, the battery charges when I disconnect Vout from the BabySitter to the ESP8266.
long long title how many chars? lets see 123 ok more? yes 60

We have created lots of YouTube videos just so you can achieve [...]

Another post test yes yes yes or no, maybe ni? :-/

The best flat phpBB theme around. Period. Fine craftmanship and [...]

Do you need a super MOD? Well here it is. chew on this

All you need is right here. Content tag, SEO, listing, Pizza and spaghetti [...]

Lasagna on me this time ok? I got plenty of cash

this should be fantastic. but what about links,images, bbcodes etc etc? [...]