This Python script implements a smart home project that controls water irrigation based on a soil moisture sensor. The system uses a Raspberry Pi and GPIO (General Purpose Input/Output) pins to interact with the soil moisture sensor and a water pump.
To run this code, you need the following hardware components:
- Raspberry Pi (any model with GPIO pins)
- Soil moisture sensor
- Water pump
- Jumper wires for connections
Make sure you have the necessary Python libraries installed, such as RPi.GPIO
.
The code assumes the following GPIO pin configuration:
sensor_pin
: GPIO pin number connected to the soil moisture sensor (input pin)pump_pin
: GPIO pin number connected to the water pump (output pin)
Ensure that you have made the correct connections between the Raspberry Pi, soil moisture sensor, and water pump.
The code performs the following operations:
-
Imports the necessary libraries:
RPi.GPIO
for GPIO control andtime
for time-related operations. -
Defines the pin numbers for the GPIO communication,
sensor_pin
andpump_pin
. -
Defines the
setup()
function, which initializes the GPIO pins. It sets the GPIO pin numbering mode to BCM and configurespump_pin
as an output pin andsensor_pin
as an input pin. -
Defines the
read_moisture()
function, which reads the moisture level from the soil moisture sensor. It uses theGPIO.input()
function to retrieve the sensor value. -
Defines the
control_irrigation()
function, responsible for controlling the water irrigation process based on the moisture level. It performs the following steps:- Sets a
moisture_threshold
value, which can be adjusted based on sensor readings. - Reads the current moisture level using the
read_moisture()
function. - If the moisture level is below the threshold, it activates the water pump by setting
pump_pin
to a high state usingGPIO.output()
. - Prints a message indicating that watering is in progress.
- Pauses the program execution for a specified duration (5 seconds in this example) using
time.sleep()
. - Deactivates the water pump by setting
pump_pin
to a low state. - Prints a message indicating that watering is finished.
- If the moisture level is above or equal to the threshold, it prints a message indicating that irrigation is not required.
- Sets a
-
In the main program, it checks if the script is being run directly and not imported as a module (
if __name__ == '__main__'
).- Sets up the GPIO pins using the
setup()
function. - Calls the
control_irrigation()
function to control the irrigation process. - Catches the
KeyboardInterrupt
exception to handle program termination via Ctrl+C.
- Sets up the GPIO pins using the
-
Make the necessary hardware connections between the Raspberry Pi, soil moisture sensor, and water pump.
-
Install the required Python libraries, such as
RPi.GPIO
. -
Save the Python script to a file, e.g.,
irrigation_control.py
. -
Run the script using
python irrigation_control.py
or a similar command. -
Observe the console output, which will indicate whether watering is needed and control the water pump accordingly.