Temp. and hum. sensor project with LCD readout, logger and alarm - 3 versions

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. :slight_smile:

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)

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 - Arduino Playground - HomePage -

That said, a well done project.

Hi,

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 ?

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

Service Unavailable

Guru Meditation:

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

Anyone have ideas??

The new cache module of the forum had some hick-ups. Saw a "reboot" request recently.

Due to attachment problems I include the rest of the code for the i2c interface here.

//*******************************************************

Code for 1604 with i2c 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 <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>     // needed for LCD 16x2 display

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
 

LiquidCrystal_I2C       lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);


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 dht(DHTPIN, DHTTYPE);

void setup() {
  
  {
  lcd.begin (16,2,LCD_5x8DOTS);
 
 
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // init the backlight
 lcd.setBacklight(HIGH);     // Backlight on
}
  
  
  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.setBacklight(HIGH);     // Backlight on
    lcd.home();
    lcd.print("Hum: "); 
    lcd.print(h);
    lcd.print(" %");
    lcd.setCursor(0, 1);
    lcd.print("Temp: "); 
    lcd.print(t);
    lcd.print(" *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 i2c 2004 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 <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>     // needed for LCD 16x2 display

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
 

LiquidCrystal_I2C       lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);


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 dht(DHTPIN, DHTTYPE);

void setup() {
  
  {
  lcd.begin (16,2,LCD_5x8DOTS);
 
 
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); // init the backlight
 lcd.setBacklight(HIGH);     // Backlight on
}
  
  
  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.setBacklight(HIGH);     // Backlight on
    lcd.home();
    lcd.print("Hum: "); 
    lcd.print(h);
    lcd.print(" %");
    lcd.setCursor(0, 1);
    lcd.print("Temp: "); 
    lcd.print(t);
    lcd.print(" *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);
      } 
    }
  }
} 


//*********************************************

HJi,
Here's my Temperature/Humidity Arduino system displaying on the web:
https://ksduino.org/?devices&device_id=3322

Click on a Parameter Name to see a graph of history data.

I'm working on good examples of doing this stuff with the developer of the KSduino site. I've just started on it, but there is a code example here:
http://arduino-info.wikispaces.com/ksduino-tests1

Hi,

Looks very interesting. I also have an Arduino hooked up to my router, allowing me to access the conditions at home on my smartphone. It is just very plain and functional. I try to keep my power consumption down and also reduce the amount of data transferred to my smartphone.

But what you are doing could be very useful for some of our customers at work. I work in the Refrigeration and Air Conditioning industry and commercial systems that do that kind of thing are very expensive for smaller companies. This could be a cost effective alternative.

@maroelawerner
Please modify your post with the code, to apply code tags. For this use the # button just above the smileys. '(select only the code part and press # button thats all.)

Done.

Thank you for your help.

Thanks, much better readable !