Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Automated plant watering system in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Automated Plant Watering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this soil moisture check?

Consider this Python code snippet that reads a soil moisture sensor value and decides whether to water the plant.

moisture = 350
if moisture < 300:
    print("Watering plant")
else:
    print("Soil is moist enough")

What will this code print?

Raspberry Pi
moisture = 350
if moisture < 300:
    print("Watering plant")
else:
    print("Soil is moist enough")
ASoil is moist enough
BError: moisture sensor not found
CNo output
DWatering plant
Attempts:
2 left
💡 Hint

Think about the condition: is 350 less than 300?

❓ Predict Output
intermediate
1:30remaining
What does this pump control code print?

This code controls a water pump based on a boolean variable pump_on. What is the output?

pump_on = False
if pump_on:
    print("Pump is running")
else:
    print("Pump is off")
Raspberry Pi
pump_on = False
if pump_on:
    print("Pump is running")
else:
    print("Pump is off")
APump status unknown
BPump is running
CPump is off
DError: pump_on not defined
Attempts:
2 left
💡 Hint

Remember that False means the condition is not true.

🔧 Debug
advanced
2:00remaining
Why does this watering loop run forever?

Look at this code snippet that should water the plant once when soil is dry, but it keeps watering endlessly. What is the cause?

while True:
    moisture = 250
    if moisture < 300:
        print("Watering plant")
    else:
        print("Soil is moist enough")
Raspberry Pi
while True:
    moisture = 250
    if moisture < 300:
        print("Watering plant")
    else:
        print("Soil is moist enough")
AThe else block is missing a break statement
BThe moisture value never changes inside the loop
CThe print statement causes the loop to restart
DThe loop condition is wrong, should be while moisture < 300
Attempts:
2 left
💡 Hint

Think about what happens to the moisture value each time the loop runs.

📝 Syntax
advanced
1:30remaining
Which option has correct syntax to read sensor and water plant?

Choose the code snippet that correctly reads a moisture sensor and waters the plant if dry.

A
moisture = read_sensor()
if moisture &lt; 300:
    water_plant()
B
moisture = read_sensor()
if moisture &lt; 300
    water_plant()
C
moisture = read_sensor()
if moisture &lt; 300:
water_plant()
D
moisture = read_sensor()
if moisture &lt; 300:
    water_plant
Attempts:
2 left
💡 Hint

Remember Python needs a colon and indentation for blocks.

🚀 Application
expert
2:00remaining
How many times will the pump run in this code?

This code runs a watering pump based on sensor readings in a list. How many times will it print "Pump running"?

moisture_readings = [320, 280, 290, 310, 270]
pump_runs = 0
for moisture in moisture_readings:
    if moisture < 300:
        print("Pump running")
        pump_runs += 1
print(pump_runs)
Raspberry Pi
moisture_readings = [320, 280, 290, 310, 270]
pump_runs = 0
for moisture in moisture_readings:
    if moisture < 300:
        print("Pump running")
        pump_runs += 1
print(pump_runs)
A4
B2
C5
D3
Attempts:
2 left
💡 Hint

Count how many values in the list are less than 300.

Practice

(1/5)
1.

What is the main purpose of an automated plant watering system using a Raspberry Pi?

easy
A. To control room temperature automatically
B. To water plants continuously without checking soil
C. To measure sunlight for plants
D. To water plants only when the soil is dry

Solution

  1. Step 1: Understand the system goal

    The system uses sensors to check soil moisture.
  2. Step 2: Identify the watering condition

    It waters plants only when soil is dry to save water.
  3. Final Answer:

    To water plants only when the soil is dry -> Option D
  4. Quick Check:

    Water only if dry = A [OK]
Hint: Focus on saving water by watering only when needed [OK]
Common Mistakes:
  • Thinking it waters plants all the time
  • Confusing soil moisture with sunlight
  • Assuming it controls temperature
2.

