Arduino – Datalogger with Temperature Sensor and Photoresistor

In this project I’m going to create a simple Datalogger with my Arduino and an Ethernet shield.

With this concept you can change my code and monitor any sensor you desire.

I’ll be using a photoresistor and a temperature sensor and all the information will be stored in a micro SD card.

Watch the video below for a complete tutorial

Note: I apologize for my voice. I’m a bit sick so my voice sounded a bit different than usual.

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

Build 25 cool Arduino projects with our course even with no prior experience DOWNLOAD »

Parts Required

parts1

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematics

Schematics

Upload the code below

/*
 Modified by Rui Santos
 For more Arduino Projects: https://randomnerdtutorials.com 
 
 SD card datalogger
 
 This example shows how to log data from three analog sensors 
 to an SD card using the SD library.
    
 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
 
 based on Tom Igoe example.
 */
 
#include <SD.h>
float tempK;  //stores kelvin temperature
int sensor;   //stores sensor value everytime we use the analog read function
// 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;
int analogPin=0;      //help us read the analogpin of our INPUTS
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(10, 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:
    return;
  }
  Serial.println("card initialized.");
  //Defines Photoresistor as an INPUT on PIN Number 0
  pinMode(0, INPUT);
  //Defines Temperature sensor as an INPUT on PIN Number 1
  pinMode(1, INPUT);
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";
  // reads three sensors and append to the string:
  for (analogPin = 0; analogPin < 3; analogPin++){
    //reads the photoresistor on PIN0
    if( analogPin==0){
      sensor = analogRead(analogPin);
    }
    //reads the kelvin Tempeature on PIN1 
    //then converts our tempeature to Degrees
    else if(analogPin==1){  
      //reads temperature and converts to kelvin
      tempK = (((analogRead(analogPin)/ 1023.0) * 5.0) * 100.0);
      //Converts Kelvin to Celsius minus 1.5 degrees error
      sensor = tempK - 273.0; 
    }
    //reads the Kelvin temperature on PIN1
    //then converts our tempeature to Farenheit
    else{
      //reads temperature and converts to kelvin
      tempK = (((analogRead(analogPin-1)/ 1023.0) * 5.0) * 100.0);
      //Converts Kelvin to Farenheit
      sensor = ((tempK - 2.5) * 9 / 5) - 459.67;
    }
    //stores our values
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ","; 
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("data.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  } 
}

View raw code

I hope this tutorial can help you with your projects!

Support my work by sharing this video with a friend that also likes electronics.

News: I’m currently working on a project that I’m really excited about. I won’t tell exactly what It is. But I’ll share with you in a few weeks as soon as It’s finished. So stay tuned for more.

Thanks for reading, you can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.


Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »


Enjoyed this project? Stay updated by subscribing our weekly newsletter!

23 thoughts on “Arduino – Datalogger with Temperature Sensor and Photoresistor”

  1. Great tutorial. In your partlist can you link each part to the store that you got it from? It would be easier to find the component that way. will save a few google searches..

  2. hei Rui, could you share why did you chose Arduino Uno rather than other ones like Due or Yun? I’m a new member and interesting in this temperator sensor project.

    • Hi Nick,
      Thanks for joining the Arduino community. I’ve use the Arduino Uno because it’s the one I have.
      I don’t own an Arduino Yun yet, but that would great board to do this project with.

      Might do that later if I get a Yun.

      Thanks again for taking the time to leave a comment,
      Rui

  3. Hello Rui,
    Thank you for this nice tutorial!

    I tried to make it on my own, but when i open the serial monitor, it says : “error opening datalog.txt”

    It doesn’t write anything…
    Because i’m new in the world off arduino, i hope you can help me.

    Thank you.

  4. Would like to see this expanded to add Date/Time stamping with a RTC and a simple setup to specify logging period & the option to add identifying text for each line of output then of course saviny in CSV format so it can be imported into Excel.

    • Yeah those are great ideas!
      I’m currently finishing one of my major project
      (Home Automation Platform that will be released in 2 weeks)

      Probably I might add those features in a month or two!

      • Have I overlooked something maybe but have you expanded this project now with a RTC. Looking forward o your response

  5. Right here is the perfect blog for everyone who really wants to
    find out about the Arduino. You understand a whole lot its
    almost hard to argue with you.
    Excellent stuff, just wonderful!

Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.