Monday, June 23, 2014

Arduino datalogger code.

This is my new Arduino datalogger script, it writes the temperature, light value (resistance to ground) and the batteries voltage in procent to an SD card (and if you set the row with #define ECHO_TO_SERIAL 0 to #define ECHO_TO_SERIAL 1 it writes it in the serial monitor:
This is so that you can import it to excel:
/*
  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

 created  24 Nov 2010
 modified 9 Apr 2012 by Tom Igoe

 This example code is in the public domain.
 
 */
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
#define ECHO_TO_SERIAL   0 // echo data to serial port
int photocellPin = A0;
int photocellReading;
int tempReading;
#define aref_voltage 5.0         // we tie 3.3V to ARef and measure it with a multimeter!
int tempPin = A1;
RTC_DS1307 rtc;



// 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 = 10;

File dataFile;
const float referenceVolts = 5;        // the default reference on a 5-volt board
//const float referenceVolts = 3.3;  // use this for a 3.3-volt board

const float R1 = 1000; // value for a maximum voltage of 10 volts
const float R2 = 1000;
int u;
// determine by voltage divider resistors, see text
const float resistorFactor = 511.0 / (R2/(R1 + R2));
const int batteryPin = A4;

void setup()
{
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();
 // Open serial communications and wait for port to open:
  Serial.begin(9600);



  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);
    pinMode(53, OUTPUT);


  // see if the card is present and can be initialized:
  if (!SD.begin(10, 11, 12, 13)) {
    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("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
     pinMode(A5, INPUT);
   digitalWrite(A5, LOW);
}

void loop()
{
     int val = analogRead(batteryPin);  // read the value from the sensor
   float volts = (val / resistorFactor) * referenceVolts ; // calculate the ratio
   u = (volts) * 100 / 5;
  // make a string for assembling the data to log:
    photocellReading = analogRead(photocellPin);
    tempReading = analogRead(tempPin);
 

DateTime now = rtc.now();
    #if ECHO_TO_SERIAL
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    #endif
 
   dataFile.print(now.year(), DEC);
   dataFile.print(" - ");
   dataFile.print(now.month(), DEC);
   dataFile.print(" / ");
   dataFile.print(now.day(), DEC);
   dataFile.print("*");
   dataFile.print("kl. ");
   dataFile.print(now.hour(), DEC);
   dataFile.print(':');
   dataFile.print(now.minute(), DEC);
   dataFile.print(':');
   dataFile.print(now.second(), DEC);
   dataFile.print("*");
   dataFile.print("Value, Light: *");
   dataFile.print(photocellReading);
   dataFile.print("*");
   analogRead(tempPin);
   float voltage = tempReading * aref_voltage / 1024;
   float temperatureC = (voltage - 0.5) * 100 ;
   float temperatureF = (temperatureC * 9 / 5) + 32;
   dataFile.print("Temperature(Celcius): *");
   dataFile.print(temperatureC);
   dataFile.print("*");
   dataFile.print("Temperature(Farenheit): *");
   dataFile.print(temperatureF);
   dataFile.print("*");
   dataFile.print("Battery: *");
   dataFile.print(u);
   dataFile.print("*");
   dataFile.println(" %");
   #if ECHO_TO_SERIAL
  // print to the serial port too:
  Serial.print("Light: ");
  Serial.println(photocellReading);
  Serial.print("Temperature C: ");
  Serial.println(temperatureC);
  Serial.print("Temperature F: ");
  Serial.println(temperatureF);
  Serial.print("Battery: ");
  Serial.print(u);
  Serial.println(" %");
  #endif


  // 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(50);
}




And this is just normal format:

/*
  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

 created  24 Nov 2010
 modified 9 Apr 2012 by Tom Igoe

 This example code is in the public domain.
 
 */
#include <Wire.h>
#include "RTClib.h"
#include <SPI.h>
#include <SD.h>
#define ECHO_TO_SERIAL   0 // echo data to serial port
int photocellPin = A0;
int photocellReading;
int tempReading;
#define aref_voltage 5.0         // we tie 3.3V to ARef and measure it with a multimeter!
int tempPin = A1;
RTC_DS1307 rtc;



// 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 = 10;

File dataFile;
const float referenceVolts = 5;        // the default reference on a 5-volt board
//const float referenceVolts = 3.3;  // use this for a 3.3-volt board

const float R1 = 1000; // value for a maximum voltage of 10 volts
const float R2 = 1000;
int u;
// determine by voltage divider resistors, see text
const float resistorFactor = 511.0 / (R2/(R1 + R2));
const int batteryPin = A4;

void setup()
{
#ifdef AVR
  Wire.begin();
#else
  Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
  rtc.begin();
 // Open serial communications and wait for port to open:
  Serial.begin(9600);



  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);
    pinMode(53, OUTPUT);


  // see if the card is present and can be initialized:
  if (!SD.begin(10, 11, 12, 13)) {
    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("datalog.txt", FILE_WRITE);
  if (! dataFile) {
    Serial.println("error opening datalog.txt");
    // Wait forever since we cant write data
    while (1) ;
  }
     pinMode(A5, INPUT);
   digitalWrite(A5, LOW);
}

void loop()
{
     int val = analogRead(batteryPin);  // read the value from the sensor
   float volts = (val / resistorFactor) * referenceVolts ; // calculate the ratio
   u = (volts) * 100 / 5;
  // make a string for assembling the data to log:
    photocellReading = analogRead(photocellPin);
    tempReading = analogRead(tempPin);
 

DateTime now = rtc.now();
    #if ECHO_TO_SERIAL
    Serial.print(now.year(), DEC);
    Serial.print('/');
    Serial.print(now.month(), DEC);
    Serial.print('/');
    Serial.print(now.day(), DEC);
    Serial.print(' ');
    Serial.print(now.hour(), DEC);
    Serial.print(':');
    Serial.print(now.minute(), DEC);
    Serial.print(':');
    Serial.print(now.second(), DEC);
    Serial.println();
    #endif
 
   dataFile.print(now.year(), DEC);
   dataFile.print(" - ");
   dataFile.print(now.month(), DEC);
   dataFile.print(" / ");
   dataFile.println(now.day(), DEC);
   dataFile.print("kl. ");
   dataFile.print(now.hour(), DEC);
   dataFile.print(':');
   dataFile.print(now.minute(), DEC);
   dataFile.print(':');
   dataFile.println(now.second(), DEC);
   dataFile.print("Value, Light: ");
   dataFile.println(photocellReading);
   analogRead(tempPin);
   float voltage = tempReading * aref_voltage / 1024;
   float temperatureC = (voltage - 0.5) * 100 ;
   float temperatureF = (temperatureC * 9 / 5) + 32;
   dataFile.print("Temperature(Celcius):");
   dataFile.println(temperatureC);
   dataFile.print("Temperature(Farenheit):");
   dataFile.println(temperatureF);
   dataFile.print("Battery: ");
   dataFile.print(u);
   dataFile.println(" %");
   #if ECHO_TO_SERIAL
  // print to the serial port too:
  Serial.print("Light: ");
  Serial.println(photocellReading);
  Serial.print("Temperature C: ");
  Serial.println(temperatureC);
  Serial.print("Temperature F: ");
  Serial.println(temperatureF);
  Serial.print("Battery: ");
  Serial.print(u);
  Serial.println(" %");
  #endif


  // 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(300000);
}









No comments:

Post a Comment