NCEA Level 2 Digital Technologies study materials
NCEA Level 2 Digital Technologies study resources

Greenhouse Irrigation That Waters at Midnight

NCEA Level 2 Digital Technologies · Put your knowledge to work in context.

All case studies

Greenhouse Irrigation That Waters at Midnight

A Year 12 group has built an automatic irrigation system for the school's 4 m × 3 m greenhouse. A Raspberry Pi Pico running MicroPython reads a capacitive soil-moisture probe every two seconds through an ADC pin, and switches a 5 V relay module that turns a 12 V pump on when the reading falls below 45% and off when it rises above it. A photoresistor input is meant to stop watering at night. During a week of trials the logged moisture values jump between 31% and 79% within seconds even though the soil has not changed; the pump chatters on and off around the threshold, noticeably worse when the pump is running; the Pico sometimes reboots at the moment the pump starts; and twice the system watered at 11 pm despite the light rule. The group has one week and a $40 budget before the holidays, when the greenhouse will be unattended for three weeks.

1. Explain why the moisture readings are unreliable and describe how to change the program so the pump decision is dependable.

Read the worked answer

The program is treating a single analog sample as if it were exact. A capacitive probe drifts with temperature and supply voltage, the Pico's ADC has several counts of noise, and the pump motor injects electrical noise into the wiring. A swing from 31% to 79% in seconds cannot be real soil moisture - soil changes over hours - so the readings are noise, not information.

Three code changes, each aimed at a different failure:

  1. Take nine samples about 100 ms apart and use the median. The median rejects the occasional wild spike that an average would smear into the result.
  2. Replace the single threshold with hysteresis: switch on below 40%, switch off only above 55%. The gap must be wider than the remaining noise band, or chatter returns. Add a minimum on-time and a cooldown so the pump cannot cycle.
  3. Sample every 15 minutes, not every 2 seconds. Soil moisture changes slowly; frequent switching wears the relay and wastes water.

Also discard readings taken while the pump is running, and log both raw and filtered values so thresholds can be re-tuned from evidence rather than guesswork.

2. Explain the two remaining faults - the reboots when the pump starts, and the watering at 11 pm - and what should change in both hardware and code.

Read the worked answer

Two separate faults: one electrical, one logical.

Reboots. The pump motor is an inductive load. Switching it off produces a back-EMF spike, and starting it draws an inrush current well above its running current. On a shared supply this browns out the Pico. Fixes: a flyback diode across the pump motor and across the relay coil, a separate 12 V supply for the pump with a single common ground point, and a large capacitor (470–1000 µF) across the Pico's supply rails. Route sensor wiring away from the pump wiring.

Night watering. Either the light rule is checked once per loop and then forgotten during a long pump cycle, or the reboot resets variables to defaults that permit watering. Fix the logic so the light level is re-checked immediately before the relay is energised, and set the post-reset default to pump off. Log a timestamp with every decision so you can prove which cause it was.

Before three unattended weeks, add a maximum run time and a daily maximum. A stuck sensor must not be able to flood the greenhouse or burn out the pump.

3. Describe how you would test and verify the system before leaving it unattended for three weeks.

Read the worked answer

Test the decision logic before it is trusted with water.

  1. Hardware-in-the-loop simulation. Replace the moisture probe with a potentiometer, or feed recorded values from a script: a dry-down curve plus the noisy samples logged this week. Confirm the pump switches at the intended thresholds and never chatters.
  2. Dry run. Swap the pump for an LED or a relay with a buzzer, run for 24 hours, and compare the decision log against what should have happened at each timestamp.
  3. Failure injection. Unplug the sensor and check what the ADC reads - does the code treat a disconnected probe as 'very dry' and water forever? Cover the photoresistor and confirm night lockout holds. Pull power mid-cycle and confirm the safe default on restart.
  4. Written acceptance criteria, agreed in advance: pump starts only inside the light window, fewer than 10 on/off cycles per day, no more than a set number of litres per day.

Keep the log as evidence, and leave a printed card in the greenhouse with a manual override and a contact number in case the class's assumptions are wrong.