Water Your Plant Using a Raspberry Pi and Python

March 9, 2020 | 8 min read

If you've found this article useful consider donating.


I'd like to point out that I am by no means an expert in electronics. If you were to ask me to make a circuit diagram or explain the minutiae of how a certain electronic piece works, I'd draw a complete blank. In reality, I completed this project by just tinkering around with electronic components and having a basic understanding of how electricity works. That being said, you should always exercise caution when working with electronics, especially when using external power sources. Do your own research before attempting to work with electronics and electricity!

Final product

I suck at taking care of plants, mostly because I forget to water them. So then I started thinking, "I'm sure someone out there has figured out a way to automate this process." Turns out, there are plenty of people who have. Namely in the form of using an Arduino or a Raspberry Pi to automatically water their plants. In this post, I'll be talking about how I used a Raspberry Pi and Python to automatically water my plant.

The following are the components I used in this project:

The links above are Amazon Affiliate links. By clicking on a link above and purchasing an item, I earn a little slice of the sale.

Once I had all the components I needed, it was time to do some research. The following are the resources I used to hookup the electronics and have a basic understanding of how they function:

Here is an image of the GPIO pins on the Raspberry Pi:

GPIO pins on the raspberry pi

First, I started out by hooking up the relay to the Raspberry Pi like so:

Relay wiring to Raspberry Pi

The red wire (female to female) leads from the JD_VCC pin on the relay board to the 5V pin (pin 2) on the Raspberry Pi. The orange wire (female to female) leads from the VCC pin on the relay board to the 3.3V pin (pin 1) on the Raspberry Pi.

Close up of JD_VCC and VCC pins on relay

By hooking up these wires, we are giving the relay board power. To actually control an individual relay, we need to connect two more pins from the relay board to the Raspberry Pi, the GND (ground) pin and the IN1 pin.

Close up of GND and IN1 pins on relay

The black wire (female to female) is the ground wire and that leads from the GND pin on the relay board to any ground pin on the Raspberry Pi (I used pin 34). The white wire (female to female) leads from the IN1 pin on the relay board to pin 12 on the Raspberry Pi. The white wire is what will allow us to turn on and off the relay with code.

Pin connections on Raspberry Pi

You can wire up the relay and Raspberry Pi however you wish but just remember to change the pin numbers if you use the code I provide.

After the relay board and the Raspberry Pi are wired up, I then proceeded to connect the 12V adapter to the individual relay. DON'T CONNECT THE 12V WALL ADAPTER INTO A WALL OUTLET UNTIL AFTER YOU FINISH WIRING UP EVERYTHING, YOU DON'T WANT TO BE WORKING WITH LIVE POWER.

12V adapter wiring

The 12V power supply comes with a handy adapter that you can use to connect wires. I wired up the red wire (male to male) to the + sign of the adapter and the brown wire (male to male) to the - sign of the adapter. I then added an alligator clip (female to male) to the brown wire.

12V adapter

The relay board has four individual relays and each relay has three outputs where you can screw in wires. The output in the middle is where you want to connect the positive lead of an external power supply while the output to the left of that is where you want to connect the positive lead of the device you want to power. This is the same for all of the individual relays on the board.

Relay outputs

From the 12v adapter, I hooked up the red wire (positive/power) to the relay output in the middle. I then hooked up a new orange wire (male to male) to the leftmost output on the relay and attached an alligator clip (female to male) to the end of the orange wire.

Relay output wiring
12V adapter to wall adapter
Most of the wiring complete

Now we just need to connect the alligator clips to the water pump. It doesn't matter in which order you connect the alligator clips to the pump, all you're doing is just setting the water flow.

The way I set up my water pump, I wanted water to flow from left to right so I connected the black alligator clip to the pin that has a red dot next to it and I connected the other alligator clip to the other pin on the pump. If I were to reverse the way in which the alligator clips are connected, the water would flow would be from right to left.

Water pump wiring
Final product
Water flows in from the left tube, into the pump, out the right tube, and into the plant.

Finally, we come to the part I'm actually good at, the code. To interface with the GPIO pins on the Raspberry Pi, I used the gpiozero library. It's really easy to use and abstracts away a lot of the lower-level operations you'd usually need to learn in order to get started interfacing with the GPIO pins. You can find a link to the documentation here.

Before we can actually get started writing code, you have to hook up your Pi to a monitor, mouse, and keyboard or you can SSH into your Pi.

Once you have logged into your Pi, navigate to your Desktop and create a folder called "run." Inside of "run," you'll have another folder called "classes." Now within the "classes" folder, create a file called "Hardware.py" which should look something like this:

In this file, I just define a new class called Relay that inherits from the class OutputDevice.

Next, within the "classes" folder, create a new file called "TimeKeeper.py" which should look like the following:

Essentially, the purpose of this class is to keep track of the current time and the last time our plant gets watered.

Both "Harware.py" and "TimeKeeper.py" are in the folder called "classes." I then created a new file called "water_plant.py" which I placed outside of the "classes" folder.

The file "water_plant.py" looks something like this:

You can change the value of the "WATERING_TIME" and "SECONDS_TO_WATER" variables to whatever you'd like. The first variable determines at what time during the day the plant should receive water while the latter determines how long the plant will receive water.

I also set up email reminders so you receive an email whenever your plant gets watered as well as an email every Friday to remind you to check your water level. By default, I've disabled these reminders so the program doesn't crash on start, but if you want to enable them you have to do three things: 1) You have to change lines 26 and 27 to include your Gmail and password 2) You then have to uncomment lines 63, 65, and 68 3) You have to go here, log in with the Gmail you want to receive the reminders on, and toggle the switch to "ON" or else you'll receive an error when you try and send an email using your Gmail account. It's also important to note that the email reminders only work with a Gmail account.

Now that all the files are set up, you should have something that looks like the following directory tree structure:

run
├── classes
│   ├── Hardware.py
│   └── TimeKeeper.py
└── water_plant.py

You can place this "run" folder anywhere on the Raspberry Pi but I just decided to keep it on the Desktop.

Finally, instead of SSHing into the Pi or hooking it up to a monitor every time we want to run the "water_plant.py" script, I wanted the script to run whenever we turn on the Raspberry Pi so that way we can turn the Pi off and move it anywhere. Once we turn it on, we don't have to SSH into the Pi or connect it to a monitor just to run the script. To do this, we need to use the "crontab" command.

If you open a terminal window on your Pi, either by hooking it up to a monitor or by SSHing into it and you type in the following command:

sudo crontab -e

You should see something similar to this:

Terminal window on Raspberry Pi

In this file, you're going to append the following code snippet:

@reboot python3 /home/pi/Desktop/run/water_plant.py

Then save it by doing CTRL+XYEnter

Essentially, we just told the Pi "Hey whenever you reboot, run the python script 'water_plant.py' inside the 'run' folder located on our Desktop." If you didn't place the 'run' folder on your Desktop, just update the path to wherever you placed your "water_plant.py" script.

You can view the full code here or you can just clone the repository with the following command:

git clone https://github.com/AlanConstantino/rpi-plant-project.git

That's it! Now you should have a working Raspberry Pi that waters your plants every 24 hours! It even sends you email reminders every time your plant gets watered as well as every Friday reminding you to check your water level.


If you've found this article useful consider donating.