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.

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 build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our 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..

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

    Reply
    • 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

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

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

    Reply
    • 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!

      Reply
  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!

    Reply

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.