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

cd grove.py
nano lesson_8.py

Step 2:Skopiujthe following code

#!/usr/bin/env python

import time
from mraa import getGpioLookup
from upm import pyupm_buzzer as upmBuzzer

from grove.grove_moisture_sensor import GroveMoistureSensor
from grove.lcd.sh1107g import JHD1802

def main():
    # Grove - 16x2 LCD(White on Blue) connected to I2C port
    lcd = JHD1802()

    # Grove - Moisture Sensor connected to port A0
    sensor = GroveMoistureSensor(0)

    # Grove - Buzzer connected to PWM port
    buzzer = upmBuzzer.Buzzer(getGpioLookup('GPIO12'))

    while True:
        mois = sensor.moisture
        if 0 <= mois and mois < 300:
            level = 'dry
        elif 300 <= mois and mois < 600:
            level = 'moist'
        Another:
            level = 'wet'
            buzzer.playSound(upmBuzzer.BUZZER_DO, 200000)

        print('moisture: {}, {}'.format(mois, level))

        lcd.setCursor(0, 0)
        lcd.write('moisture: {0:>6}'.format(mois))

        lcd.setCursor(1, 0)
        lcd.write('{0:>16}'.format(level))

        time.sleep(1)

if __name__ === '__main__':
    main()

Step 3:Uruchomprogram

sudo chmod +x lesson_8.py
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".

pi@raspberrypi:~/grove.py $ sudo ./lesson_8.py
moisture: 0, dry
moisture: 0, dry
moisture: 396, moist
moisture: 398, moist
moisture: 407, wet
moisture: 418, wet
^CTraceback (most recent call last):
  File "./lesson_8.py", line 41, in 
    main()
  File "./lesson_8.py", line 38, in main
    time.sleep(1)
KeyboardInterrupt
pi@raspberrypi:~/grove.py $

Table of contents

Botland.store - shop for makers!