Building a useful robot doesn't have to require complicated code or expensive equipment. With a Raspberry Pi and some cheap, basic hardware, you can keep your plants healthy and happy from anywhere in the world!
Follow this workshop to learn how to set up an automatic plant watering system by wiring the components of the device, configuring it in the Viam app, and coding a simple script to activate the water pump motor.
What You'll Build
A smart system that triggers a water pump to irrigate the plant when the soil moisture falls below a certain threshold.
What You'll Learn
How to physically assemble a functional plant watering device by connecting and wiring the components.
How to configure the device's components using the Viam app.
How to use Viam modules to control the plant watering device.
How to test and adjust the system for optimal performance.
Prerequisites
Sign up for a free Viam account, and then sign in to the Viam app.
Purchase or acquire the hardware necessary for the workshop:
Have a planter box or flower pot with soil or coffee grounds, a screw driver and a water container ready.
Watch the Video
Follow along with the step-by-step video.
The Raspberry Pi boots from a microSD card. You need to install Raspberry Pi OS on a microSD card that you will use with your Pi. For more details about alternative methods of setting up your Raspberry Pi, refer to the Viam docs.
Click CHOOSE DEVICE. Select your model of Pi, i.e. Raspberry Pi 4 or Raspberry Pi 5.
Click CHOOSE OS. Select Raspberry Pi OS (64-bit) from the menu.
Click CHOOSE STORAGE. From the list of devices, select the microSD card you intend to use in your Raspberry Pi.
Configure your Raspberry Pi for remote access. Click Next. When prompted to apply OS customization settings, select EDIT SETTINGS.
Check Set hostname and enter the name you would like to access the Pi by in that field, for example, echo.
Select the checkbox for Set username and password and set a username (for example, your first name) that you will use to log into the Pi. If you skip this step, the default username will be pi (not recommended for security reasons). And specify a password.
Check Configure wireless LAN and enter your wireless network credentials. SSID (short for Service Set Identifier) is your Wi-Fi network name, and password is the network password. Also change the section Wireless LAN country to where your router is currently being operated. This will allow your Pi to connect to your Wi-Fi so that you can run viam-server wirelessly.
Check Set locale settings and set your time zone and keyboard layout.
Select the SERVICES tab, check Enable SSH, and select Use password authentication.
Save your updates, and confirm YES to apply OS customization settings. Confirm YES to erase data on the microSD card. You may also be prompted by your operating system to enter an administrator password. After granting permissions to the Imager, it will begin writing and then verifying the Linux installation to the microSD card.
Remove the microSD card from your computer when the installation is complete.
Connect with SSH
Place the microSD card into your Raspberry Pi and boot the Pi by plugging it in to an outlet. A red LED will turn on to indicate that the Pi is connected to power.
Once the Pi is started, connect to it with SSH. From a command line terminal window, enter the following command. The text in <> should be replaced (including the < and > symbols themselves) with the username and hostname you configured when you set up your Pi.
ssh <USERNAME>@<HOSTNAME>.local
# for example, my command would look like this:
ssh atacke@echo.local
If you are prompted "Are you sure you want to continue connecting?", type "yes" and hit enter. Then, enter the password for your username. You should be greeted by a login message and a command prompt.
Update your Raspberry Pi to ensure all the latest packages are installed
sudo apt update
sudo apt upgrade
Before programming the Pi to make the plant watering robot functional, you need to physically set up the plant watering robot by wiring the different components together. You will set up the robot to receive signals from the resistive soil moisture sensor and signal to the pump when it is time to pump water from the water's container to the plant's container.
Refer back to this full wiring diagram as you complete the steps to wire your hardware.
First, wire the sensor module to the Raspberry Pi:
Raspberry Pi
<->
Sensor Module
PIN 4 (5V)
to
VCC (Power)
PIN 6 (GND)
to
GND (Ground)
PIN 40 (DIGITAL OUTPUT)
to
DO (Digital Output)
Soil Moisture Sensor
<->
Sensor Module
+
to
+
-
to
-
Next is the relay. The relay will connect to both the Raspberry Pi and the pump motor. First, wire the connections between the relay and the Raspberry Pi:
Raspberry Pi
<->
Relay
PIN 1 (3.3V)
to
COM
PIN 2 (5V)
to
5V
PIN 14 (GND)
to
GND
PIN 8 (IN)
to
IN
Lastly, wire the connections between the relay and the pump motor. Use a wire connector (sometimes called wago connector) to connect a jumper wire from the relay to the wire on the pump motor.
Relay
<->
Pump Motor
NO (Normally Open)
to
Pump Motor
PIN 39 (GND)
to
Pump Motor
Now you are done!
Take a Quiz
Where is the sensor module getting its power from?
The Raspberry PiBattery packElectrical socket
Make sure you understand these concepts before moving ahead.
In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machine.
Click View setup instructions.
To install viam-server on your Raspberry Pi (so you can communicate with and control your sensor and the RGB LED), select the Linux / Aarch64 platform for the Raspberry Pi, and leave your installation method as viam-agent.
Use the viam-agent to download and install viam-server on your Raspberry Pi. Follow the instructions to run the command provided in the setup instructions from the SSH prompt of your Raspberry Pi.
The setup page will indicate when the machine is successfully connected.
With a machine configured and connected, it's time to add the peripherals.
Click the + icon in the left-hand menu and select Component.
Select board, and find the raspberry-pi:rpi4 (or raspberry-pi:rpi5 for the Raspberry Pi 5) module. Click Add Module. Leave the default name board-1 for now, then click Create. This adds the module for working with the Raspberry Pi's GPIO pins.
Notice adding this module adds the board hardware component called board-1. You'll see a collapsible card on the right, where you can configure the board component, and the corresponding board-1 part listed in the left sidebar.
Click Save in the top right to save and apply your configuration changes.
Expand the TEST section of the panel to test your component. Here, you can specify which pin you want to interact with, whether to read or write to it, change its state, or get information from it.
Adjust the moisture sensor sensitivity from the blue screw top notch. Doing this sets a threshold - when the soil moisture level exceeds the threshold, the module outputs LOW (0), otherwise HIGH (1). Type 40 into the Pin field. Then try reading from your pin while you adjust and put the end of the sensor in water.
Next, test your relay and pump. Type in 8 into the Pin field, select Write as your Mode, enter High in the State field, then click Set. This equates to an inadequate soil moisture level, which should start the motor.
To automate the pump motor based on sensor feedback, we can write some code to create a control loop that continuously monitors the pin values from the board. If moisture is detected from the sensor (low pin value), the relay pin should be set to False and will not pump water. If moisture is not detected (high pin value), the relay pin should be set to True and pump water. This logic could be added to your device as a custom service. Luckily, there is a watering-controller module that does this for you! Let's go over how to add that module to your plant watering device and save you some time.
Click the + icon in the left-hand menu and select Service.
Select generic and find the watering-controller:plant-watering module. Click Add Module. Be sure to change the default name to something more descriptive, like watering-service, then click Create.
Notice adding this module adds the generic service of your chosen name. You'll see a collapsible card on the right, where you can configure the watering controller module, and the corresponding part listed in the left sidebar.
In the CONFIGURE panel, add the following attributes. This tells the service which board to use (which should be the Raspberry Pi you've just configured).
{
"board_name": "board-1"
}
Click Save to apply your configuration changes. This may take a moment.
After a few moments, your device should start running the pump motor and water your plants when low moisture is detected and automatically stop once it detects a high moisture level. Test it out by moving your sensor in and out of water. Be sure the other side of your tube is placed within an empty cup or plant - otherwise, it could get unintentionally splashy!
Congratulations on automating your plant watering device!
Now that you have created your automatic plant watering device with a resistive soil moisture sensor, you can easily use Viam to automate other aspects of your garden. For example: