Lesson 7: LCD display & temperature and humidity sensor

Objective

Using Grove - LCD 16*2 to display temperature and humidity data from Grove - Temperature and humidity sensor

Equipment requirements

Prepare:

  • MicroUSB cable
  • Raspberry Pi 3 Model B
  • Computer
  • Grove Base Hat
  • Grove wire
  • Grove - LCD display 16*2
  • Grove - Temperature and humidity sensor

Connecting equipment

Step 1 Connect Grove - 16*2 LCD display to I2C,Grove - Temperature and humidity sensor to port D5.

Step 2 Connect Base Hat to Raspberry Pi.

Step 3 Connect the Raspberry Pi to a power source using a micro USB cable.

Programming

Step 1: Enter the following commands to create a python file.

  1. cd grove.py
  2. nano lesson_7.py

Step 2: Copy the code below.

  1. #!/usr/bin/env python
  2.  
  3. import time
  4.  
  5. from grove.grove_temperature_humidity_sensor import DHT
  6. from grove.display.jhd1802 import JHD1802
  7.  
  8. def main():
  9. # Grove - 16x2 LCD(White on Blue) connected to I2C port
  10. lcd = JHD1802()
  11.  
  12. # Grove - Temperature&Humidity Sensor connected to port D5
  13. sensor = DHT('11', 5)
  14.  
  15. while True:
  16. humi, temp = sensor.read()
  17. print('temperature {}C, humidity {}%'.format(temp, humi))
  18.  
  19. lcd.setCursor(0, 0)
  20. lcd.write('temperature: {0:2}C'.format(temp))
  21.  
  22. lcd.setCursor(1, 0)
  23. lcd.write('humidity: {0:5}%'.format(humi))
  24.  
  25. time.sleep(1)
  26.  
  27. if __name__ === '__main__':
  28. main()

Step 3:Uruchomprogram

  1. sudo chmod +x lesson_7.py
  2. sudo ./lesson_7.py

If everything goes well, the LCD screen should show the current temperature and humidity values.

  1. pi@raspberrypi:~/grove.py $ sudo ./lesson_7.py
  2. temperature 23C, humidity 16%
  3. temperature 22C, humidity 17%
  4. temperature 22C, humidity 17%
  5. ^CTraceback (most recent call last):
  6. File "./lesson_7.py", line 28, in
  7. main()
  8. File "./lesson_7.py", line 25, in main
  9. time.sleep(1)
  10. KeyboardInterrupt
  11. pi@raspberrypi:~/grove.py $

Table of contents

Botland.store - shop for makers!