I needed an instrument to monitor the humidity levels in the house we had just moved into. I first tested it on my Aduino Duemilanove 328. Once I had got it working properly I was not willing to let it occupy my Arduino for a long period of time and I end up having nothing to play with. So I migrated it onto a strip-board. The Atmega 328 is in a socket, for easy removal. This way I simply plug it back into the Arduino and modify code if needed.
The first one I built, was with a standard 1604 display. Then I got a working 1604 display with an i2c interface. I decided to change the code to accommodate that interface. I then got a 2004 as well and found it works well with that as well. Just hook up the i2c interface to the SDA and SDL connections on the Arduino and power. SDA and SDL are pins Analog 4 and 5 on the Dueilanove and pins 20 and 21 on the Mega.
It constantly displays the current humidity and temperature.
It logs the temperature and humidity onto a SD card. I might add a time stamp to the log and allow for log interval adjustment in future.
When the humidity exceeds the lower alarm limit (hla), the buzzer starts to with short beeps with long silent intervals. As the humidity approaches the high alarm level (hha), it beeps for longer with shorter intervals of silence.
This is my first attempt at a project like this. Suggestions and Questions are welcome.
The DHT Libraries I used come from GitHub - adafruit/DHT-sensor-library: Arduino library for DHT11, DHT22, etc Temperature & Humidity Sensors .
I have modified this post and included the code, because the attaching of files seems to be problematic at the moment.
Enjoy.
Code for normal 1604 interface:
//*******************************
// Temperature and Humidity display and logger
// Code is patched and modified from various sources (Thank you original contributors)
/*
I made this instrument to keep the humidity in my house in check. Started off with just a sensor and display.
This was ignored most the time and therefore a buzzer was added. This starts giving a short beep as the humidity exceeds the "hla".
The more it exceeds it the longer and more frequent the beeps become. When the humidity reaches "hha" it beeps continuously.
For record purposes a SD card logs the temperatures and humidity.
This project started off on an Arduino Duemilanove 328 and then transferred to a strip-board.
I used Arduino IDE 1.0.1
Feel free to contact me for any suggestions and questions.
maroelawerner@gmail.com
*/
#include <SD.h> // needed for SD card
const int chipSelect = 4;
#include <LiquidCrystal.h> // needed for LCD 16x2 display
int beep = 10; // pin for piezo buzzer
int hd = 0; // initialize beep delay
int ds = 0; // initialize beep interval
int hum = 0; // initialize humidity
int hla = 70; // High humidity Low level Alarm
int hha = 20; // High humidity High level Alarm
File root;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(9, 8, 6 , 5, 3, 2); // connect rs -> pin 9, enable -> pin 8, d4 -> pin 6, d5 -> pin 5, d6 -> pin 3, d7 -> pin 2
#include "DHT.h"
#define DHTPIN 7 // pin for data from DHT
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// DHT Sensor connection
// 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);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
pinMode(beep, OUTPUT); // define pin as output
if (!SD.begin(chipSelect)) {
// don't do anything more:
return;
}
lcd.print("card initialized.");
delay(3000);
lcd.println("DHTxx test!");
lcd.clear();
lcd.print("Home Weather");
lcd.setCursor(0, 1);
lcd.print("display");
delay(2000);
dht.begin();
}
void loop() {
// 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();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(t) || isnan(h)) {
lcd.println("Failed to read from DHT"); // display error message if no data from sensor is available
} else {
lcd.home();
lcd.print("Hum: ");
lcd.print(h);
lcd.print(" %");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(t);
lcd.println(" *C ");
int sensor = h;
String dataString = "";
dataString += String(sensor);
dataString += ",";
sensor = t;
dataString += String(sensor);
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("datalog.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println(dataString);
dataFile.close();
}
if (h > hla);{ // calculate duration and interval of beep
hum = h;
hd = hum - hla; // duration of beep
ds = hha - hd; // interval of beep
ds = ds * 10;
digitalWrite(beep, HIGH); // turn the Buzzer on
for (int x = 0; x <= hd; x++) {
delay(100);
}
Serial.println();
digitalWrite(beep, LOW); // turn the Buzzer off
for (int y = 0; y <= ds; y++) {
delay(100);
}
}
}
}
//*******************************************************
Code for 1604 and 2004 with i2c interface is in a reply, due to post length restriction.
DHTmonitor_logger1_i2c_1602.pde (4.1 KB)
DHTmonitor_logger1_i2c_2004.pde (4.2 KB)
DHTmonitor_logger.pde (3.85 KB)