GardenPi: Garden Care with Raspberry Pi

Like any good “lazy programmer,” I’m always looking for ways to automate. This spring’s project: monitoring and watering my garden. I had a wifi-enabled Raspberry Pi laying around and decided to put it to good use. For this project I wanted to do better than just a glorified timer. The goal: An automated watering system that can use the weather forecast, soil, light, and temperature sensors to keep my garden looking great all summer.

Phase 1: Timer and Forecast-Based Watering

I’ve written a simple script that wakes up at 10PM, determines the likelihood of rain, and waters accordingly. The script looks up the forecast using the API. If there is less than a 50% chance of rain, the GardenPi will water for a short duration.

Future phases (already underway) will include sensors for temperature, soil moisture, light, and humidity. Based on that information, the GardenPi will be able to make better watering decisions. The sensor data could be made available via an onboard website or mobile application. Better sensors, form factor, time lapse, and user interface are all things that I’ll be looking at for future versions.

I’ve learned a lot in the last few weeks from building this project — a little bit more about basic electronics, how to solder, how to draw up schematics, and the joy of building actual gizmos that serve a purpose. I’ve also learned that I’m not the only one doing this kind of project with the Raspberry Pi; I’ve included links to other projects at the end of this post.

Required Parts (approx. cost ~$85)

  • Raspberry Pi
  • USB Wifi Dongle
  • SD Card
  • Soaker / Drip hose throughout the garden
  • Solenoid for turning on the soaker hose
  • A female hose swivel adapter to connect your garden hose to the solenoid
  • Relay for switching 12V power
  • 12V / Raspberry Pi power supplies
  • Assorted electronics (breadboard, resistors, wires, LED)
  • High tech weather resistant case (aka medium sized Tupperware container)


  1. Install Raspbian and configure WiFi access.
  2. Install Ruby for root user.
  3. Run gem install god.
  4. Pull the garden_pi_waterer repository and bundle install to install dependencies.
  5. Modify the parameters ( API key, timing info, etc.) in environment.rb.
  6. Build out breakout board (see schematic below).
  7. Connect the solenoid between water source and soaker hose, taking note of the directional arrow on the solenoid and using the adapter to connect the male end of your garden hose to the input on the solenoid.
  8. Power up the Pi and your 12V power supply.
  9. Install install/init.d to /etc/init.d/god and follow instructions in that file
  10. Install god.conf to /home/pi
  11. Restart and enjoy.


I’ve watered my garden with a Raspberry Pi. Success. There is a lot of room for improvement, and I’m looking forward to extending the GardenPi’s capabilities.



