Raspberry Display Temperature and Humidity Value with LCD
In this project, we will use the Shield to connect both the temperature and humidity sensor and LCD module, and use the Pico to display the temperature and humidity value obtained by the sensor on the LCD. First of all, we need to use the LCD to display the value of temperature and humidity.
In this project, the electronic hardware we need to use is as follows:
• Raspberry Pi Pico
• Grove Shield for Pi Pico
• Grove - Temperature & Humidity Sensor
• Grove - 16 x 2 LCD
Connect Pico and the Shield, use a Grove data cable to connect the LCD to I2C1, and connect the Temperature & Humidity Sensor to D18.
Write a Program
In this project, we need to use not only the third-party function library lcd1602 which has been saved in Pico, but also a new library dht11.py(3kB) Download the library file in the same way as in previous lessons and save it in Pico. After that, we can start to import the libraries that we need to use.
Recall how we defined the I2C pin in the last lesson. To achieve I2C, we must define both the SCL and SDA data lines and mark the data type, number of lines and the number of characters in each line. Then, we add the pin definitions for the temperature and humidity sensor.
Next, we will read the values detected by the temperature and humidity sensor and save them in the variables “temp" and "humid" respectively.
Finally, we will display the read values on the LCD. There are two values, one each for temperature and humidity, so their labels should be displayed on the screen at the same time to distinguish them. The effect is as follows:
The characters of “Temp" and "Humid" can be directly displayed on the LCD, while the values read by “temp" and “humid" cannot be directly displayed on the LCD. So, we need to use the “str()" function to convert them into the String data type. It should also be noted that for temperature and humidity to be displayed on different lines, the coordinate position needs to be set as shown below:
The complete program is as follows:
Use a USB cable to connect Pico to the computer, click the “run" button to save the program to any location, and you can see program in action.