Saving Arduino Sensor Data


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:

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



Wiring Diagram





// 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
  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!");
  Serial.println("Initialization done.");

  tempsFile ="temps.txt", FILE_WRITE);

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


void loop() {
  // Open SD card for writing
  tempsFile ="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(" Fahrenheit: ");

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

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



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…


  1. Hi is it possible to save data directly on the dropbox from arduino using temboo web service ?
    I have used arduino yun board with DHT22 temperature/ humidity sensor

    • I have not had experience with this, but there seems to be examples out there if you search for “arduino dropbox”.

  2. 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 “MQ135.h”

    // 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()
    float rzero = gasSensor.getRZero();
    // Serial.print(“MQ135 RZERO Calibration Value : “);

    // 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!”);
    Serial.println(“Initialization done.”);

    tempsFile =“temps.txt”, FILE_WRITE);

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


    void loop() {

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

    if (tempsFile) {

    // reading gas sensor
    float ppm = gasSensor.getPPM();

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

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


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

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

    Best wishes,


    • The code and instructions for saving the data directly on your computer are here:
      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.

  4. 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.

    • Unfortunately, we did not actually end up using this in the real environment (or at all), so I don’t know how long the batteries would last.