Saving Arduino Sensor Data

IMG_20160411_103622522_HDR

We had a need to take the temperature of an environment over a period of time, and record those temperatures for later analysis.

There are a number of options for recording sensor data.

  1. If connected to a computer, the data can be saved by reading the serial output and storing that in a file.
  2. If there is an SD card connected to the Arduino, the data can be saved directly to the SD card.

In this case, the Arduino needed to function by itself without being connected to a computer, so the sensor data needed to be saved to an SD card. We also needed the temperature sensor to be quite a distance from the Arduino and batteries, to minimize radiant heat affecting the temperature, so I soldered the sensor on to lengths of wire.

A number of extension shields offer SD card readers. We had a WiFi shield with an SD card reader, so that is the one I used.

There are some limitations with this set up. The biggest issue is that this set up does not include a time stamp with the temperatures. In order to get a timestamp, it is best to record the data with the Arduino connected to a computer, or a Raspberry Pi. See here for code to capture the sensor data on a computer (like a Raspberry Pi) using Python: https://github.com/mossiso/arduino-tuts/tree/master/raspberry-pi

Another way is to use a dedicated SD card shield with an RTC (Real Time Clock) built in, such as this one from adafruit: https://www.adafruit.com/products/1141

Hardware

 

Wiring Diagram

temperature-tmp36

 

Code

 

// SPI and SD libraries. SPI for connecting SD card to SPI bus.
#include <SPI.h>
#include <SD.h>
const int sdPin = 4;

// Temperature pin set to analog 0
const int temPin = 0;

// Delay time. How often to take a temperature reading, in miliseconds
// 20 minutes = 1200000 milliseconds
const int delayTime = 1200000;

// File variable
File tempsFile;



void setup() {
  // Serial output for when connected to computer
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");
  if(!SD.begin(sdPin)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("Initialization done.");

  tempsFile = SD.open("temps.txt", FILE_WRITE);

  if (tempsFile) {
    Serial.println("Printing temperatures");
    tempsFile.println("Printing temperatures:");
    tempsFile.close();
    Serial.println("Done.");
  } else {
    Serial.println("Error opening file in setup.");
  }

}

void loop() {
  /********************/
  // Open SD card for writing
  tempsFile = SD.open("temps.txt", FILE_WRITE);

  if (tempsFile) {
    // Temperature readings
    float voltage, degreesC, degreesF;
    voltage = getVoltage(temPin);
    degreesC = (voltage - 0.5) * 100.0;
    degreesF = degreesC * (9.0/5.0) + 32.0;

    // write temps to Serial
    Serial.print("Celsius: ");
    Serial.print(degreesC);
    Serial.print(" Fahrenheit: ");
    Serial.println(degreesF);

    // write temps to SD card
    tempsFile.print("Celsius: ");
    tempsFile.print(degreesC);
    tempsFile.print(" Fahrenheit: ");
    tempsFile.println(degreesF);

    // close the file
    tempsFile.close();
  } else {
    Serial.println("Error opening file in loop.");
  }


  delay(delayTime);

}

float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);
}

Ammon is a Digital Humanities Developer and Makerspace Technologist at the Scholars' Lab where he codes projects, builds tools to fix old projects, does some systems administration, 3D prints and plays with Arduinos and Raspberry Pis. Ammon has BA's in History and German from ASU, an MA in History from GMU and is ABD PhD…

5 Comments

  1. Hi I’m trying to save the data from a Air Quality Sensor to an sd card (OpenLog from Sparkfun). When using the code I have. It keeps saying. Initializing failed. What is wrong?

    #include
    #include “MQ135.h”
    #include

    // SD OpenLog
    const int sdPin = 0;

    // File variable
    File tempsFile;

    #define ANALOGPIN A0 // Define Analog PIN on Arduino Board
    #define RZERO 206.85 // Define RZERO Calibration Value
    MQ135 gasSensor = MQ135(ANALOGPIN);

    void setup()
    {
    Serial.begin(9600);
    //Serial.print(“Hello”);
    float rzero = gasSensor.getRZero();
    delay(3000);
    // Serial.print(“MQ135 RZERO Calibration Value : “);
    //Serial.println(rzero);

    // Serial output for when connected to computer
    // while (!Serial) {
    // ; // wait for serial port to connect. Needed for native USB port only
    // }

    Serial.print(“Initializing SD card…”);
    if(!SD.begin(sdPin)) {
    Serial.println(“initialization failed!”);
    return;
    }
    Serial.println(“Initialization done.”);

    tempsFile = SD.open(“temps.txt”, FILE_WRITE);

    if (tempsFile) {
    Serial.println(“Printing temperatures”);
    tempsFile.println(“Printing temperatures:”);
    tempsFile.close();
    Serial.println(“Done.”);
    } else {
    Serial.println(“Error opening file in setup.”);
    }

    }

    void loop() {

    // Open SD card for writing
    tempsFile = SD.open(“Sensors.txt”, FILE_WRITE);

    if (tempsFile) {

    // reading gas sensor
    float ppm = gasSensor.getPPM();
    delay(1000);
    digitalWrite(13,HIGH);

    Serial.print(“CO2 ppm value : “);
    Serial.print(String(ppm));

    // writing it to the sd card
    tempsFile.print(“ppm value: “);
    tempsFile.print(ppm);

    tempsFile.close();

    }else {
    Serial.println(“Error opening file in loop.”);
    }
    }

  2. Hi,
    Is it possible to write how we can save data from sensor directly on computer without using SD card?

    Best wishes,

    Mahmood,

    • The code and instructions for saving the data directly on your computer are here: https://github.com/mossiso/arduino-tuts/tree/master/raspberry-pi
      The instructions say to use a Raspberry Pi, but if you have python installed on your computer, and the Arduino hooked up to your computer (via USB), then you can “pretend” your computer is the Raspberry Pi when following the instructions.

      You may need to change the serial port detected by the python code (this part /dev/ttyACM0).

      You can send me an email directly if you run into other issues.

  3. Hello! I’m trying to build a similar remote detector, and I was just wondering how long your batteries last while recording data? My detector won’t be easy to access, so I don’t want to have to replace the batteries too often.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Archives