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
- Arduino UNO – read Best Arduino Starter Kits
- 1x Ethernet Shield
- 1x Micro SD Card
- 1x Temperature Sensor (I’m using the LM335Z)
- 1x Photoresistor
- 1x 1k ohm Resistor
- 1x 4700 ohm Resistor
- 1x Breadboard (or two)
- Jumper Cables
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
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");
}
}
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.
Cant wait to get home after work and try this project out. Looks cool.
Will let you know how i get on.
Regards
Mike
Hi Mike!
thanks so much for your feedback!
Let me know if this worked for you,
Rui
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..
Hi Alex,
You can find almost everything I use on my tutorials right here:
https://makeradvisor.com/tools/
Or simply go directly to amazon.
I hope this helps.
Have a nice day,
Rui
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
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.
That’s really weird.
Did the Arduino Code uploaded properly?
It said in the Arduino IDE: “Upload Complete”
Your Micro SD Card is inserted properly? Is it working?
Thanks for trying my project Tom!
Hello Rui,
First On your original code at the beginning I add Line #include
Than as Tom says above I have the same txt message as he …“error opening datalog.txt”
When I delete line pinMode(10, OUTPUT);
I get readings but I think they are not true …
Pls Help
You can’t deleted that line of code. Are you using a good temperature sensor? Is everything well connected?
Correction:
*First On your original code at the beginning I add Line #include SPI.h
Thanks!
Hello Tom try this
adverta.com.mk/test/arduino/code2.ino
I took Rui’s code and cadge something
I think its working
Looks good Tanja,
Thank you for sharing!
me too
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
Hello,
i get this message when i compilate: fatal error: SD.h: No such file or directory
What can i do?
Thanks
You have to install the SD.h library in your Arduino IDE folder.
How? Sorry i’m new with arduino.
Thanks
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!
Thank you!
-Rui