Skip to content

Instantly share code, notes, and snippets.

@walkerjeffd
Created May 25, 2014 17:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save walkerjeffd/c1fe2d498476b4e6a9b0 to your computer and use it in GitHub Desktop.
Save walkerjeffd/c1fe2d498476b4e6a9b0 to your computer and use it in GitHub Desktop.
Arduino Temperature and Humidity Logger using Adafruit SD Shield and DHT22
// log DHT sensor readings to SD card
#include "DHT.h"
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL 60000 // mills between entries (reduce to take more/faster data)
#define ECHO_TO_SERIAL 1 // echo data to serial port
#define DHTPIN 3 // 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)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
// select 10 for adafruit shield
const int chipSelect = 10;
// create file to log to
File logfile;
void setup() {
Serial.begin(9600);
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);
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
// create a new file
char filename[] = "LOGGER00.CSV";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
Serial.println("could not create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
// connect to RTC
Wire.begin();
if (!RTC.begin()) {
logfile.println("RTC failed");
#if ECHO_TO_SERIAL
Serial.println("RTC failed");
#endif //ECHO_TO_SERIAL
}
logfile.println("millis,datetime,rh,temp_c,temp_f,sat_vapor_density,vapor_density");
#if ECHO_TO_SERIAL
Serial.println("millis,datetime,rh,temp_c,temp_f,sat_vapor_density,vapor_density");
#endif //ECHO_TO_SERIAL
dht.begin();
}
void loop() {
DateTime now;
delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
String dataString = "";
// fetch the time
now = RTC.now();
// log time
logfile.print(now.unixtime()); // seconds since 1/1/1970
logfile.print(",");
if (now.month() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.month(), DEC);
logfile.print("/");
if (now.day() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.day(), DEC);
logfile.print("/");
logfile.print(now.year(), DEC);
logfile.print(" ");
if (now.hour() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.hour(), DEC);
logfile.print(":");
if (now.minute() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.minute(), DEC);
logfile.print(":");
if (now.second() < 10) {
logfile.print(0, DEC);
}
logfile.print(now.second(), DEC);
#if ECHO_TO_SERIAL
Serial.print(now.unixtime()); // seconds since 1/1/1970
Serial.print(",");
if (now.month() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.month(), DEC);
Serial.print("/");
if (now.day() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.day(), DEC);
Serial.print("/");
Serial.print(now.year(), DEC);
Serial.print(" ");
if (now.hour() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.hour(), DEC);
Serial.print(":");
if (now.minute() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.minute(), DEC);
Serial.print(":");
if (now.second() < 10) {
Serial.print(0, DEC);
}
Serial.print(now.second(), DEC);
#endif //ECHO_TO_SERIAL
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity(); // relative humidity, %
float t_c = dht.readTemperature(); // air temp, degC
float t_f = t_c*9.0/5.0 + 32.0; // air temp, degF
float svd; // saturation vapor density, g/m3
float vd; // vapor density, g/m3
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t_c) || isnan(h)) {
Serial.println("Failed to read from DHT");
} else {
svd = 5.018 + 0.32321*t_c + 8.1847e-3*pow(t_c,2) + 3.1243e-4*pow(t_c,3);
vd = h/100*svd;
logfile.print(",");
logfile.print(h, 2);
logfile.print(",");
logfile.print(t_c, 2);
logfile.print(",");
logfile.print(t_f, 2);
logfile.print(",");
logfile.print(svd, 4);
logfile.print(",");
logfile.print(vd, 4);
#if ECHO_TO_SERIAL
Serial.print(",");
Serial.print(h, 2);
Serial.print(",");
Serial.print(t_c, 2);
Serial.print(",");
Serial.print(t_f, 2);
Serial.print(",");
Serial.print(svd, 4);
Serial.print(",");
Serial.print(vd, 4);
#endif //ECHO_TO_SERIAL
}
logfile.println();
#if ECHO_TO_SERIAL
Serial.println();
#endif // ECHO_TO_SERIAL
// flush to file
logfile.flush();
}
@preeyada
Copy link

preeyada commented Oct 1, 2014

have simulate error
i need library ,please

@male420
Copy link

male420 commented Nov 11, 2015

need to #include <SPI.h>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment