Guest User

Untitled

a guest
Jun 16th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.15 KB | None | 0 0
  1. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  2. // ARDUINO BATTERY CAPACITY TESTER
  3. //Version-1.0
  4. //by deba168,INDIA
  5. //Dated : 04/09/2016
  6. //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  7.  
  8. #include "U8glib.h"
  9. #define MOSFET_Pin 2
  10. #define Bat_Pin A0
  11. #define Res_Pin A1
  12. #define Buzzer_Pin 9
  13. //U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);  // I2C / TWI  
  14. U8GLIB_SSD1306_128X32 u8g(U8G_I2C_OPT_NONE);    // I2C / TWI
  15.  
  16. float Capacity = 0.0; // Capacity in mAh
  17. float Res_Value = 6.8;  // Resistor Value in Ohm
  18. float Vcc = 4.64; // Voltage of Arduino 5V pin ( Mesured by Multimeter )
  19. float Current = 0.0; // Current in Amp
  20. float mA=0;         // Current in mA
  21. float Bat_Volt = 0.0;  // Battery Voltage
  22. float Res_Volt = 0.0;  // Voltage at lower end of the Resistor
  23. float Bat_High = 4.3; // Battery High Voltage
  24. float Bat_Low = 3.0; // Discharge Cut Off Voltage
  25. unsigned long previousMillis = 0; // Previous time in ms
  26. unsigned long millisPassed = 0;  // Current time in ms
  27. float sample1 =0;
  28. float sample2= 0;
  29. int x = 0;
  30. int row = 0;
  31.  
  32. //************************ OLED Display Draw Function *******************************************************
  33. void draw(void) {
  34.    //u8g.setFont(u8g_font_fub14r); // select font
  35.    u8g.setFont(u8g_font_unifont);
  36.    
  37.    if ( Bat_Volt < 1){
  38.     u8g.setPrintPos(10,20);        // set position
  39.     u8g.print("No Battery!");
  40.    }
  41.    else if ( Bat_Volt > Bat_High){
  42.     u8g.setPrintPos(25,20);        // set position
  43.     u8g.print("High-V!");
  44.    }
  45.    else if(Bat_Volt < Bat_Low){
  46.     u8g.setPrintPos(25,20);        // set position
  47.     u8g.print("Low-V!");
  48.    }
  49.    else if(Bat_Volt >= Bat_Low && Bat_Volt < Bat_High  ){
  50.      
  51.    u8g.drawStr(0, 10, "Volt: ");   // put string of display at position X, Y
  52.    u8g.drawStr(0, 21, "Curr: ");
  53.    u8g.drawStr(0, 32, "mAh: ");
  54.    /*u8g.drawStr(0, 20, "Volt: ");   // put string of display at position X, Y
  55.    u8g.drawStr(0, 40, "Curr: ");
  56.    u8g.drawStr(0, 60, "mAh: ");
  57.    */
  58.    u8g.setPrintPos(58,10);        // set position
  59.    u8g.print( Bat_Volt,2);  // display Battery Voltage in Volt
  60.    u8g.print(" V");
  61.    u8g.setPrintPos(58,21);        // set position
  62.    u8g.print( mA,0);  // display current in mA
  63.    u8g.print(" mA");
  64.    u8g.setPrintPos(58, 32);        // set position
  65.    u8g.print( Capacity ,1);     // display capacity in mAh
  66.  
  67. }
  68. }
  69. //******************************Buzzer Beep Function *********************************************************
  70.  
  71.   void beep(unsigned char delay_time){
  72.   analogWrite(9, 20);      // PWM signal to generate beep tone
  73.   delay(delay_time);          // wait for a delayms ms
  74.   analogWrite(Buzzer_Pin, 0);  // 0 turns it off
  75.   delay(delay_time);          // wait for a delayms ms  
  76.  
  77. }  
  78.  
  79. //*******************************Setup Function ***************************************************************
  80.  
  81.   void setup() {
  82.    Serial.begin(9600);
  83.    pinMode(MOSFET_Pin, OUTPUT);
  84.    pinMode(Buzzer_Pin, OUTPUT);
  85.    digitalWrite(MOSFET_Pin, LOW);  // MOSFET is off during the start
  86.    Serial.println("CLEARDATA");
  87.    Serial.println("LABEL,Time,Bat_Volt,capacity");
  88.    
  89.    //Serial.println("Arduino Battery Capacity Tester v1.0");
  90.    //Serial.println("BattVolt Current mAh");
  91.   }
  92.   //********************************Main Loop Function***********************************************************
  93.   void loop() {
  94.  // Vcc = readVcc()/1000.0; // Conevrrt mV to Volt
  95.  
  96.  
  97.   // Voltage devider Out = Bat_Volt * R2/(R1+R2 ) // R1 =10K and R2 =10K
  98.  
  99.   //************ Measuring Battery Voltage ***********
  100.  
  101.   for(int i=0;i< 100;i++)
  102.   {
  103.    sample1=sample1+analogRead(Bat_Pin); //read the voltage from the divider circuit
  104.    delay (2);
  105.   }
  106.   sample1=sample1/100;
  107.   Bat_Volt = 2* sample1 *Vcc/ 1024.0;
  108.  
  109.   // *********  Measuring Resistor Voltage ***********
  110.  
  111.    for(int i=0;i< 100;i++)
  112.   {
  113.    sample2=sample2+analogRead(Res_Pin); //read the voltage from the divider circuit
  114.    delay (2);
  115.   }
  116.   sample2=sample2/100;
  117.   Res_Volt = 2* sample2 * Vcc/ 1024.0;
  118.  
  119.   //********************* Checking the different conditions *************
  120.  
  121.   if ( Bat_Volt > Bat_High){
  122.     digitalWrite(MOSFET_Pin, LOW); // Turned Off the MOSFET // No discharge
  123.     beep(200);
  124.     Serial.println( "Warning High-V! ");
  125.     delay(1000);
  126.    }
  127.    
  128.    else if(Bat_Volt < Bat_Low){
  129.       digitalWrite(MOSFET_Pin, LOW);
  130.       beep(200);
  131.       Serial.println( "Warning Low-V! ");
  132.       delay(1000);
  133.   }
  134.   else if(Bat_Volt > Bat_Low && Bat_Volt < Bat_High  ) { // Check if the battery voltage is within the safe limit
  135.       digitalWrite(MOSFET_Pin, HIGH);
  136.       millisPassed = millis() - previousMillis;
  137.       Current = (Bat_Volt - Res_Volt) / Res_Value;
  138.       mA = Current * 1000.0 ;
  139.       Capacity = Capacity + mA * (millisPassed / 3600000.0); // 1 Hour = 3600000ms
  140.       previousMillis = millis();
  141.       Serial.print("DATA,TIME,"); Serial.print(Bat_Volt); Serial.print(","); Serial.println(Capacity);
  142.       row++;
  143.       x++;
  144.       delay(4000);
  145.  
  146.      }
  147.  
  148.    //**************************************************
  149.  
  150.   u8g.firstPage();  
  151.   do {
  152.     draw();
  153.   } while( u8g.nextPage() );
  154.  
  155.  //*************************************************
  156.  }
Add Comment
Please, Sign In to add comment