MOCKSTACKS
EN
Questions And Answers

More Tutorials









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.

Hardware Connection


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.

1 from lcd1602 import LCD1602 
2 from dht11 import * 
3 from machine import I2C, Pin, ADC 
4 from utime import sleep

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.

1 i2c = I2C(1, scl=Pin(7), sda=Pin(6), freq=400000) 
2 d = LCD1602(i2c, 2, 16) 
3 d.display() 
4 dht = DHT(18)

Next, we will read the values detected by the temperature and humidity sensor and save them in the variables “temp" and "humid" respectively.

1
temp , humid = dht.readTemp Humid()

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:

Temp: 25

Humid: 40

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:

1 d.setCursor(0,0) 
2 d.print("Temp: "+str(temp)) 
3 d.setCursor(0,1) 
4 d.print("Humid:"+str(humid))

The complete program is as follows:

1 from lcd1602 import LCD1602 
2 from dht11 import * 
3 from machine import 12C, Pin, ADC
4 from utime import sleep
5
6 i2c = 12C(1, scl=Pin(7), sda=Pin(6), freq=400000) 
7 d = LCD1602(i2c, 2, 16) 
8 d.display ( ) 
9 dht = DHT(18)
10
11 while True: 
12 temp, humid = dht.readTempHumid()#temp: humid:
13 sleep(1) 
14 d.clear()
15 d.setCursor(0,0) 
16 d.print("Temp: "+str(temp)) 
17 d.setCursor(0,1)
18 d.print("Humid:"+str(humid)) 
19 sleep (1)

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.



Conclusion

In this page (written and validated by ) you learned about Raspberry Display Temperature and Humidity Value with LCD . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry Adding an Alarm Function.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.