This is an old revision of the document!
In this scenario, you will present temperature and humidity as read by the attached DHT22 sensor, on the LCD screen.
Beginners
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, DHT22 sensor (white) or DHT11 (blue), 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>
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.
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.
You should be able to see temperature and humidity readings on the LCD display using the video stream.
There are no special steps to be performed.
Include LCD and DHT sensor driver libraries:
#include <LiquidCrystal_I2C.h> #include <Adafruit_Sensor.h> #include <DHT.h>
Instantiate software controler 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,DHT22,50);
Declare a buffer for sprintf function:
char buffer [10];
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 ...
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.
Observe temperature and humidity readings on the LCD. Temperature ranges between 20-30C while humidity between 35-60%Rh.