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

Prerequisites

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.

Install Raspberry Pi OS

  1. Connect the microSD card to your computer.
  2. Launch the Raspberry Pi Imager. raspberry pi imager
  3. Click CHOOSE DEVICE. Select your model of Pi, i.e. Raspberry Pi 4 or Raspberry Pi 5.
  4. Click CHOOSE OS. Select Raspberry Pi OS (64-bit) from the menu.
  5. Click CHOOSE STORAGE. From the list of devices, select the microSD card you intend to use in your Raspberry Pi. raspberry pi storage
  6. Configure your Raspberry Pi for remote access. Click Next. When prompted to apply OS customization settings, select EDIT SETTINGS. raspberry pi edit settings
  7. Check Set hostname and enter the name you would like to access the Pi by in that field, for example, echo.
  8. 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.
  9. 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.
  10. Check Set locale settings and set your time zone and keyboard layout. raspberry pi hostname username and password
  11. Select the SERVICES tab, check Enable SSH, and select Use password authentication. raspberry pi enable SSH
  12. 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. raspberry pi imager erase prompt After granting permissions to the Imager, it will begin writing and then verifying the Linux installation to the microSD card.
  13. Remove the microSD card from your computer when the installation is complete.

Connect with SSH

  1. 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.
  2. 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
    
  3. 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. raspberry pi SSH login success
  4. 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.

Full Wiring Diagram

First, wire the sensor module to the Raspberry Pi:

sensor module wiring diagram

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:

relay wiring diagram

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 wiring diagram

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 Pi Battery pack Electrical socket

Make sure you understand these concepts before moving ahead.

  1. In the Viam app under the LOCATIONS tab, create a machine by typing in a name and clicking Add machine. add machine
  2. Click View setup instructions. setup instructions
  3. 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. select platform
  4. 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.
  5. The setup page will indicate when the machine is successfully connected. machine connected

With a machine configured and connected, it's time to add the peripherals.

  1. In the Viam app, find the CONFIGURE tab.
  2. Click the + icon in the left-hand menu and select Component. Board Configuration
  3. 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.
  4. 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.
  5. Click Save in the top right to save and apply your configuration changes.
  6. 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. test board component
  7. 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. test moisture sensor via reading pin 40
  8. 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. test relay and pump via pin 8

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.

watering controller module program loop

  1. In the Viam app, find the CONFIGURE tab.
  2. Click the + icon in the left-hand menu and select Service.
  3. 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. add watering controller module
  4. 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.
  5. 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"
    }
    
    configure watering controller
  6. Click Save to apply your configuration changes. This may take a moment.
  7. 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:

Check out our documentation for more inspiration.

What You Learned

Related Resources