Water Your Plant Using a Raspberry Pi and Python
Tags: python, beginners, automation, electronics
Published: 2020-03-10 07:18:57

*[If you've found this article useful, consider donating.](https://www.patreon.com/alanjc)*

<a href="https://blog.alanjc.com/articles/water-your-plant-using-a-raspberry-pi-and-python.html"><i>This article was originally published on my blog.</i></a>

<i>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. <strong>Do your own research before attempting to work with electronics and electricity!</strong></i>

![Complete project](https://dev-to-uploads.s3.amazonaws.com/i/5on0g3x84b457qq4ysaa.jpg)

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:

- [Raspberry Pi 3 Starter Kit] (https://amzn.to/2v4127P)
- [12v water pump] (https://amzn.to/39Ckg3e)
- [12v power supply] (https://amzn.to/3aEaqOE)
- [5v Relay] (https://amzn.to/2v0dfu2)
- [An assortment of jumper wires] (https://amzn.to/2xm3dnZ)
- [Alligator clip jumper wires] (https://amzn.to/2VVQpig)
- [Silicone tubing] (https://amzn.to/38E2AmR)

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

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:

- [How electricity works] (https://www.dummies.com/education/science/science-electronics/electronics-basics-fundamentals-of-electricity/)
- [How to use a relay] (https://www.youtube.com/watch?v=5NxVmg8ZFEc)
- [Supplying power to the relay from the Raspberry Pi] (https://youtu.be/My1BDB1ei0E?t=173)
- [Hooking up the 12v power supply to the relay] (https://youtu.be/51f3ZazNW-w?t=10)

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

![GPIO Pins](https://dev-to-uploads.s3.amazonaws.com/i/sxyul54ch0twsvd20dfe.png)

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

![Relay to Pi](https://dev-to-uploads.s3.amazonaws.com/i/mzj54ej08lpdjdyfdtbq.jpg)

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.

![Relay close up](https://dev-to-uploads.s3.amazonaws.com/i/0czjwhrothysxiqa7s5o.jpg)

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.

![Relay close up 2](https://dev-to-uploads.s3.amazonaws.com/i/jmyzere94uzhwutleokx.jpg)

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.

![Relay pin close up](https://dev-to-uploads.s3.amazonaws.com/i/o6a2r31jzxumdgxbmeuj.jpg)

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. <strong>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.</strong>

![12v adapter wiring](https://dev-to-uploads.s3.amazonaws.com/i/teq0k5zn0kph74cbgpn2.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/8egremvotcwbjvp1zfkg.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/edc9j1rgm7vhm3uobn5e.jpg)

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](https://dev-to-uploads.s3.amazonaws.com/i/nkxhrw5yzrolsc3dtsas.jpg)

![12v adapter to wall](https://dev-to-uploads.s3.amazonaws.com/i/pwvhn5ko0i94ldf8oefn.jpg)

![Box in wiring](https://dev-to-uploads.s3.amazonaws.com/i/6xbwnh3lqy2q2lny7s14.jpg)

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 alligator clips](https://dev-to-uploads.s3.amazonaws.com/i/p8uoix6jby0xxxvtu3kd.jpg)

<figure>
<img src="https://dev-to-uploads.s3.amazonaws.com/i/ul1q7x5zylf63n6zccsf.jpg">
<figcaption>Water flows in from the left tube, into the pump, out the right tube, and into the plant.</figcaption>
</figure>

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] (https://gpiozero.readthedocs.io/en/stable/index.html). 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] (https://gpiozero.readthedocs.io/en/stable/index.html).

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] (https://www.raspberrypi.org/documentation/remote-access/ssh/).

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:

{% gist https://gist.github.com/AlanConstantino/fc7d5f1bbfa212f0a125900ce48b80de %}

In this file, I just define a new class called Relay that inherits from the class [OutputDevice] (https://gpiozero.readthedocs.io/en/stable/api_output.html#outputdevice).

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

{% gist https://gist.github.com/AlanConstantino/4a4c9080280d14c387caa6f51b1d67e0 %}

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:

{% gist https://gist.github.com/AlanConstantino/3136cb51b65833cc914b73d3b69192a9 %}

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] (https://myaccount.google.com/lesssecureapps?pli=1), 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:

<code>sudo crontab -e</code>

You should see something similar to this:

![Pi Terminal](https://dev-to-uploads.s3.amazonaws.com/i/mcnn0hqtf44jgpaxcica.png)

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

<code>@reboot python3 /home/pi/Desktop/run/water_plant.py</code>

Then save it by doing <kbd>CTRL+X</kbd> &rarr; <kbd>Y</kbd> &rarr; <kbd>Enter</kbd>

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] (https://github.com/AlanConstantino/rpi-plant-project) or you can just clone the repository with the following command:

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

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.