Arduino Temperature and Humidity sensor

This project will show the temperature in C and % of relative humidity of the ambiant air on an LCD display. The program can easily be modified to display the temperature in F, just by calling the function getTemperatureFInt() instead of getTemperatureCInt() For this project I am using the following components:

Shopping list

  • 1 Arduino costs about $24.00
  • 1 DHT11 or DHT22 Humidity and Temperature Sensor DHT11 costs $2.50, DHT22 $9.00 In this prototype I am using a DHT11
  • 1 OrditalMatrix LK162-12 Serial LCD Display cost around $24.00. I got mine from an old computer. Having to manage the LCD display through a serial connection is convenient, because it only uses 2 pins on the Arduino. 1 pin for transmission and 1 pin for reception. I only use 1 pin to transmit the message to the display
  • 1 Breadboard. If you are planning to run the temperature and humidity sensor over the long term, my advice is to use a ProtoShield where you can solder the components
  • A few wires, a couple of resistors and batteries

The DHT22/DHT11 is a low cost humidity and temperature sensor with a single wire digital interface. The sensor is calibrated and doesn't require extra components.

Program

First, the program initializes the DHT22 sensor and set up the digital pin to which the sensor will be connected. In this program the sensor is connected to the digital pin #4. Then the program creates an object SoftwareSerial and assigns the pin #2 for data transmission.

We have also two helper functions clearLCD() and cursorOff() to manage some of the display capabilities. The hexadecimal codes in these functions are from the LK162-12 manual. I invite you to take a look at the manual to learn about all the fun and cool capabilities of that tiny display.

The standard Arduino setup() function sets the speed of the serial connection to the LCD, clears the display and turns off the cursor. We don't want a cursor moving around and flickering everytime the temperature is updated.

Finally we have the main loop() function. This function reads the data from the sensor, clears the screen, prints a message with the temperature and humidity and waits for 10 seconds before reading the data again. The switch block also displays error messages if necessary.

#include <SoftwareSerial.h>
#include <DHT22.h>

#define DHT22_PIN 4
DHT22 sensor(DHT22_PIN);

/*
 * since the LCD does not send data back to the Arduino, we only
 * define the tx_Pin
 */
#define tx_PIN 2
SoftwareSerial LCD = SoftwareSerial(0, tx_PIN);

const int LCDdelay=10;  // conservative, 2 actually works

void clearLCD() {
  LCD.write(0xFE);   // command flag
  LCD.write(0x58);   // clear command.
  delay(LCDdelay);
}

void cursorOff() {
  LCD.write(0xFE);
  LCD.write(0x54);
  delay(LCDdelay);
}

void setup() {
  pinMode(tx_PIN, OUTPUT);
  LCD.begin(9600);
  delay(1000);        // the LCD display take some time to initialize
  clearLCD();
  cursorOff();
  LCD.print("Starting sensor");
}


void loop()
{
  DHT22_ERROR_t errorCode;
  errorCode = sensor.readData();
  clearLCD();
  switch(errorCode)
  {
    case DHT_ERROR_NONE:
      char buf[128];
      sprintf(buf, "    Temp: %hi.%01hiC\nHumidity: %i.%01i%%",
              sensor.getTemperatureCInt()/10, abs(sensor.getTemperatureCInt()%10),
              sensor.getHumidityInt()/10, sensor.getHumidityInt()%10);
      LCD.print(buf);
      break;
    case DHT_ERROR_CHECKSUM:
      LCD.print("Check sum error");
      break;
    case DHT_BUS_HUNG:
      LCD.print("BUS Hung");
      break;
    case DHT_ERROR_NOT_PRESENT:
      LCD.print("Not Present");
      break;
    case DHT_ERROR_ACK_TOO_LONG:
      LCD.print("ACK time out");
      break;
    case DHT_ERROR_SYNC_TIMEOUT:
      LCD.print("Sync Timeout");
      break;
    case DHT_ERROR_DATA_TIMEOUT:
      LCD.print("Data Timeout");
      break;
    case DHT_ERROR_TOOQUICK:
      LCD.print("Polled to quick");
      break;
  }
  delay(10000);       // We update poll the sensor every 10 sec
}

Future work

I am planning to plug an Arduino Ethernet shield to send the collected data to pachube.com to draw and watch temperature and humidity amplitudes. I am also planning to add an RTC module to display the time and date.


Comments !