By connecting a DHT22 temperature and humidity sensor, a DS1307 RTC module and an SD card module to the Arduino Mega 2560 we can make a data logging system. The DHT22 sensor measures the temperature, the DS1307 gives us the date and time and this data is then written to the SD card over the SPI bus. The values from the DHT22 sensor and the DS1307 are comma separated so you can easily import the data in a spreadsheet. Was straightforward to get to work. The data is written every 3 seconds to the SD card, I just used the “delay” function. Should use the time from the DS1307 the next time. I tried adding an ethernet module to the mix but I can’t get the SD card and the ethernet module to work together on the Mega 2560, although separately they work.
The sketch:
/* http://www.bajdi.com SD card datalogger for Arduino Mega (this sketch will ONLY work with Arduino Mega) This example shows how to log data from a DHT22, to an SD card using the SD library. Time stamp is taken from a DS1307. * SD card attached to SPI bus as follows: ** MOSI - pin 51 ** MISO - pin 50 ** CLK - pin 52 ** CS - pin 53 */ #include <Wire.h> #include "RTClib.h" #include <dht.h> #include <SD.h> dht DHT; RTC_DS1307 RTC; #define DHTTYPE DHT22 // Sensor type #define DHT22_PIN 2 // DHT22 signal on pin 2 // 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 = 53; void setup() { Serial.begin(9600); Wire.begin(); RTC.begin(); RTC.adjust(DateTime(__DATE__, __TIME__)); 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(53, 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."); } void loop() { DateTime now = RTC.now(); int chk = DHT.read22(DHT22_PIN); File dataFile = SD.open("datalog.csv", FILE_WRITE); // if the file is available, write to it: if (dataFile) { dataFile.print(now.day(), DEC); dataFile.print('/'); dataFile.print(now.month(), DEC); dataFile.print('/'); dataFile.print(now.year(), DEC); dataFile.print(" , "); dataFile.print(now.hour(), DEC); dataFile.print(':'); dataFile.print(now.minute(), DEC); dataFile.print(" , "); dataFile.println((float)DHT.temperature); dataFile.close(); // print to the serial port too: Serial.println(DHT.temperature); } // if the file isn't open, pop up an error: else { Serial.println("error opening datalog.csv"); } delay(3000); }
thanks for dht22 source, good work
Hi, I have an ethermega and there is no way I can get this code to work. I can not get the DHT22 and onboard SD card to work together. Do you have any ideas?
What’s an ethermega? Can you get the DHT22 and SD card to work separately? I would try that first.
Hi, I now have it working. The Ethermega 2560 is from freetronics and is an arduino compatible with ethernet, sd card, and usb as one unit. I am getting confused with the pins that are being “shared” with these built in components and was trying to use pin 53 as a data pin for the DHT22. I rewired everything as you suggested and was ok. I now need to find a complete list of pins and how they are used for the ethermega.
Excellent work! Worked almost out of the box. I will use this to monitor the Temp and RV in a small breeding greenhouse with grafted tomatoes.
Many thanks!