DHT11 humidity and temperature sensor is available as a sensor and as a module. The difference between this sensor and module is the pull-up resistor and a power-on LED.
DHT11 is a relative humidity sensor. To measure the surrounding air this sensor uses a thermistor and a capacitive humidity sensor.For measuring temperature this sensor uses a Negative Temperature coefficient thermistor, which causes a decrease in its resistance value with increase in temperature.
The temperature range of DHT11 is from 0 to 50 degree Celsius with a 2-degree accuracy. Humidity range of this sensor is from 20 to 80% with 5% accuracy. The sampling rate of this sensor is 1Hz .i.e. it gives one reading for every second. DHT11 is small in size with operating voltage from 3 to 5 volts. The maximum current used while measuring is 2.5mA.
DHT11 sensor has four pins- VCC, GND, Data Pin and a not connected pin. A pull-up resistor of 5k to 10k ohms is provided for communication between sensor and micro-controller.As amodule it has only 3 pins , pull up resistor provided on board.
Connect DATA pin of DHT11 to D2 of Arduino , Vcc to 5v & Gnd to Gnd.
Open NEXTION EDITOR and start a new project with .HMI file name.
Generate a FONT file and add to the project.
Click on Picture tab at bottom left corner and then + symbol to import a back ground picture.This picture resolution must match that of your NEXTION model
In this demo I’m using 4024T032 ( 400 X 240 pixel 3.2 inch size)
Use IRFANVIEW or INKSCAPE to match the resolution 400 x 240 of the image.
Click on PICTURE tool .It appears p0 on canvas.
At the right side window you can set the attributes for this object.Leave the object name as p0.
Click browse against pic attribute and select the image to be loaded as back ground.
Now the image appears on canvas.
Add 2 TEXT objects .One for Temperature and the other for Humidity.
As Nextion editor cannot handle fraction numbers , we use Text instead of Number object.
Following are the attribute settings for these 2 Text objects.
t0 objname can be changed to Temperature
vscope to global.All objects that is controlled through SERIAL to be set to global.
sta attribute by default is solid color.Change this to crop image.
Also select the browse against picc attribute and select the back ground image.
This creates a Transparent effect to the object.
Text color can be changed by the pco attribute. 65535 means White & 0 means Black.
You can click on Select against pco to select a color.
Same way repeat for the t1 object Humidity.
Next add on 4 more Text objects for display purpose.You need to change the txt attribute to change the display name .
txt – to change display name
pco – to change color of text.
sta -crop image to give transparent effect so that background image is visible through this object.
Click on DEBUG to start the Simulator.
You can test the display by sending STRING Commands.
temperature.txt=”32.0”
changes the Temperature display to 32.0.
Note the usage of double quotes for data to be displayed.While typing the code the interpreter of Nextion editor gives options to select (intellisense).
The commands can be sent as STRING from Arduino and to be followed by 3 bytes of 0xFF.
Upload the following sketch on to Arduino.
Here we use the library for DHT Sensor by including the header file DHT.h
We define the DHT data pin connection as 2 (D2 of Arduino) and type as DHT11.
The variables temperature and humidity are initialized with float literal constants.
Reading of sensor is by calling functions readHumidity() & readTemperature() from the Class dht.
The assigned data is sent to Nextion as a STRING command.
Sending data.
Data to Nextion should be sent in the following format:
-
- String command
=”humidity.txt=\””+String(humidity,1)+”\””;
- name of the receiving element humidity
- sign “.”
- field name of the receiving element txt
- sign “=”
- data, and if it is String, then in quotation marks, and if it is digital information, then four bytes of data
- three bytes 0xff
- Notice the use of ESCAPE character backslash \ before double quote “.
- This is how to send double quotes from Arduino Serially.
Nextion does not work with float. So all to be sent as – String
To upload the .tft file on to Nextion we use USB-TTL device.Connect Nextion to USB-TTL
Tx to Rx
Rx to Tx
Gnd – Gnd
5v to 5v
Click on UPLOAD button and select the COM port of USB device and a faster baudrate.
Once uploaded , remove USB device and connect Arduino with Nextion with same configuration as above.
The result is displayed on NEXTION DISPLAY.
The DHT sensor requires 1 second refresh rate for each reading.This is provided by 1 sec delay in Arduino code.
VIDEO :