Related Links


  • pascal says:

    You might also want to look at

    Despite the reference to the BBB, the software works on the raspberry pi (raspbian) – which is what i use in my own controller.

  • Aleck Landgraf says:

    Hi Shawn,
    Thanks for the writeup. I’m working on something similar and am worried about protecting the solenoid water valve from being shorted as I might put one per raised bed soaker hose. Did you encase your water valve in anything? I’m thinking a rubber spray sealant, like for gutters, might work?


    • Shawn Anderson Shawn Anderson says:

      Thanks for the comment Aleck. To prevent shorting, I just covered my valve with a cheap plastic container. To help with these sorts of problems, I’m moving the Pi, relay, and valves out of the garden and only putting the sensors in the garden. Details of my new project are up here.

    • Aleck Landgraf says:

      Thanks for the additional links. I’m ideally working to make a solar powered solution for folks that don’t have power near their gardens and don’t want to run long wires to the moisture sensors. But housing the water valve away from the raised bed shouldn’t be an issue either. Something like this:

      BTW, to deal with the galvanizing issues of the moisture sensors, I’m only powering up and taking readings once an hour. Code and circuit schematic here:


  • tanuja says:

    hi….very nice project. Can you please tell me know how to integrate a soil moisture sensor into this project.i want to attach a soil moisture sensor and access weather data to manage water resources better

  • Shawn Anderson Shawn Anderson says:

    The moisture sensor is basically using the soil as one of the resistors in a voltage divider. You should be able to do that with any commercially available sensor. Please post the details of your project, I’d love to follow along.

  • Have you considered adding moisture sensor(s)? The weather is one thing, but ultimately what matters is how much water is in the ground around the plants…

  • Shawn Anderson Shawn Anderson says:

    Thanks for suggestion Johannes. There is a link a few comments up to part two of this project. I’ve also played around with using Bluetooth LE to broadcast the information wirelessly:

    What I like about these kind of projects is the ability to bring multiple technologies together to solve some problem in a way that hasn’t been done before.

  • Chris says:

    Hey Shawn,

    I’m trying to replicate your system here but I am still learning (a lot). Can you expand a little on step 4. I assume “pull in” is telling me to use git to add the repo to the Pi, but I don’t know what “&& bundle install” is telling me to do. Is there a specific location it should be placed? Can you give a little more detail please?

    Also, before step 3, “ruby-dev” package needs to be installed for “gem install god” to work.

    • Shawn Anderson Shawn Anderson says:

      Chris, I’m glad to hear you’re trying to build this! Step 4 is telling you to use git to pull down the repository. After that, cd into the repository and run “gem install bundler && bundle install”. Feel free to file issues on the github repo as well.

      Good luck with your project! Let me know how it turns out.

  • Chris says:

    Thanks for the encouragement. I decided to play a little before your comment back and got most everything in place and working. I am able to manually run the app.rb file and see that it triggers events, but I can not seem to get the god to properly load at startup (at least I don’t think I am). Manually running it from /etc/init.d/god does not give an error. Output from /var/log/god shows.
    “/etc/init.d/god: 39: /etc/init.d/god: /usr/local/rvm/bin/bootup_god: not found
    God started”
    Any trouble shooting tips?

    • Chris says:

      Update: As the “/usr/local/rvm/bin/bootup_god” was unable to be found, I switched the god location to “/usr/local/bin/god”, and it appears to run at boot, I can see the process in top use CPU every second or 2. However, it still does not appear to work in terms of running/calling the app.rb. If I manually run app.rb it waits till the specified time and lights an LED (used for testing). I’m sooo close, can you shed any light on what I can do to see whats happening?

      • Shawn Anderson Shawn Anderson says:

        What are you seeing in the logs? Is it logging to /var/log/garden_pi.log?

        • Chris says:

          I think I solved the issue I was having getting god to start. I modified the god file in init.d and pointed it to a different location.
          I also made a change to the god.conf file. I removed the #{bundler} reference and replaced it with the following.
          w.start = “sudo ruby /home/pi/garden_pi/app.rb”
          This allowed the god to launch and monitor the app.rb, proven by killing ruby and seeing it relaunched.
          I’m now trying to ensure my location is the one being searched for on the forcastio api. I noticed you included your weather information (forcast.txt). and I’m trying to replicate that now ;) I got it to output to a text file, but it is not formatted clean or easy to read as yours was. What am I missing….
          def update_forecast
          @forecast = ForecastIO.forecast @lat, @long
          File.write(‘/home/pi/garden_pi/location.xml’, @forecast)

          • Shawn Anderson Shawn Anderson says:

            Hey Chris, glad to hear you got it worked out! Are you able to run forecast_me.rb? (don’t forget to update the API token). If you have further problems, please open an issue on the github repo. Thanks!

  • Alfred Morlin says:

    Ive got a few questions. Can this be done the same way with a 5v pump and a 5v relay? I got a 5V DPDT relay laying around. Can i use that?

    • Shawn Anderson Shawn Anderson says:

      The relay is this build was chosen because it can be activated by low current on the Pi’s 3V GPIO pin. Because of the low current, the GPIO cannot drive your 5V relay. If the load of the 5V relay is within spec, you’d be better off just buying the relay mentioned in the post. Feel free to post any other questions and I’ll do my best to answer. Good luck with your build!

  • Comments are closed.