/* SD card datalogger This example shows how to log data from three analog sensors to an SD card using the SD library. The circuit: * SD card attached to SPI bus as follows: ** UNO: MOSI - pin 11, MISO - pin 12, CLK - pin 13, CS - pin 4 (CS pin can be changed) and pin #10 (SS) must be an output ** Mega: MOSI - pin 51, MISO - pin 50, CLK - pin 52, CS - pin 4 (CS pin can be changed) and pin #52 (SS) must be an output ** Leonardo: Connect to hardware SPI via the ICSP header Pin 4 used here for consistency with other Arduino examples original code was created on 24 Nov 2010 and modified 9 Apr 2012 by Tom Igoe I (Debasish Dutta) was again modified for my energy monitoring requirement on 14/01/14 This example code is in the public domain. */ #include // On the Ethernet Shield, CS is pin 4. Note that even if it's not // used as the CS pin, the hardware CS pin (10 on most Arduino boards, // 53 on the Mega) must be left as an output or the SD library // functions will not work. const int chipSelect = 4; File dataFile; float sample1=0; // for voltage float sample2=0; // for current float voltage=0.0; float val; // current callibration float actualval; // read the actual current from ACS 712 float amps=0.0; float totamps=0.0; float avgamps=0.0; float amphr=0.0; float watt=0.0; float energy=0.0; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } Serial.print("Initializing SD card..."); // make sure that the default chip select pin is set to // output, even if you don't use it: pinMode(SS, OUTPUT); // see if the card is present and can be initialized: if (!SD.begin(chipSelect)) { Serial.println("Card failed, or not present"); // don't do anything more: while (1) ; } Serial.println("card initialized."); // Open up the file we're going to log to! dataFile = SD.open("energy.txt", FILE_WRITE); if (! dataFile) { Serial.println("error opening energy.txt"); // Wait forever since we cant write data while (1) ; } } void loop() { long milisec = millis(); // calculate time in milisec long time=milisec/1000; // convert time to sec ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// taking 150 samples from sensors with a inerval of 2sec and then average the samples data collected for(int i=0;i<150;i++) { sample1+=analogRead(A2); //read the voltage from the sensor sample2+=analogRead(A3); //read the current from sensor delay(2); } sample1=sample1/150; sample2=sample2/150; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /////voltage calculation////////////////////// voltage=4.669*2*sample1/1000; // callibration // 3.25 from voltage div is eqv 696 in A0 reading // multiply 2 to get actual voltage// I used two 1k resistor to read 6.36v battery volt //////////////////////////////////////////////// current calculation ////////////////////// val =(5.0*sample2)/1024.0; actualval =val-2.5; // offset voltage is 2.5v amps =actualval*10;// 10 is multiplied as 100mv/A ( from data sheet ) totamps=totamps+amps; // total amperes avgamps=totamps/time; // average amperess amphr=(avgamps*time); // ampere hour watt =voltage*amps; // power=voltage*current energy=(watt*time)/3600; // energy = power*timein Watt-sec ///again convert to Watt-Hr by dividing 3600sec //////////////////////////////////////////////////////// // make a string for assembling the data to log: String dataString = ""; int parameter[4]={voltage,amps,watt,energy}; // here parameters are power,energy,watt-hour and current // read 4 parameters and append to the string: for (int i = 0; i < 4; i++) { int sensor = parameter[i]; dataString += String(sensor); if (i < 4) { dataString += ","; } } dataFile.println(dataString); // print to the serial port too: Serial.println(dataString); // The following line will 'save' the file to the SD card after every // line of data - this will use more power and slow down how much data // you can read but it's safer! // If you want to speed up the system, remove the call to flush() and it // will save the file only every 512 bytes - every time a sector on the // SD card is filled with data. dataFile.flush(); // Take 1 measurement every 500 milliseconds delay(500); }