Arduino Temperature Logger With Sd Card
How to create a temperature Arduino data logger. We’ll use the DHT11 to measure temperature
Components and supplies
1
Arduino UNO
1
DS3231M - ±5ppm, I2C Real-Time Clock
1
Flash Memory Card, SD Card
1
DHT11 Temperature & Humidity Sensor (4 pins)
Apps and platforms
1
Arduino IDE
Project description
Code
Code
arduino
Code
1#include <SPI.h> //for the SD card module 2#include <SD.h> // for the SD card 3#include <DHT.h> // for the DHT sensor 4#include <RTClib.h> // for the RTC 5 6//define DHT pin 7#define DHTPIN 2 // what pin we're connected to 8 9// uncomment whatever type you're using 10#define DHTTYPE DHT11 // DHT 11 11//#define DHTTYPE DHT22 // DHT 22 (AM2302) 12//#define DHTTYPE DHT21 // DHT 21 (AM2301) 13 14// initialize DHT sensor for normal 16mhz Arduino 15DHT dht(DHTPIN, DHTTYPE); 16 17// change this to match your SD shield or module; 18// Arduino Ethernet shield and modules: pin 4 19// Data loggin SD shields and modules: pin 10 20// Sparkfun SD shield: pin 8 21const int chipSelect = 4; 22 23// Create a file to store the data 24File myFile; 25 26// RTC 27RTC_DS1307 rtc; 28 29void setup() { 30 //initializing the DHT sensor 31 dht.begin(); 32 33 //initializing Serial monitor 34 Serial.begin(9600); 35 36 // setup for the RTC 37 while(!Serial); // for Leonardo/Micro/Zero 38 if(! rtc.begin()) { 39 Serial.println("Couldn't find RTC"); 40 while (1); 41 } 42 else { 43 // following line sets the RTC to the date & time this sketch was compiled 44 rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); 45 } 46 if(! rtc.isrunning()) { 47 Serial.println("RTC is NOT running!"); 48 } 49 50 // setup for the SD card 51 Serial.print("Initializing SD card..."); 52 53 if(!SD.begin(chipSelect)) { 54 Serial.println("initialization failed!"); 55 return; 56 } 57 Serial.println("initialization done."); 58 59 //open file 60 myFile=SD.open("DATA.txt", FILE_WRITE); 61 62 // if the file opened ok, write to it: 63 if (myFile) { 64 Serial.println("File opened ok"); 65 // print the headings for our data 66 myFile.println("Date,Time,Temperature ºC"); 67 } 68 myFile.close(); 69} 70 71void loggingTime() { 72 DateTime now = rtc.now(); 73 myFile = SD.open("DATA.txt", FILE_WRITE); 74 if (myFile) { 75 myFile.print(now.year(), DEC); 76 myFile.print('/'); 77 myFile.print(now.month(), DEC); 78 myFile.print('/'); 79 myFile.print(now.day(), DEC); 80 myFile.print(','); 81 myFile.print(now.hour(), DEC); 82 myFile.print(':'); 83 myFile.print(now.minute(), DEC); 84 myFile.print(':'); 85 myFile.print(now.second(), DEC); 86 myFile.print(","); 87 } 88 Serial.print(now.year(), DEC); 89 Serial.print('/'); 90 Serial.print(now.month(), DEC); 91 Serial.print('/'); 92 Serial.println(now.day(), DEC); 93 Serial.print(now.hour(), DEC); 94 Serial.print(':'); 95 Serial.print(now.minute(), DEC); 96 Serial.print(':'); 97 Serial.println(now.second(), DEC); 98 myFile.close(); 99 delay(1000); 100} 101 102void loggingTemperature() { 103 // Reading temperature or humidity takes about 250 milliseconds! 104 // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) 105 // Read temperature as Celsius 106 float t = dht.readTemperature(); 107 // Read temperature as Fahrenheit 108 //float f = dht.readTemperature(true); 109 110 // Check if any reads failed and exit early (to try again). 111 if (isnan(t) /*|| isnan(f)*/) { 112 Serial.println("Failed to read from DHT sensor!"); 113 return; 114 } 115 116 //debugging purposes 117 Serial.print("Temperature: "); 118 Serial.print(t); 119 Serial.println(" *C"); 120 //Serial.print(f); 121 //Serial.println(" *F\ "); 122 123 myFile = SD.open("DATA.txt", FILE_WRITE); 124 if (myFile) { 125 Serial.println("open with success"); 126 myFile.print(t); 127 myFile.println(","); 128 } 129 myFile.close(); 130} 131 132void loop() { 133 loggingTime(); 134 loggingTemperature(); 135 delay(5000); 136}
Downloadable files
Schematics
Schematics
Schematics

Schematics
Schematics
Schematics

Comments
Only logged in users can leave comments