This article shows you how to create a temperature Arduino data logger. We’ll use the DHT11 to measure temperature, the real time clock (RTC) module to take time stamps and the SD card module to save the data on the SD card.
I want to turn my breadboard circuits into real boards and make my project look more professional, so I decided to Design my own coustom PCB for this project.
Designing the PCB
To design the circuit and PCB, we used EasyEDA which is a browser based software to design PCBs.
Designing the circuit works like in any other circuit software tool, you place some components and you wire them together.
Then, you assign each component to a footprint.
Having the parts assigned, place each component. When you’re happy with the layout, make all the connections and route your PCB.
Save your project and export the Gerber files.
Ordering the PCBs at PCBWay
This project is sponsored by PCBWay. PCBWay is a full feature Printed Circuit Board manufacturing service.
Turn your DIY breadboard circuits into professional PCBs – get 10 boards for approximately $5 + shipping (which will vary depending on your country).
Once you have your Gerber files, you can order the PCB. Follow the next steps.
2.Unzip the .zip folder and you should get DHT-sensor-library-master folder
3.Rename your folder from DHT-sensor-library-master to DHT
4.Move the DHT folder to your Arduino IDE installation libraries folder
5.Finally, re-open your Arduino IDE
Code
Copy the following code to your Arduino IDE and upload it to your Arduino board.
#include <SPI.h> //for the SD card module
#include <SD.h> // for the SD card
#include <DHT.h> // for the DHT sensor
#include <RTClib.h> // for the RTC
//define DHT pin
#define DHTPIN 2 // what pin we're connected to
// uncomment whatever type you're using
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);
// change this to match your SD shield or module;
// Arduino Ethernet shield and modules: pin 4
// Data loggin SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
const int chipSelect = 5;
// Create a file to store the data
File myFile;
// RTC
RTC_DS1307 rtc;
void setup() {
//initializing the DHT sensor
dht.begin();
//initializing Serial monitor
Serial.begin(9600);
// setup for the RTC
while(!Serial); // for Leonardo/Micro/Zero
if(! rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
else {
// following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
if(! rtc.isrunning()) {
Serial.println("RTC is NOT running!");
}
// setup for the SD card
Serial.print("Initializing SD card...");
if(!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
//open file
myFile=SD.open("DATA.txt", FILE_WRITE);
// if the file opened ok, write to it:
if (myFile) {
Serial.println("File opened ok");
// print the headings for our data
myFile.println("Date,Time,Temperature ºC");
}
myFile.close();
}
void loggingTime() {
DateTime now = rtc.now();
myFile = SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
myFile.print(now.year(), DEC);
myFile.print('/');
myFile.print(now.month(), DEC);
myFile.print('/');
myFile.print(now.day(), DEC);
myFile.print(',');
myFile.print(now.hour(), DEC);
myFile.print(':');
myFile.print(now.minute(), DEC);
myFile.print(':');
myFile.print(now.second(), DEC);
myFile.print(",");
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.println(now.day(), DEC);
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.println(now.second(), DEC);
myFile.close();
delay(1000);
}
void loggingTemperature() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius
float t = dht.readTemperature();
// Read temperature as Fahrenheit
//float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(t) /*|| isnan(f)*/) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//debugging purposes
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" *C");
//Serial.print(f);
//Serial.println(" *F\t");
myFile = SD.open("DATA.txt", FILE_WRITE);
if (myFile) {
Serial.println("open with success");
myFile.print(t);
myFile.println(",");
}
myFile.close();
}
void loop() {
loggingTime();
loggingTemperature();
delay(5000);
}
Getting the data from the SD card.
Let this project run for a few hours to gather a decent amount of data, and when you’re happy with the data logging period, shut down the Arduino and remove the SD from the SD card module.
Insert the SD card on your computer, open it, and you should have a DATA.txt file with the collected data.
You can open the data with a text editor, or use a spreadsheet to analyse and process your data.
Your blog is a true gem – thanks for your hard work!I’ve submitted my email for subscription but haven’t received the confirmation yet.