You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
IoT-For-Beginners/2-farm/lessons/2-detect-soil-moisture/pi-soil-moisture.md

3.9 KiB

Measure soil moisture - Raspberry Pi

In this part of the lesson, you will add a capacitive soil moisture sensor to your Raspberry Pi, and read values from it.

Hardware

The Raspberry Pi needs a capacitive soil moisture sensor.

The sensor you'll use is a Capacitive Soil Moisture Sensor, that measures soil moisture by detecting the capacitance of the soil, a property than changes as the soil moisture changes. As the soil moisture increases, the voltage decreases.

This is an analog sensor, so uses an analog pin, and the 10-bit ADC in the Grove Base Hat on the Pi to convert the voltage to a digital signal from 1-1,023. This is then sent over I2C via the GPIO pins on the Pi.

Connect the soil moisture sensor

The Grove soil moisture sensor can be connected to the Raspberry Pi.

Task - connect the soil moisture sensor

Connect the soil moisture sensor.

A grove soil moisture sensor

  1. Insert one end of a Grove cable into the socket on the soil moisture sensor. It will only go in one way round.

  2. With the Raspberry Pi powered off, connect the other end of the Grove cable to the analog socket marked A0 on the Grove Base hat attached to the Pi. This socket is the second from the right, on the row of sockets next to the GPIO pins.

The grove soil moisture sensor connected to the A0 socket

  1. Insert the soil moisture sensor into soil. It has a 'highest position line' - a white line across the sensor. Insert the sensor up to but not past this line.

The grove soil moisture sensor in soil

Program the soil moisture sensor

The Raspberry Pi can now be programmed to use the attached soil moisture sensor.

Task - program the soil moisture sensor

Program the device.

  1. Power up the Pi and wait for it to boot

  2. Launch VS Code, either directly on the Pi, or connect via the Remote SSH extension.

    ⚠️ You can refer to the instructions for setting up and launch VS Code in lesson 1 if needed.

  3. From the terminal, create a new folder in the pi users home directory called soil-moisture-sensor. Create a file in this folder called app.py:

  4. Open this folder in VS Code

  5. Add the following code to the app.py file to import some required libraries:

    import time
    from grove.adc import ADC
    

    The import time statement imports the time module that will be used later in this assignment.

    The from grove.adc import ADC statement imports the ADC from the Grove Python libraries. This library has code to interact with the analog to digital converter on the Pi base hat and read voltages from analog sensors.

  6. Add the following code below this to create an instance of the ADC class:

    adc = ADC()
    
  7. Add an infinite loop that reads from this ADC on the A0 pin, and write the result to the console. This loop can then sleep for 10 seconds between reads.

    while True:
        soil_moisture = adc.read(0)
        print("Soil moisture:", soil_moisture)
    
        time.sleep(10)
    
  8. Run the Python app. You will see the soil moisture measurements written to the console. Add some water to the soil, or remove the sensor from the soil, and see the value change.

    pi@raspberrypi:~/soil-moisture-sensor $ python3 app.py 
    Soil moisture: 615
    Soil moisture: 612
    Soil moisture: 498
    Soil moisture: 493
    Soil moisture: 490
    Soil Moisture: 388
    

    In the example output above, you can see the voltage drop as water is added.

💁 You can find this code in the code/pi folder.

😀 Your soil moisture sensor program was a success!