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

What You'll Learn

Prerequisites

What You'll Need

Watch the Video

Follow along with the step-by-step video.

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

For wiring the sensor module, follow the below chart (see https://pinout.xyz/ for a detailed look at the Raspberry Pi pin layout):

For connecting the soil moisture sensor to the sensor module, + goes to +, and - to -.

For wiring the relay, follow the below chart:

Last step in the wiring section is connecting the pump and the water tube:

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.

Board Configuration

The full example code is in the project Github repository. From there, you can:

Grab the machine address, API Key, and API Key ID from the Connect tab, Code Sample sub-tab and insert them in the plantwatering.py provided. This is how your code connects to your device.

ROBOT_API_KEY = os.getenv('ROBOT_API_KEY') or 'PUT ROBOT API KEY HERE'
ROBOT_API_KEY_ID = os.getenv('ROBOT_API_KEY_ID') or 'PUT ROBOT API KEY ID HERE'
ROBOT_ADDRESS = os.getenv('ROBOT_ADDRESS') or 'PUT ROBOT ADDRESS HERE'

In the main loop of the program (a method called moisture_loop), you are getting the pin values from the board, if the moisture is detected from the sensor (low pin value), it is setting the relay pin to false and not pump any water. If the moisture is not detected (high pin value), it is setting the relay pin to true and pumping water.

async def moisture_loop(board):
    '''
    moisture_pin value:
    - high: no moisture (True)
    - low: means there is moisture (False)
    '''
    # Main program loop, with relay pin
    moisture_pin = await board.gpio_pin_by_name(MOISTURE_PIN)
    relay_pin = await board.gpio_pin_by_name(RELAY_PIN)
    detect_moisture = await moisture_pin.get()
    
    # debug
    print(f'moisture_loop: {MOISTURE_PIN}:{detect_moisture}')
    if detect_moisture == True:
        # start the relay
        await relay_pin.set(True)
    else:
        # stop the relay
        await relay_pin.set(False)

    # return the status of the relay
    return await relay_pin.get()   

The while function in the main function, let's you know if the relay is running or not, and allows you to sleep after certain minutes of being on. This will change depending on the size of the pot and how much water you want to pump each cycle.

 while True:
        running = await moisture_loop(pi)
        if running:
            # you can sleep for 5 seconds (depending on size of pot)
            print('relay running')
        else:
            print('relay not running')
        await asyncio.sleep(watering_sleep)

Run the program: python plantwatering.py

You should see some output to the console about connecting to the robot, along with messages about the relay running or not running depending on the return value of the moisture_loop.

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