NCEA L1 Digital Tech study materials
NCEA L1 Digital Tech study resources

Micro:bit watering system overwaters the bed

NCEA L1 Digital Tech · Put your knowledge to work in context.

All case studies

Micro:bit watering system overwaters the bed

A community garden group installs a micro:bit-controlled watering system in a raised bed. A soil-moisture sensor on pin 1 returns 0 (dry) to 1023 (wet). The code reads the sensor and, if the reading is below 300, switches a relay on pin 8 to run a 5 V pump for 10 seconds. In the first week the bed is overwatered, and the pump sometimes runs at night when the bed is already damp. The relay also sometimes clicks randomly when a nearby strimmer motor starts. A second sensor in a drier bed reads 260 while the first reads 430, although the two beds were watered the same day. The group has one spare relay, a 5 V power supply, and no budget for new sensors before the planting day.

1. The pump overwaters the bed. Explain why a single reading below 300 does not reliably mean the soil is dry, and describe two code changes that would reduce overwatering without buying new parts.

Read the worked answer

One analogue reading is a sample, not a measurement. Soil-moisture sensors are noisy, their output drifts with temperature and supply voltage, and the reading depends on how deep and how firmly the probe sits. A single low spike can trigger a 10-second pump cycle even when the bed is damp, so the system waters on noise rather than on soil condition.

Two code changes are enough to make a real difference. First, average several readings taken over a few seconds, discard the highest and lowest, and require the average to be below the threshold. That removes short spikes without new hardware. Second, add hysteresis and a cooldown: start watering only below 280, stop at 380, and refuse to water again for at least two hours. Hysteresis stops the pump cycling around one threshold, and the cooldown limits the total water if the sensor is faulty.

If a light-level reading or a simple timer is available, restrict watering to daylight as well. Together these changes address the overwatering and the night runs.

2. The relay clicks randomly when the strimmer motor starts. Explain the most likely cause and what the group should do with the parts they have.

Read the worked answer

The random clicking when the strimmer starts points to electrical noise on the relay control line, not to a logic error in the code. A motor generates electromagnetic interference, and a high-impedance or floating signal line can pick it up as false pulses. The relay coil also produces a back-EMF spike when it switches off, which can disturb the micro:bit.

With the parts on hand, the group should keep the relay wires short and twisted and route them away from the pump's mains cable. Fitting a pull-down resistor, around 10 kΩ, from the signal pin to GND holds the pin low when it is not being driven, so noise is much less likely to be read as a high signal. Use the separate 5 V supply for the relay and pump, with a common ground, rather than powering the relay from the micro:bit. Check that the relay module has a flyback diode across the coil, as most modules do.

The group must not drive the relay coil directly from a micro:bit pin, because the pin cannot supply enough current. Software can set the pin low at startup and re-assert state, but it cannot fix a floating input.

3. Two sensors give very different readings in beds watered on the same day. Should the group trust the numbers and set a different threshold for each sensor, or calibrate first? Justify your answer.

Read the worked answer

Do not treat the two numbers as directly comparable yet. A reading of 260 versus 430 in beds watered on the same day can come from different soil mixes, probe depth, probe contact, sensor age or corrosion. Both sensors may be working correctly but scaled differently, so setting thresholds from these two numbers would encode an unknown error into the code.

The correct first step is a short calibration. Take readings in each bed when the soil is visibly dry, then again after a thorough watering, and record the range. Choose a threshold for each bed midway between its dry and wet readings, or convert raw values to a percentage. Only then should the code use different thresholds, and the group should write the calibration values down so they can be repeated next season.

Calibrating first avoids the trap of 'fixing' the software to match an unverified number and then overwatering the drier bed. If a sensor barely changes between dry and wet, it is faulty and should be replaced or excluded, not compensated for in code.