Advertisement
sunu

FF3O206H2BMNS8L.ino

Nov 26th, 2014
255
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //http://www.instructables.com/files/orig/FF3/O206/H2BMNS8L/FF3O206H2BMNS8L.ino
  2.  
  3. #include <LiquidCrystal.h>
  4.  
  5. /* This sketch describes how to connect a ACS715 Current Sense Carrier
  6. (http://www.pololu.com/catalog/product/1186) to the Arduino,
  7. and read current flowing through the sensor.
  8.  
  9. */
  10.  
  11. LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
  12.  
  13. /*
  14.  
  15. Vcc on carrier board to Arduino +5v
  16. GND on carrier board to Arduino GND
  17. OUT on carrier board to Arduino A0
  18.  
  19.  
  20.  
  21. Insert the power lugs into the loads positive lead circuit,
  22. arrow on carrier board points to load, other lug connects to
  23. power supply positive
  24.  
  25. Voltage Divider
  26.  
  27. 11.66 from + to A4
  28. 4.62k from A4 to Gnd
  29. Ratio 2.5238
  30.  
  31.  
  32. */
  33. int batMonPin = A4;    // input pin for the voltage divider
  34. int batVal = 0;       // variable for the A/D value
  35. float pinVoltage = 0; // variable to hold the calculated voltage
  36. float batteryVoltage = 0;
  37.  
  38. int analogInPin = A0;  // Analog input pin that the carrier board OUT is connected to
  39. int sensorValue = 0;        // value read from the carrier board
  40. int outputValue = 0;        // output in milliamps
  41. unsigned long msec = 0;
  42. float time = 0.0;
  43. int sample = 0;
  44. float totalCharge = 0.0;
  45. float averageAmps = 0.0;
  46. float ampSeconds = 0.0;
  47. float ampHours = 0.0;
  48. float wattHours = 0.0;
  49. float amps = 0.0;
  50.  
  51. int R1 = 11660; // Resistance of R1 in ohms
  52. int R2 = 4620; // Resistance of R2 in ohms
  53.  
  54. float ratio = 0;  // Calculated from R1 / R2
  55.  
  56. void setup() {
  57.   // initialize serial communications at 9600 bps:
  58.   Serial.begin(9600);
  59.   lcd.begin(20, 4);
  60. }
  61.  
  62. void loop() {
  63.  
  64. int sampleBVal = 0;
  65. int avgBVal = 0;  
  66. int sampleAmpVal = 0;
  67. int avgSAV = 0;
  68.  
  69.  for (int x = 0; x < 10; x++){ // run through loop 10x
  70.  
  71.   // read the analog in value:
  72.   sensorValue = analogRead(analogInPin);  
  73.   sampleAmpVal = sampleAmpVal + sensorValue; // add samples together
  74.  
  75.   batVal = analogRead(batMonPin);    // read the voltage on the divider
  76.   sampleBVal = sampleBVal + batVal; // add samples together
  77.  
  78.   delay (10); // let ADC settle before next sample
  79.  
  80.  }
  81.  
  82.  avgSAV = sampleAmpVal / 10;
  83.  
  84.   // convert to milli amps
  85.   outputValue = (((long)avgSAV * 5000 / 1024) - 500 ) * 1000 / 133;  
  86.  
  87. /* sensor outputs about 100 at rest.
  88. Analog read produces a value of 0-1023, equating to 0v to 5v.
  89. "((long)sensorValue * 5000 / 1024)" is the voltage on the sensor's output in millivolts.
  90. There's a 500mv offset to subtract.
  91. The unit produces 133mv per amp of current, so
  92. divide by 0.133 to convert mv to ma
  93.          
  94. */
  95.  
  96.  
  97.  avgBVal = sampleBVal / 10; //divide by 10 (number of samples) to get a steady reading
  98.  
  99.   pinVoltage = avgBVal * 0.00610;       //  Calculate the voltage on the A/D pin
  100.                                 /*  A reading of 1 for the A/D = 0.0048mV
  101.                                     if we multiply the A/D reading by 0.00488 then
  102.                                     we get the voltage on the pin.  
  103.                                    
  104.                                     NOTE! .00488 is ideal. I had to adjust
  105.                                     to .00610 to match fluke meter.
  106.                                    
  107.                                     Also, depending on wiring and
  108.                                     where voltage is being read, under
  109.                                     heavy loads voltage displayed can be
  110.                                     well under voltage at supply. monitor
  111.                                     at load or supply and decide.
  112. */
  113.  
  114.   ratio = (float)R1 / (float)R2;
  115.   batteryVoltage = pinVoltage * ratio;    //  Use the ratio calculated for the voltage divider
  116.                                           //  to calculate the battery voltage
  117.                                          
  118.                                            
  119.   amps = (float) outputValue / 1000;
  120.   float watts = amps * batteryVoltage;
  121.    
  122.   Serial.print("Volts = " );                      
  123.   Serial.print(batteryVoltage);      
  124.   Serial.print("\t Current (amps) = ");      
  125.   Serial.print(amps);  
  126.   Serial.print("\t Power (Watts) = ");  
  127.   Serial.print(watts);  
  128.  
  129.    
  130.   sample = sample + 1;
  131.  
  132.   msec = millis();
  133.  
  134.  
  135.  
  136.  time = (float) msec / 1000.0;
  137.  
  138.  totalCharge = totalCharge + amps;
  139.  
  140.  averageAmps = totalCharge / sample;
  141.  
  142.  ampSeconds = averageAmps*time;
  143.  
  144.  ampHours = ampSeconds/3600;
  145.  
  146.  wattHours = batteryVoltage * ampHours;
  147.  
  148.  
  149.  
  150.  
  151.  
  152.   Serial.print("\t Time (hours) = ");
  153.   Serial.print(time/3600);
  154.  
  155.   Serial.print("\t Amp Hours (ah) = ");
  156.   Serial.print(ampHours);
  157.   Serial.print("\t Watt Hours (wh) = ");
  158.   Serial.println(wattHours);
  159.  
  160.  
  161.   lcd.setCursor(0,0);
  162.     lcd.print(batteryVoltage);
  163.     lcd.print(" V ");
  164.     lcd.print(amps);
  165.     lcd.print(" A ");
  166.  
  167.   lcd.setCursor(0,1);
  168.   lcd.print(watts);
  169.   lcd.print(" W ");
  170.   lcd.print(time/3600);
  171.   lcd.print(" H ");
  172.  
  173.   lcd.setCursor(0,2);
  174.   lcd.print(ampHours);
  175.   lcd.print(" Ah ");
  176.   lcd.print(wattHours);
  177.   lcd.print(" Wh ");
  178.  
  179.   lcd.setCursor(0,3);
  180.   lcd.print(ratio, 5);
  181.   lcd.print("   ");
  182.   lcd.print(avgBVal);
  183.  
  184.   // wait 10 milliseconds before the next loop
  185.   // for the analog-to-digital converter to settle
  186.   // after the last reading:
  187.   delay(10);                    
  188. }
Advertisement
RAW Paste Data Copied
Advertisement