Lesson 8: LCD Display & Moisture Sensor & Buzzer

Objective

Use Grove - LCD16 * 2 to display the current moisture level. When the moisture level is "wet", Grove - Buzzer will notify you.

Required equipment

Prepare:

  • microUSB cable
  • Raspberry Pi
  • Computer
  • Grove Base Hat
  • Grove wire
  • Grove - LCD display 16*2
  • Grove - Moisture sensor
  • Grove - Buzzer

Connecting equipment

Step 1 Connect Grove - 16x2 LCD display to I2C port, Grove - Moisture Sensor to A0 port and Grove - Buzzer to PWM port on Grove Base Hat.

Step 2 Connect Base Hat to Raspberry Pi.

Step 3 Use the microUSB to connect Raspberry Pi to your computer.

Programming

Note

Make sure to clone the python.py repository library on your Raspberry Pi.

Step 1: Enter the following commands to create a Python file

  1. cd grove.py
  2. nano lesson_8.py

Step 2:Skopiujthe following code

  1. #!/usr/bin/env python
  2.  
  3. import time
  4. from mraa import getGpioLookup
  5. from upm import pyupm_buzzer as upmBuzzer
  6.  
  7. from grove.grove_moisture_sensor import GroveMoistureSensor
  8. from grove.lcd.sh1107g import JHD1802
  9.  
  10. def main():
  11. # Grove - 16x2 LCD(White on Blue) connected to I2C port
  12. lcd = JHD1802()
  13.  
  14. # Grove - Moisture Sensor connected to port A0
  15. sensor = GroveMoistureSensor(0)
  16.  
  17. # Grove - Buzzer connected to PWM port
  18. buzzer = upmBuzzer.Buzzer(getGpioLookup('GPIO12'))
  19.  
  20. while True:
  21. mois = sensor.moisture
  22. if 0 <= mois and mois < 300:
  23. level = 'dry
  24. elif 300 <= mois and mois < 600:
  25. level = 'moist'
  26. Another:
  27. level = 'wet'
  28. buzzer.playSound(upmBuzzer.BUZZER_DO, 200000)
  29.  
  30. print('moisture: {}, {}'.format(mois, level))
  31.  
  32. lcd.setCursor(0, 0)
  33. lcd.write('moisture: {0:>6}'.format(mois))
  34.  
  35. lcd.setCursor(1, 0)
  36. lcd.write('{0:>16}'.format(level))
  37.  
  38. time.sleep(1)
  39.  
  40. if __name__ === '__main__':
  41. main()

Step 3:Uruchomprogram

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

If everything goes well, the moisture level will be visible on the LCD. The Buzzer will send out warnings when the moisture level reaches "wet".

  1. pi@raspberrypi:~/grove.py $ sudo ./lesson_8.py
  2. moisture: 0, dry
  3. moisture: 0, dry
  4. moisture: 396, moist
  5. moisture: 398, moist
  6. moisture: 407, wet
  7. moisture: 418, wet
  8. ^CTraceback (most recent call last):
  9. File "./lesson_8.py", line 41, in
  10. main()
  11. File "./lesson_8.py", line 38, in main
  12. time.sleep(1)
  13. KeyboardInterrupt
  14. pi@raspberrypi:~/grove.py $

Table of contents

Botland.store - shop for makers!