Welcome back friends. Today I’m back again with a complete project based on Raspberry Pi which is a further extension of one of my previous articles. Recently I published a tutorial on Raspberry Pi soil moisture sensor. I hope you have viewed it carefully. Then to make that in further extension I decided to build a smart irrigation system raspberry pi. So today we’ll do a complete automation of smart irrigation using Pi.
Soil moisture sensor working:
I have explained very clearly about it in my previous tutorial on same topic. You may visit that for more clear explanation. In brief, soil moisture sensor is built out of LM393 IC which is a Op-Amp. it actually amplifies the voltage signal received when probes of sensor dug into the ground. The amount of sensitivity of moisture in soil detected by probes directly varies the voltage signal.
Although signal is detected but it is not identified by arduino alone it value changes. So to make these changes effective we use an Op-Amp. Potentiometer on sensor allow to change the sensitivity to some amount.
- Raspberry Pi with screen keyboard and mouse
- Soil-Moisture sensor
- RGB led with resistor
- Relay Module
- water pump
- 12V supply (for pump)
For programming open Thonny Python IDE or create a new file using terminal ( sudo nano smart_irrigation.py).
First we import Raspberry Pi GPIO library as gp using first command i.e., ‘import RPi.GPIO as gp‘. Then we set numbering of pins according to BOARD pin numbers and also define some pins as input or output.
gp.setmode(gp.BOARD) gp.setup(8,gp.IN) gp.setup(12,gp.OUT, initial=gp.LOW) gp.setup(26,gp.OUT,initial=gp.LOW) gp.setup(32,gp.OUT,initial=gp.LOW)
Now in while loop we put our main code which runs until a keybopard interrupt is encountered.
while True: try: print(not gp.input(8)) gp.output(36,gp.input(8)) gp.output(32,not gp.input(8)) gp.output(12,gp.input(8)) except KeyboardInterrupt: gp.cleanup()
Here is the main complete code:
import RPi.GPIO as gp gp.setmode(gp.BOARD) gp.setup(8,gp.IN) gp.setup(12,gp.OUT, initial=gp.LOW) gp.setup(26,gp.OUT,initial=gp.LOW) gp.setup(32,gp.OUT,initial=gp.LOW) while True: try: print(not gp.input(8)) gp.output(36,gp.input(8)) gp.output(32,not gp.input(8)) gp.output(12,gp.input(8)) except KeyboardInterrupt: gp.cleanup()
As RPi has only digital pin, we can only detect 0 or 1 means HIGH or LOW signal from sensor. So when a High signal is encountered on Pi pin 8, it means there is no moisture in the soil. It is so because soil moisture sensor works on inverting output circuit op-amp. Using this circuit reverses the output on the digital data pin of soil moisture sensor.
So in order to turn on pump we put a not before all read statements in code and output to reverse the output as normally we want to get. then it turns on or off the pump and LED’s to show the current status of moisture in the soil.
With this we have completed our project of smart irrigation system raspberry pi. I hope you like it, and if you need my help then let me know.