B2: Presenting temperature and humidity on the LCD

In this scenario, you will present temperature and humidity as read by the attached DHT11 sensor, on the LCD screen.

Target group

Beginners

Prerequisites

You need to know, how to print and position text on the LCD display. Use LCD I2C library to control it:

#include <LiquidCrystal_I2C.h>

The temperature and humidity sensor is all-in-one, DHT11, connected to the pin D4. Observe your camera to check which sensor is equipped with your lab and consult node documentation.
To read data you may use a dedicated library (libraries):

#include <Adafruit_Sensor.h>
#include <DHT.h>

Scenario

Once you initialise sensor and LCD display, read sensor data (temperature and humidity, mind they're float values, not integers) and then display them in the loop. Give each loop execution some 5-10s delay.

Do not try to read temperature and humidity too frequently. Once every 5s is quite enough both for information and safe enough to not let your readings race with the communication protocol. If you read too frequent, your sensor may not be able to deliver data on time and you will obtain a 'nan' (not a number) instead of temperature and humidity.

The first line of the LCD should display: “Temperature is:
The second line should provide temperature within the ”NN.N C” form, in Celcius.
The third line should display: “Humidity is:
Finally, the fourth should present relative humidity: ”NN.N%Rh

You will use sprintf function to format string and convert from float to string.

Result

You should be able to see temperature and humidity readings on the LCD display using the video stream.

Start

There are no special steps to be performed.

Steps

Step 1

Include LCD and DHT sensor driver libraries:

#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
Step 2

Instantiate software controller components for the LCD display and DHT sensor:

LiquidCrystal_I2C lcd(0x3F,20,4);  // set the LCD address to 0x3F for nodes 1 through 5, 8 and 9
//LiquidCrystal_I2C lcd(0x27,20,4); // for nodes 10 and 11 only!
                                   // for a 20 chars and 4 line display
DHT dht(DHTpin, DHT11, 50); 
Step 3

Declare a buffer for sprintf function:

char buffer [10];
Step 4

Initialize LCD and DHT sensor:

...
  lcd.init(D2,D1);     // initialize the lcd 
  lcd.backlight();
  lcd.home();
...
  dht.begin();
  delay(1000);         // give it a second to let the sensor initialize
...
Step 5

Read in the loop:

...
  float hum = dht.readHumidity();
  float temp = dht.readTemperature();
...

To convert float into the string with formatting use sprintf function:

...
  sprintf(buffer,"%2.1f C",temp);
...
  sprintf(buffer,"%2.1f%%Rh",hum);
...
  delay(5000);
...
sprintf uses number of wildcards that are rendered with data. Refer to the c/c++ documentation on sprintf. Here
%2.1f

means: having float number render it to string using two digits before decimal point and one after. Temperature in our lab is always above 10C.

%%

is an escape character to print a

%

(percent) symbol.

delay(time)

is measured in milliseconds.

Result validation

Observe temperature and humidity readings on the LCD. The temperature in our lab usually ranges between 20-30C (not, we observed over 30C during really hot summer!) while humidity is usually between 30-60%Rh.

en/iot-open/remotelab/sut/generalpurpose2/b2.txt · Last modified: 2021/10/28 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0