Project showcase

Arduino Addons: Battery Level Indicator © GPL3+

A realtime battery indicator for your project without extra components or using any analog I/Os.

  • 3,985 views
  • 12 comments
  • 31 respects

Components and supplies

Apps and online services

About this project

Several times I've wished I had a battery indicator in my battery-powered projects, but it uses so many custom characters in the LCD and sacrifice analog input for voltage indication and components and connections. So we decided to do it the smart way and push the Arduino to its limits "as we always do" and get the maximum out of it.

In this project we use only one custom character from the LCD. So you still have seven free custom characters to use in your project. We also used the trick of reff voltage to get the input voltage from the Arduino without using any analog inputs or components, looks like the complete package.

All you have to is add the function that displays the indicator in the loop function and add the place where you wanted to display it and you are ready.

Enjoy

Download Code

Schematics

Schema
Lcd keypad shield diagram pinout wiring configuration schematics exfbetirpa

Code

Battery IndicatorC/C++
#include <LiquidCrystal.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void setup() {
  // Initialize LCD
    lcd.begin(16 , 2);
}

void loop() {
  // load battery icon on position 15,0
  batterylevel(15,0);
  
}
//draw battery level in position x,y
void batterylevel(int xpos,int ypos)
{
  //read the voltage and convert it to volt
  double curvolt = double( readVcc() ) / 1000;
  // check if voltge is bigger than 4.2 volt so this is a power source
  if(curvolt > 4.2)
  {
    byte batlevel[8] = {
    B01110,
    B11111,
    B10101,
    B10001,
    B11011,
    B11011,
    B11111,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
  if(curvolt <= 4.2 && curvolt > 4.0)
  {
    byte batlevel[8] = {
    B01110,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
  if(curvolt <= 4.0 && curvolt > 3.8)
  {
    byte batlevel[8] = {
    B01110,
    B10001,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
  if(curvolt <= 3.8 && curvolt > 3.6)
  {
    byte batlevel[8] = {
    B01110,
    B10001,
    B10001,
    B11111,
    B11111,
    B11111,
    B11111,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
  if(curvolt <= 3.6 && curvolt > 3.4)
  {
    byte batlevel[8] = {
    B01110,
    B10001,
    B10001,
    B10001,
    B11111,
    B11111,
    B11111,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
  if(curvolt <= 3.4 && curvolt > 3.2)
  {
    byte batlevel[8] = {
    B01110,
    B10001,
    B10001,
    B10001,
    B10001,
    B11111,
    B11111,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
  if(curvolt <= 3.2 && curvolt > 3.0)
  {
    byte batlevel[8] = {
    B01110,
    B10001,
    B10001,
    B10001,
    B10001,
    B10001,
    B11111,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
  if(curvolt < 3.0)
  {
    byte batlevel[8] = {
    B01110,
    B10001,
    B10001,
    B10001,
    B10001,
    B10001,
    B10001,
    B11111,
    };
    lcd.createChar(0 , batlevel);
    lcd.setCursor(xpos,ypos);
    lcd.write(byte(0));
  }
}
//read internal voltage
long readVcc() {
  long result;
  // Read 1.1V reference against AVcc
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Convert
  while (bit_is_set(ADCSRA, ADSC));
  result = ADCL;
  result |= ADCH << 8;
  result = 1126400L / result; // Back-calculate AVcc in mV
  return result;
}

Comments

Similar projects you might like

Distance Measurement Vehicle via Websocket

Project tutorial by Matthew Lee

  • 3,238 views
  • 1 comment
  • 22 respects

Barbot: Cocktail Mixing Robot

Project tutorial by sidlauskas

  • 4,547 views
  • 3 comments
  • 31 respects

RING PONG

Project showcase by aerodynamics

  • 1,603 views
  • 0 comments
  • 7 respects

An Urban Plant Watering Solution

Project tutorial by James Yu

  • 4,118 views
  • 7 comments
  • 17 respects

Electron Music Box Buzzer App

Project tutorial by Iain

  • 458 views
  • 0 comments
  • 3 respects
Add projectSign up / Login