Advertisement
learnelectronics

Arduino Li-Ion 18650 Battery Charge Gauge

Jul 14th, 2017
4,936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. /*
  2.  * Arduino 18650 Battery Gauge
  3.  *
  4.  * learnelectronics
  5.  * 13 JUL 2017
  6.  *
  7.  * www.youtube.com/c/learnelectronics
  8.  */
  9.  
  10. #include <LiquidCrystal.h>                    //we need the library for the lcd display
  11.  
  12. LiquidCrystal lcd(4, 6, 10, 11, 12, 13);      //we ned to tell the arduino which pins to use with the lcd
  13.  
  14. float rawV = 0.0;                             //a variable with a decimal point to hold battery voltage
  15. int batC = 0;                                 //a variable with no decimal point to hold battery percentage
  16. int graph = 0;                                //a variable with no decimal point to hold the value for the graph
  17.  
  18. void setup() {                                //only runs when the Arduino is powered on reset
  19.  
  20.   lcd.begin(16, 2);                           //start lcd with 16 columns & 2 rows
  21.   lcd.clear();                                //clear any garbage from screen
  22.   pinMode(A0,INPUT);                          //we will use analog pin 0 to read voltage from 18650
  23.  
  24. }
  25.  
  26. void loop() {                                 //runs over and over
  27.  
  28.   rawV = (analogRead(A0)*4.98)/1024;          //figure out the battery voltage (4.98 is the actual reading of my 5V pin)
  29.  
  30.  
  31.                                               //some logic to set values
  32.                                              
  33.   if (rawV <3.6){                             //battery @ 3.5V or less
  34.   batC = 0;                                   //% = 0
  35.   graph = 0;                                  //# of graph segments to light
  36.   }
  37.  
  38. if(rawV > 3.5 && rawV < 3.7){                 //battery @ 3.6V
  39.   batC = 2;
  40.   graph = 1;
  41.   }
  42.  
  43. if (rawV > 3.6 && rawV < 3.8){                //battery @ 3.7V
  44.   batC = 12;
  45.   graph = 4;
  46.   }
  47.  
  48. if (rawV > 3.7 && rawV <3.9){                 //battery @ 3.8V
  49.   batC = 42;
  50.   graph = 7;
  51.   }
  52.  
  53. if (rawV > 3.8 && rawV < 4.0){                //battery @ 3.9V
  54.   batC = 62;
  55.   graph = 10;
  56.   }
  57.  
  58. if (rawV > 3.9 && rawV < 4.1){                //battery @ 4.0V
  59.   batC = 79;
  60.   graph = 13;
  61.   }
  62.  
  63. if (rawV > 4.0 && rawV < 4.2){                //battery @ 4.1V
  64.   batC = 81;
  65.   graph = 15;
  66.   }
  67.  
  68. if (rawV > 4.19){                             //battery @ 4.2V
  69.   batC = 100;
  70.   graph = 16;
  71.   }
  72.  
  73.  
  74.                                               //send all the values to the lcd
  75.  
  76.   lcd.setCursor(0,0);
  77.   lcd.print("V:");
  78.   lcd.setCursor(3,0);
  79.   lcd.print(rawV);
  80.   lcd.setCursor(10,0);
  81.   lcd.print("%:");
  82.   lcd.setCursor(13,0);
  83.   lcd.print(batC);
  84.   lcd.setCursor(0,1);
  85.   for(int m = 0; m < graph; m++){             //draw the graph (light up m number of segments)
  86.     lcd.write(255);
  87.   }
  88.  
  89. delay(1000);                                  //wait one second to stabilize
  90.  
  91. lcd.clear();                                  //clear previous data from lcd
  92.  
  93.  
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement