Posted in raspberrypi

header_logo_lg.jpg

Every plant needs water, but our busy schedule neglects them. In order to avoid checking your plant soil every day, I propose the following easy solution. A project where we can read the state of the soil and be notified if we need to act on it.

The pin connections

For this project, I will use a soil moisture sensor and Raspberry Pi Zero. You can use other boards (which run Linux) if you like, the difference will be in the pin connections and slight changes in the code base.

soil_moisture_sensor_raspberry_pi_schematic.jpg

As you might notice from the sensor image, it already tells us when it's powered (PWR-LED) and when the soil is moist (D0-LED). After we make all the connections, put the probe in a glass of water and adjust the potentiometer in order to turn ON the D0-LED. If the LED already emitted light, make sure that you adjust the potentiometer until it almost turns OFF.

In the below table are the pin associations that I made.

 SensorRaspberry Pi Zero
1VCC 5VVCC 5V (PIN4)
2D0GPIO21 (PIN40)
4GNDGND (PIN6)

The code

I tried to maintain the code as simple as possible. Basically, it checks every 1 second the data pin (21). If the value that we read is LOW then the soil is moist, otherwise, the soil is dry. For these situations, we can take some actions.

One of the possible actions is to send a message through email, SMS or in another application like Telegram or Discord. I actually some blog posts a while ago on how you can send an SMS or Email using AWS Simple Notification Service (SNS), how to send messages through Discord or Telegram, or through another messaging application.

##
#   The Humble Code
#   www.thehumblecode.com
##
import RPi.GPIO as GPIO
import time

# use channel numbers on the Broadcom SOC
GPIO.setmode(GPIO.BCM)

# defining the pins
GPIO_MOISTURE = 21

# defining the pins as output
GPIO.setup(GPIO_MOISTURE, GPIO.IN)

def isSoilMoisted():
    # if it detects water the output is LOW
    # we need to negate the output
    return not GPIO.input(GPIO_MOISTURE) 
        
if __name__ == '__main__':
    try:
        while 1:
            if isSoilMoisted():
                print("Soil is moisted! Notification has been sent!")
            else:
                print("Dry soil. Water the plant as fast as possible.")

            time.sleep(1)

        # close execution by pressing CTRL + C
    except KeyboardInterrupt:
        print("Intrerrupted by user")
        pass
    finally:
        print("Program stopped")

Run the code using the following command on the terminal and press CTRL + C to cancel anytime.

python soil_moisture.py

Conclusions

Below I attached a picture with this sensor attached to my Dracaena Marginata plant. The soil was moist and the D0-LED turned ON. 

In the future, I plan to automatically water the plant when the soil is dry.

soil_moisture_sensor_attached_to_the_pot.jpg

But, what if I have 5 plants or maybe 20. Do I need that many sensors and development boards? How do I scale in this case? The situation can get even more complicated if you add the plant type, location, size and many other factors.

For a starter, I think it's best to attach this only to a single plant. Personally, I chose the plant which receives the most sun because on this one the soil will dry quicker. And I will try to go check other plants as well when I receive the notification.

Thank you for reading.

Comments

Be the first to post a comment

Post a comment