Posted in raspberrypi


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.


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
2D0GPIO21 (PIN40)

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
import RPi.GPIO as GPIO
import time

# use channel numbers on the Broadcom SOC

# defining the pins

# defining the pins as output

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__':
        while 1:
            if isSoilMoisted():
                print("Soil is moisted! Notification has been sent!")
                print("Dry soil. Water the plant as fast as possible.")


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

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



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.


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.


Be the first to post a comment

Post a comment