Which of the following Python code snippets correctly sets up a GPIO pin 17 as an output to control a water pump?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
# What goes here?
easy
A. GPIO.setup(17, GPIO.OUT)
B. GPIO.setup(17, GPIO.IN)
C. GPIO.output(17, GPIO.IN)
D. GPIO.output(17, GPIO.OUT)

Solution

  1. Step 1: Identify pin mode setup

    GPIO.setup(pin_number, GPIO.OUT) sets pin as output.
  2. Step 2: Choose correct function and mode

    GPIO.setup(17, GPIO.OUT) correctly sets pin 17 as output.
  3. Final Answer:

    GPIO.setup(17, GPIO.OUT) -> Option A
  4. Quick Check:

    Setup output pin = A [OK]
Hint: Use GPIO.setup(pin, GPIO.OUT) to set output pins [OK]
Common Mistakes:
  • Using GPIO.IN instead of GPIO.OUT for output pins
  • Calling GPIO.output instead of GPIO.setup to set mode
  • Confusing pin numbers or modes
3.

What will be the output of this Python code controlling a pump based on soil moisture sensor reading?

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
soil_moisture = 300  # sensor value
if soil_moisture < 400:
    GPIO.output(17, GPIO.HIGH)
    print("Pump ON")
else:
    GPIO.output(17, GPIO.LOW)
    print("Pump OFF")
medium
A. SyntaxError
B. Pump OFF
C. Pump ON
D. No output

Solution

  1. Step 1: Check soil moisture condition

    Value 300 is less than 400, so condition is true.
  2. Step 2: Determine pump action and output

    GPIO.output sets pin HIGH and prints "Pump ON".
  3. Final Answer:

    Pump ON -> Option C
  4. Quick Check:

    300 < 400 triggers Pump ON [OK]
Hint: Compare sensor value to threshold to decide pump state [OK]
Common Mistakes:
  • Misreading the comparison operator
  • Confusing HIGH and LOW states
  • Ignoring print output
4.

Identify the error in this Raspberry Pi watering system code snippet:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
soil_moisture = 500
if soil_moisture > 400
    GPIO.output(17, GPIO.LOW)
    print("Pump OFF")
medium
A. soil_moisture value is invalid
B. Missing colon (:) after if condition
C. GPIO.output should be GPIO.setup
D. Wrong GPIO pin number

Solution

  1. Step 1: Check if statement syntax

    Python requires a colon (:) after the if condition.
  2. Step 2: Identify missing colon error

    The code misses ':' after 'if soil_moisture > 400', causing syntax error.
  3. Final Answer:

    Missing colon (:) after if condition -> Option B
  4. Quick Check:

    if statement needs ':' = D [OK]
Hint: Always put ':' after if conditions in Python [OK]
Common Mistakes:
  • Forgetting colon after if
  • Confusing GPIO.output with GPIO.setup
  • Assuming pin number is wrong
5.

You want to improve the watering system to water plants only if soil moisture is below 400 and only between 6 AM and 8 AM. Which Python code snippet correctly adds this time condition?

import datetime
now = datetime.datetime.now()
soil_moisture = 350
if soil_moisture < 400 and ???:
    print("Watering plants")
else:
    print("No watering")
hard
A. now.hour >= 6 and now.hour <= 8
B. now.hour > 6 or now.hour < 8
C. now.hour < 6 and now.hour > 8
D. now.hour == 6 or now.hour == 8

Solution

  1. Step 1: Understand time range condition

    We want hours from 6 to 8 inclusive, so hour >= 6 and hour <= 8.
  2. Step 2: Combine soil moisture and time conditions

    Use 'and' to check both soil moisture and time range.
  3. Final Answer:

    now.hour >= 6 and now.hour <= 8 -> Option A
  4. Quick Check:

    Check hour between 6 and 8 = B [OK]
Hint: Use 'and' with hour >= 6 and <= 8 for time range [OK]
Common Mistakes:
  • Using 'or' instead of 'and' for time range
  • Using strict inequalities excluding 6 or 8
  • Checking only exact hours 6 or 8
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard