---
title: DIY Wi‑Fi Sprinkler Controller with Raspberry Pi: Step‑by‑Step Guide for a Smarter Garden
siteUrl: https://logzly.com/sprinklertech
author: sprinklertech (Smart Sprinkler DIY)
date: 2026-06-18T08:00:29.096869
tags: [sprinkler, raspberrypi, gardentech]
url: https://logzly.com/sprinklertech/diy-wifi-sprinkler-controller-with-raspberry-pi-stepbystep-guide-for-a-smarter-garden
---


**Disclosure: We are reader supported, and earn affiliate commissions when you buy through us.**


Ever walked out to your backyard on a scorching July afternoon and found the grass still thirsty? The weather is changing fast, [water bills](https://www.amazon.com/s?k=water+bills&tag=organizationtip101-20) are climbing, and you’re tired of guessing when to water. A Wi‑Fi sprinkler controller built on a [Raspberry Pi](https://www.amazon.com/s?k=Raspberry+Pi&tag=organizationtip101-20) can fix that. It gives you real‑time control, saves water, and lets you tinker with code while you wait for the tomatoes to ripen.

## Why a Raspberry Pi?

Most off‑the‑the‑the‑shelf [smart controllers](https://www.amazon.com/s?k=smart+controllers&tag=organizationtip101-20) lock you into a brand’s app and subscription. A Pi, on the other hand, is a tiny computer you already own (or can get for under $40). It runs Linux, talks to your [home network](https://www.amazon.com/s?k=home+network&tag=organizationtip101-20), and can be programmed in Python – a language that even a weekend gardener can learn. To see how this code can automatically adapt to current [weather conditions](https://www.amazon.com/s?k=weather+conditions&tag=organizationtip101-20), read our guide on how to [program your smart sprinkler to adjust for real‑time weather](/sprinklertech/save-water-and-money-program-your-smart-sprinkler-to-adjust-for-realtime-weather). Plus, you keep full control over your data. No [hidden fees](https://www.amazon.com/s?k=Hidden+fees&tag=organizationtip101-20), no forced updates.

## What You’ll Need

| Item | Reason |
|------|--------|
| Raspberry Pi 3B+ or newer | Built‑in Wi‑Fi, enough GPIO pins |
| 5 V 2 A [power supply](https://www.amazon.com/s?k=power+supply&tag=organizationtip101-20) | Keeps the Pi stable |
| 2‑channel 5 V relay board | Switches the valve circuits |
| 24 V DC solenoid valve (one per zone) | Turns water on/off |
| 12 V or 24 V power brick for valves | Powers the [irrigation system](https://www.amazon.com/s?k=irrigation+system&tag=organizationtip101-20) |
| Jumper wires, breadboard | Connect everything |
| Micro‑[SD card](https://www.amazon.com/s?k=SD+card&tag=organizationtip101-20) (8 GB+) | OS and code storage |
| Optional: waterproof enclosure | Protects electronics from rain |

All of these are garden‑center or electronics‑store staples. If you already have a Pi lying around, you’re half way there.

## Setting Up the Pi

### 1. Install Raspberry Pi OS

Download the Raspberry Pi Imager from the official site, flash the “Raspberry Pi OS Lite” image onto the SD card, and pop it into the Pi. Connect a monitor, keyboard, and power it up. Run through the basic setup: set a password, enable SSH (so you can work headless later), and connect to your Wi‑Fi.

### 2. Update Packages

Open a terminal and type:

```
sudo apt update && sudo apt upgrade -y
```

This makes sure you have the latest [security patches](https://www.amazon.com/s?k=security+patches&tag=organizationtip101-20).

### 3. Install Python and GPIO Library

```
sudo apt install python3-pip
pip3 install RPi.GPIO
```

The RPi.GPIO package lets Python talk to the Pi’s pins.

## Wiring the Relays

The relay board acts as a safe switch between the Pi’s low‑voltage pins and the higher voltage that runs your sprinkler valves.

1. Connect the Pi’s 5 V pin to the relay board’s VCC.
2. Connect a ground pin (GND) to the relay board’s GND.
3. Choose two GPIO pins (for example GPIO17 and GPIO27) and wire them to the relay IN1 and IN2 pins.
4. Wire the common (COM) terminals of each relay to the positive side of your valve power supply.
5. Wire the normally open (NO) terminals to the valve’s positive lead.
6. Connect the valve’s negative lead back to the power supply’s ground.

Double‑check that the relay board’s voltage rating matches your valve supply (most garden valves run on 24 V). Use a multimeter if you’re unsure.

## Writing the Control Script

Create a new Python file called `sprinkler.py` in your home directory.

```python
import RPi.GPIO as GPIO
import time
import json
from flask import Flask, request

# Pin setup
ZONE_PINS = {
    "zone1": 17,
    "zone2": 27
}
GPIO.setmode(GPIO.BCM)
for pin in ZONE_PINS.values():
    GPIO.setup(pin, GPIO.OUT)
    GPIO.output(pin, GPIO.HIGH)   # Relays are active low

app = Flask(__name__)

def run_zone(zone, minutes):
    pin = ZONE_PINS[zone]
    GPIO.output(pin, GPIO.LOW)   # Turn on valve
    time.sleep(minutes * 60)
    GPIO.output(pin, GPIO.HIGH)  # Turn off valve

@app.route('/run', methods=['POST'])
def run():
    data = request.get_json()
    zone = data.get('zone')
    minutes = data.get('minutes', 5)
    if zone not in ZONE_PINS:
        return {"error": "Invalid zone"}, 400
    run_zone(zone, minutes)
    return {"status": f"{zone} ran for {minutes} minutes"}

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)
```

### How It Works

- **Flask** creates a tiny web server that listens for HTTP POST requests.
- The JSON payload tells the script which zone to water and for how long.
- The GPIO pins are set low to close the relay (most relay boards are active‑low), opening the valve.
- After the timer expires, the pin goes high again, closing the valve.

Save the file and install Flask:

```
pip3 install flask
```

Run the script with:

```
sudo python3 sprinkler.py
```

You should now be able to send a request from any device on your network:

```
curl -X POST -H "Content-Type: application/json" \
     -d '{"zone":"zone1","minutes":10}' \
     http://<pi_ip_address>:5000/run
```

If the water starts flowing, you’ve just built a Wi‑Fi sprinkler controller!

## Adding Weather Awareness

Watering when it’s raining is wasteful. The OpenWeatherMap API offers a [free tier](https://www.amazon.com/s?k=free+tier&tag=organizationtip101-20) that returns current rain data. Add this snippet before the `run_zone` call:

```python
import requests

def should_water():
    api_key = "YOUR_API_KEY"
    city_id = "YOUR_CITY_ID"
    url = f"http://api.openweathermap.org/data/2.5/weather?id={city_id}&appid={api_key}"
    resp = requests.get(url).json()
    # rain field appears only when it is raining
    return "rain" not in resp
```

Then modify the `run` endpoint:

```python
if not should_water():
    return {"status": "Skipping watering, rain detected"}, 200
```

Now the Pi checks the sky before turning on the valves. You can schedule a daily cron job that calls the `/run` endpoint for each zone, and the weather check will automatically cancel any unnecessary watering. For a more comprehensive build, including a cost‑effective parts list, see our [DIY Wi‑Fi Sprinkler Controller for $75](/sprinklertech/build-a-diy-wifi-sprinkler-controller-for-75-complete-stepbystep-guide).

## Securing Your Controller

A web server on your home network can be a target. Here are three quick steps to keep things safe:

1. **Change the default password** for the Pi user `pi`. Use `passwd` to set a [strong password](https://www.amazon.com/s?k=strong+password&tag=organizationtip101-20).
2. **Limit access** by adding a simple token check in the Flask route. Pass a secret header with each request.
3. **Run behind your router’s firewall** – block external traffic to port 5000 unless you set up a VPN.

These steps are easy, and they protect both your garden and your [internet connection](https://www.amazon.com/s?k=internet+connection&tag=organizationtip101-20).

## Putting It All Together

1. Wire the relays and valves.
2. Install the OS, update, and add Python libraries.
3. Load the `sprinkler.py` script and test with a curl command.
4. Add the weather check and secure the endpoint.
5. (Optional) Build a small weather‑aware scheduler using `cron`:

```
0 6 * * * curl -X POST -H "Content-Type: application/json" \
    -H "X-Token: YOUR_SECRET" \
    -d '{"zone":"zone1","minutes":15}' http://localhost:5000/run
```

This line tells the Pi to water zone 1 for 15 minutes every morning at 6 am, but only if it’s not raining.

## A Little Garden Story

When I first tried this on my balcony, I accidentally wired the relay backwards and flooded the [kitchen floor](https://www.amazon.com/s?k=kitchen+floor&tag=organizationtip101-20). The good news? The Pi shut down the valve after the timer ran out, and I learned to double‑check the “normally open” vs “normally closed” labels. Now I have a tiny “rain‑skip” button on my phone that sends a POST request with a single tap. The garden is greener, the [water bill](https://www.amazon.com/s?k=water+bill&tag=organizationtip101-20) is lower, and I get a small rush every time I see the flow meter spin.

## Keep Experimenting

Your smart sprinkler can do more than just turn water on and off. Add soil‑[moisture sensors](https://www.amazon.com/s?k=moisture+sensors&tag=organizationtip101-20) to the Pi’s analog‑to‑digital converter (MCP3008) and let the script decide based on real ground conditions. Or hook up a small LCD screen to display the next [watering schedule](https://www.amazon.com/s?k=watering+schedule&tag=organizationtip101-20). The sky’s the limit – and the only thing you need is curiosity.

Happy hacking, and may your tomatoes stay juicy!
