Go Down

Topic: Temp. and hum. sensor project with LCD readout, logger and alarm - 3 versions (Read 2 times) previous topic - next topic


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 https://github.com/adafruit/DHT-sensor-library .

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:
Code: [Select]


// 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.


#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


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:
lcd.print("card initialized.");

 lcd.println("DHTxx test!");
 lcd.print("Home Weather");
 lcd.setCursor(0, 1);


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.print("Hum: ");
   lcd.print(" %");
   lcd.setCursor(0, 1);
   lcd.print("Temp: ");
   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) {
 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++) {      
 digitalWrite(beep, LOW);    // turn the Buzzer off
for (int y = 0; y <= ds; y++) {


Code for 1604  and 2004 with i2c interface is in a reply, due to post length restriction.


More important than humidity you should monitor the dewpoint as that is the condition you get molds and wet electronics etc.

You can calculate it from temperature and humidity. For a function see - http://playground.arduino.cc//Main/DHT11Lib -

That said, a well done project.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)



The dewpoint is the temperature at which a sample of air will reach 100% humidity and will be unable to hold any more moisture. So what you really need is to monitor the temperature of the surface that is prone to mould growth. If you can keep that temperature above the dewpoint you will have no problems. Adding another sensor to monitor the surface temperature would help. Or another solution is to stick the DHT onto that surface with a good thermal bond. I found that after using my project for a while I soon found the conditions were mould growth starts and I am now able to avoid those conditions.

If you look at the size of the code you will realise that there is almost no memory left on a ATmega 328. So not much room left for more code. On an Arduino Mega that would not be a problem.

Thank you for your comments. Very much appreciated.   


cannot download the sketches, how much (flash)memory is free ?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)


Hi, Cannot download either. I get:
---------------------( COPY )---------------------
Error 503 Service Unavailable

Service Unavailable

Guru Meditation:

XID: 1080076790
-----------------( END COPY )----------------------

Anyone have ideas??
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Go Up