Introduction: Raspberry Pi Soil Moisture Sensor
Agriculture consumes between 80 - 90% of all freshwater in the United States. An easy way to conserve water in the agricultural sector is to install a soil moisture sensor. Soil moisture sensors measure the amount of water in the soil to maintain consistent and ideal soil conditions for plants. In some cases, installing a soil moisture sensor reduces residential irrigation by as much as 50%.
Excluding the Pi, the system can be built for less than $25.
Here's a tutorial on how to control an outdoor irrigation valve to automatically water your plants when the soil is dry.
Step 1: Materials
Other microcontrollers, like an Arduino, also work.
This resistance was the best for my system, but a different resistor value might work better for your own setup. Experiment w/ different value resistors and see what happens!
-- Co-Planar Capacitor
-- 22 Gauge Wire (recommended to get stranded)
Recommended to get stranded wire b/c conducts better & is less likely to break.
This is the bare minimum needed to build & test the system. If you want to install it outside after it has been tested and confirmed to work, coat everything in epoxy! ...Although a better way might be to replace the breadboard w/ a PCB Board . Molex 2-Pin Connectors are also a nice addition.
Step 2: Tools
-- Soldering Iron, solder, & Solder Sucker (or desoldering wick)
A soldering iron is (almost) essential for this project, especially for attaching wire leads to the co-planar capacitor. You can purchase a soldering iron, solder and solder wick (removes solder) for ~ $20-30, or find a local makerspace/hackerspace that will let you come in and use an on-site soldering iron.
-- Optional (but highly recommended): Multimeter (for testing and debugging!)
Step 3: Sensor & Circuit Design
An RC circuit provides a quick & simple way to measure changes in the sensor capacitance due to changes in soil water content.
Every RC circuit has an associated time constant, which is the time it takes the capacitor to reach ~ 63% of its maximum charge. The time constant equals the total circuit resistance times the circuit capacitance: τ = R * C
The time constant is used to measure changes in the sensor capacitance due to changes in soil water content. As the water content increases, the capacitance increases causing the associated circuit time constant to increase. The Raspberry Pi GPIO Pin 14 measures, or counts, the circuit time constant (how long it takes the capacitor to charge).
Step 4: Build It: Hardware Pt 1.
Solder wire leads onto the co-planar capacitor (aka soil moisture sensor) pads. Test connection w/ multimeter. If the sensor is electrically connected, coat in epoxy & let dry before continuing.
If you're using stranded wire + a breadboard, you'll need to find a way to connect the stranded wire to the breadboard (b/c trying to shove it into the breadboard holes will make you want to pull your hair out). I stripped two breadboard wires and soldered them to the sensor leads. However, my connections were still a bit finicky and sensitive to touch and changes in light. Try different methods and see what works best. Use available materials and keep it simple!
Step 5: Build It: Hardware Step 2.
- Connect the RPi GPIO pins to the breadboard. Connect the 3.3 V output pin to the "+" column along the side of the breadboard.
- Connect the GPIO ground pin to the "-" column.
- Connect one resistor end to the 3.3 V output (any of the holes in the "+" column). Connect the other end to any of the breadboard rows. Orientation of the resistor leads doesn't matter.
- Connect GPIO pin 14 to the same breadboard row as the resistor. You can use a different GPIO pin, but remember to change it in the software program.The GPIO probe MUST go between the resistor and the co-planar capacitor.
- Connect one of the co-planar capacitor leads to the same breadboard row as the resistor + probe. Connect the other lead to ground (any of the holes along the "-" column). It doesn't matter which lead goes where.
Step 6: Build It: Software
Write a code to measure the capacitance of the sensor! Use the fact that the time constant changes depending on the medium in which the sensor is installed (capacitance is much larger in water than in air).
Or you can just use mine :)
Keep in mind that this is a basic program and doesn't include a GUI. All commands are run on the Pi's terminal window (LXTerminal). The program prints the circuit time constant, correlated to soil water content, and a raw time stamp. If the reading is too low, the program also prints a reminder to water the plants. It stores the raw data in a text file. To end the program, use "Ctrl + Z" or "Ctrl + C". Modify and improve the program based on your own skills/needs.
Remember to change the watering threshold based on your own experimental discoveries!
Step 7: Testing!
- Test the software program and determine your ideal threshold.
- Test the sensor in water and air first; this provides the upper and lower bounds on the sensor output. If you find that the sensor is not reading in either of these mediums, change the value of the resistor until you get a reasonable signal. Be sure to record the reading for at least 5 - 10 minutes. It is helpful to plot the results in a program like Excel or R.
- Place the sensor in a cup of dry soil. Add a small amount of water and measure changes in sensor output over time (wait at least 5 - 10 minutes). Repeat this multiple times to get a better understanding of the reading and to improve the software/hardware as necessary.
- If you are not getting a reading in either medium, try checking the electrical connections on the sensor.
- Your signal will likely be different than mine due to minor differences in your sensor and general setup. Use your findings from above to find an approximate value at which your soil is too dry.
Step 8: Enjoy!
Install in a sunny spot and use it to maintain consistent watering of your beloved plants!
Note: In the system pictured above, the Raspberry Pi is also controlling a solenoid irrigation valve, so the entire watering system is automated! This is fairly easy to do and if there is sufficient interest I will include an overview of this process as well.