0
0
Raspberry Piprogramming~10 mins

Automated plant watering system in Raspberry Pi - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Automated plant watering system
Start System
Read Soil Moisture Sensor
Is moisture < threshold?
NoWait and Read Again
Yes
Activate Water Pump
Water for set time
Deactivate Water Pump
Wait before next check
Back to Read Soil Moisture Sensor
The system reads soil moisture, checks if watering is needed, waters if dry, then waits before checking again.
Execution Sample
Raspberry Pi
import time
from gpiozero import MCP3008, OutputDevice

sensor = MCP3008(channel=0)
pump = OutputDevice(17)

while True:
    moisture = sensor.value
    if moisture < 0.3:
        pump.on()
        time.sleep(5)
        pump.off()
    time.sleep(10)
This code reads soil moisture and waters the plant for 5 seconds if moisture is below 0.3, then waits 10 seconds before repeating.
Execution Table
StepActionMoisture ValueCondition (moisture < 0.3)Pump StateWait Time (seconds)
1Read sensor0.25TrueOff0
2Check condition0.25TrueOff0
3Pump ON0.25TrueOn5
4Pump OFF0.25TrueOff0
5Wait before next check0.25TrueOff10
6Read sensor0.35FalseOff0
7Check condition0.35FalseOff0
8Pump remains OFF0.35FalseOff0
9Wait before next check0.35FalseOff10
10Read sensor0.20TrueOff0
11Check condition0.20TrueOff0
12Pump ON0.20TrueOn5
13Pump OFF0.20TrueOff0
14Wait before next check0.20TrueOff10
💡 Loop continues indefinitely; this trace shows two watering cycles and one skip.
Variable Tracker
VariableStartAfter Step 1After Step 6After Step 10Final
moistureN/A0.250.350.200.20
pump stateOffOffOffOffOff
Key Moments - 3 Insights
Why does the pump turn on only when moisture is below 0.3?
Because the condition in the execution_table at steps 2, 7, and 11 checks if moisture < 0.3; only when True does the pump activate.
What happens if the moisture is above 0.3?
At step 7 and 8, the condition is False, so the pump stays off and the system waits before checking again.
Why is there a wait time after watering or skipping watering?
The wait time (steps 5, 9, 14) prevents the system from checking too frequently, giving time for soil moisture to change.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the pump state at step 8?
AOff
BOn
CTurning On
DUnknown
💡 Hint
Check the 'Pump State' column at step 8 in the execution_table.
At which step does the system decide not to water the plant?
AStep 2
BStep 7
CStep 11
DStep 3
💡 Hint
Look at the 'Condition' column in the execution_table where it is False.
If the moisture threshold changed to 0.4, how would the pump behavior change at step 6?
APump would remain OFF
BPump would turn ON only after waiting
CPump would turn ON
DNo change
💡 Hint
Compare moisture value at step 6 with new threshold 0.4 in the execution_table.
Concept Snapshot
Automated Plant Watering System:
- Read soil moisture sensor value
- If moisture < threshold (e.g., 0.3), turn pump ON
- Water for fixed time (e.g., 5 seconds)
- Turn pump OFF
- Wait before next sensor reading
- Repeat indefinitely
Full Transcript
This automated plant watering system uses a soil moisture sensor connected to a Raspberry Pi. The program continuously reads the moisture level. If the moisture is below a set threshold (0.3), it activates a water pump for 5 seconds to water the plant. After watering, the pump turns off and the system waits 10 seconds before checking again. If the moisture is above the threshold, the pump stays off and the system waits before the next check. This cycle repeats indefinitely to keep the plant properly watered without overwatering.