Hi Folks,
Hopefully you all can help me. I'm using arduino to create a Temp/Humidity sensor that prints data onto an LCD screen and also logs it onto an SD card. This will allow the Museum I work for to both instantly see Temperature/Humidity, and collect data to look at long-term trends.
For equipment, I have:
an Arduino UNO
ethernet shield w/ SD card
DHT22 sensor
LCD 1602 module w/ pin header
10K Potentiometer
all running through a breadboard
I am starting to wrap my head around how the code works, and how wiring works, and have used many tutorials here and elsewhere, so thanks for that!
Here's my problem...I can get the DHT22 sensor to print to LCD, and I can get it to save to an SD card, but I cannot get it to do both at the same time. What usually happens is that when I initialize the arduino, and open up the serial monitor (to check that things are saving), I see temp and humidity data on the serial monitor (and on the card, when I take it out to check), but the LCD screen says "DDDDDDDDDDDD" and then goes blank.
I've printed my code below. Any help would be much appreciated.
#include "DHT.h"
#include "SD.h"
#include "LiquidCrystal.h"
#include "SPI.h"
#define DHTPIN A0#define DHTTYPE DHT22
const int chipSelect = 4;
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);DHT sensor(DHTPIN, DHTTYPE);
void setup(){
lcd.begin(16,2);
Serial.begin(9600);
sensor.begin();
Serial.print("Initializing SD card...");
pinMode(4,OUTPUT);if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");}
void loop()
{
float humidity = sensor.readHumidity();
float temperature_C = sensor.readTemperature();
float temperature_F = sensor.readTemperature(true);
if (isnan(humidity) || isnan(temperature_C) || isnan(temperature_F)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//print to LCD screen
lcd.clear();
lcd.setCursor(0,1);
lcd.print("T = ");
lcd.print(temperature_F);
lcd.setCursor(0,1);
lcd.print("H = ");
lcd.print(humidity);
delay(1000);File sdcard_file = SD.open("data.txt", FILE_WRITE);
// if the file is available, write to it:
if (sdcard_file) {
sdcard_file.print(" Temperature in F: ");
sdcard_file.print(temperature_F);
sdcard_file.print(" Humidity: ");
sdcard_file.print(humidity);
sdcard_file.println("");
sdcard_file.close();// print to the serial port too:
Serial.print(" Temperature in F: ");
Serial.print(temperature_F);
Serial.print(" Humidity: ");
Serial.print(humidity);
Serial.println("");
delay(1000);
}
// if the file isn't open, pop up an error:else {
Serial.println("error opening datalog.txt");
}
}