How to show time, temperature and humidity

The Arduino board itself is not able to provide information like temperature and humidity. Specific sensors are needed to read physical properties. This example uses the AM2302/DHT22 device, that provides calibrated digital signal output of the temperature and humidity.

Measuring time requires another device, an Real Time Clock (RTC) module, e.g. the DS1307 that provides real-time clock with 56 bytes of non-volatile RAM, full BCD code clock and calendar.

The measured properties are shown on a liquid crystal display (LCD), a SainSmart LCD2004 in this example, that can be controlled by an I²C interface. The following figure shows the main parts of this Arduino experiment:

LCD clock


Hardware set-up

Following hardware is required to build the experiment:
  • Arduino Uno board
  • Real Time Clock module based on DS1307
  • SainSmart LCD2004 (or equivalent)
  • AM2302/DHT22 device
  • resistor 5.1kΩ

Please proceed with the following connections, without any power connected:
  1. Connect GND(Arduino) together with GND(LCD), GND(RTC) and GND(AM2302/DHT22)
  2. Connect Vcc(Arduino) together with Vcc(LCD), Vcc(RTC) and Vcc(AM2302/DHT22)
  3. Connect A4(Arduino) together with SDA(RTC) and SDA(LCD)
  4. Connect A5(Arduino) together with SCL(RTC) and SCL(LCD)
  5. Connect digital pin 5(Arduino) to SDA(AM2302/DHT22) together with a pull-up resistor (5.1kΩ)

Confused about all three-letters acronyms? Here is a brief explanation: GND is the ground line, Vcc is the positive supply line, SDA is the serial data line, SCL is the serial clock line. This example uses serial I/O to demonstrate the easy wiring of serial protocols.

Now proceed with next section Software.

Software

Before writing software make sure to have installed the software development environment, downlodable from Arduino Software web site.
It may be needed to download some library if not already present: RTClib, LiquidCrystal_I2C and DHTlib (some googling will help you finding the appropriate libraries).

Libraries are required to manage the external devices. For this reason the sketch has to include the required libraries:
#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#include <dht.h>

The setup() function has to initialize libraries features:
void setup () {
    lcd.begin(20, 4);
    lcd.backlight(); // backlight on      
    lcd.setCursor(0,1);
    lcd.write("initiating");  
    Wire.begin();
    RTC.begin();
    if (! RTC.isrunning()) {
      RTC.adjust(DateTime(__DATE__, __TIME__));
    }
}

The loop() function does the job:
void loop () {
    DateTime now = RTC.now();
    lcd.clear();
    int chk = DHT.read22(DHT22_PIN);
    if (chk == DHTLIB_OK) {
      lcd.setCursor(0,2);
      lcd.print("Temp: ");
      lcd.print(DHT.temperature);
      lcd.setCursor(0,3);
      lcd.print("Humi: ");
      lcd.print(DHT.humidity);
    } else {
      lcd.setCursor(0,2);
      lcd.print("Error:");      
      lcd.print(chk);
    }
    
    lcd.setCursor(0,1);
    print2digits(now.hour());
    lcd.print(":");
    print2digits(now.minute());
    lcd.print(":");
    print2digits(now.second());     
     
    delay(1000);
}

void print2digits(int number) {
    if (number < 10)
    lcd.write('0');// Output leading zero
  lcd.print(number);
}

Download the complete sketch here, then connect your Arduino Uno board to your computer using a USB cable and upload the sketch to the Arduino board. Now the digital clock will start!

LCD Clock in action

Here is a picture of the LCD showing time, temperature and humidity:

LCD in action!
Share Share on Facebook Share on Twitter Bookmark on Reddit Share via mail
Creative Commons Attribution-Share Alike | Terms and conditions | Privacy policy