Advertisement
Guest User

Arduino Battery Capacity Tester

a guest
Jan 15th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <StopWatch.h>
  2.  
  3. #include <Time.h>
  4.  
  5. /*
  6. A battery life cycle tester using an Lcd as output
  7. */
  8.  
  9. // include the library code:
  10. #include <LiquidCrystal.h>
  11.  
  12.  
  13. // initialize the library with the numbers of the interface pins
  14. LiquidCrystal lcd( 12, 11, 6, 5, 4, 3 );
  15. int sensorPin = 15; // input battery sensor reading
  16. int sensor2Pin = 14; // input voltage reading
  17. int ledPin = 13; // output trigger for MOSFET/Transistor/relay
  18. int sensorValue = 0; // this holds the value that the sensor reads
  19. float LiMinThreshold = 2700; // Lithium Minimal Voltage for load removal
  20. float LiMaxThreshold = 4200; // Lithium Max Voltage for load removal
  21. float NmhMinThreshold = 950; // NMH Minimal Voltage for load removal
  22. float NmhMaxThreshold = 1600; // NMH Max Voltage for load removal
  23. float SelectedMinThreshold = 5000;
  24. int BatVolt = 5000;
  25. boolean done = false;
  26. float mah = 0.0;
  27. StopWatch sw_secs(StopWatch::SECONDS);
  28.  
  29.  
  30.  
  31.  
  32. void CL2(){
  33. lcd.setCursor(0, 1);// Second line first char
  34. lcd.print(" ");
  35. lcd.setCursor(0, 1);// Second line first char
  36. }
  37. void setup() {
  38. pinMode(ledPin, OUTPUT); // makes it so the IC knows this is an output pin
  39. lcd.begin(16,2); // sets up the LCD display, first is vertical pixel count second is rows
  40. lcd.print("Batt Test ON"); // prints a message to the LCD
  41. lcd.setCursor(0,1); //second line first character
  42. lcd.print("Det. Bat Type"); //print the voltage value
  43. delay(2000);
  44. lcd.setCursor(0,1); // second line first character
  45. lcd.print (" ");
  46. lcd.setCursor(0,1);
  47. digitalWrite(ledPin, HIGH); // makes the MOSFET/Transistor/relay/LED on
  48. sensorValue = analogRead(sensorPin); // reads the voltage value from the sensor
  49. digitalWrite(ledPin, LOW);
  50. BatVolt = sensorValue*3.27*2;
  51. if (BatVolt > 4500){
  52. lcd.print("Too High");
  53. done = true;}
  54. else if (BatVolt > LiMinThreshold){
  55. lcd.print("Li-Ion");
  56. SelectedMinThreshold = LiMinThreshold;
  57. }
  58. else if (BatVolt > NmhMinThreshold){
  59. lcd.print("NiCd/Alk ");
  60. SelectedMinThreshold = NmhMinThreshold;}
  61. else{
  62. lcd.print("Unknown Bat");
  63. done = true;}
  64. lcd.print("V=");
  65. lcd.print(sensorValue*3.27*2); // prints voltage value
  66. delay(3000);
  67. }
  68.  
  69. void loop(){
  70. if (BatVolt > SelectedMinThreshold && !done){
  71. digitalWrite(ledPin, HIGH); // sets the transistor on
  72. sensorValue = analogRead(sensorPin);
  73. BatVolt = (sensorValue*3.27*2);
  74. sw_secs.start();
  75. CL2();
  76. lcd.print(BatVolt);
  77. lcd.print("mV ");
  78. mah = ((sensorValue*3.27*2/4.7)*(sw_secs.elapsed())/360); // this is the mAh equation I'm confused about
  79. lcd.print(mah);
  80. lcd.print(" mAh ");
  81. delay(40);
  82. }
  83. else
  84. {
  85. done=true;
  86. digitalWrite(ledPin, LOW); //turn the transistor/mosfet/relay/led off
  87. lcd.setCursor(0,0);
  88. lcd.print("Bat Tester Done");
  89. sensorValue = analogRead(sensorPin);
  90. BatVolt = (sensorValue*3.27*2);
  91. lcd.setCursor(0,1);
  92. lcd.print(BatVolt);
  93. lcd.print("mV I=");
  94. lcd.print(mah);
  95. lcd.print("mAh ");
  96. for (int i=0; i<100 ; i++){
  97. }
  98. delay(1000);
  99. }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement