Go Down

Topic: Temperature and humidity sensor project with LCD readout, logger and alarm (Read 2079 times) previous topic - next topic

Dec 14, 2012, 11:23 pm Last Edit: Jan 12, 2013, 07:01 pm by maroelawerner Reason: 1
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.

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 .


Hello, This sounds like a very nice example project.

Would you please share the code with us here??

Reading code is one of the best ways to understand how to do things with Arduino that are more complex and interesting.
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Dec 17, 2012, 10:26 pm Last Edit: Dec 18, 2012, 03:22 am by Coding Badly Reason: 1

I did share the code. it is attached and has been downloaded 7 time as I write this.  

Examples are a very good way to learn things. It was a bit of a challenge to get the different parts to work together.

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)

// 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++) {

Moderator edit: [code] [/code] tags added.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131