AI agents & screen readers: for a machine-readable, text-only catalogue, start at /llms.txt. Products are available as Markdown (/products.md, /products/{handle}.md) and JSON (/products.json, /products/{handle}.json).
Store
Your lesson ≈ 360 min

Lesson 5 · Weeks 8-10

Control, feedback and code

What you'll learn

Make a system decide: read a sensor, apply a threshold, drive an output, and stop it chattering.

What you're doing today

  • Make an output respond to a sensor crossing a threshold you choose.
  • Find the chattering problem, then fix it with hysteresis and show the before and after.
  • Build the same task open-loop and closed-loop, and describe how they behave differently when conditions change.
  • Complete PRP 5, PRP 6 and PRP 7.
  • Write down your threshold value and the evidence you used to choose it.
PRP 5

Threshold control — making a decision

Now the system decides for itself.

  1. Choose a threshold from your own recorded readings, not from an example.
  2. Write the decision: if the reading is above the threshold do one thing, otherwise do another.
  3. Test it by changing conditions until it switches.
  4. Find the fault. Hold the sensor right at the threshold. The output will flicker on and off rapidly. This is chattering, and every real control system has to deal with it.
  5. Record it happening before you fix it.
  6. Justify your threshold in writing, referring to the readings you took.

What you'll learn

Make a system act on its own reading, and justify the threshold.

What you'll make

My threshold comes from my recorded data and I can defend the number.

What you need

Student kit, a sensor from PRP 3 or 4, LED.

Build it up — 13 steps

Wiring the sensor and the output it switches · plays 13 steps Open · Download .fz

Sample code

vce_u1_05_threshold.ino · Threshold control — PRP 5 ✦ English View Raw
// PRP 5 — a single threshold decision.
//
// THRESHOLD is a design decision. Replace this value with one you
// chose from your own recorded readings, and be able to defend it.
//
// Expect this sketch to chatter when the reading sits near the
// threshold. That fault is the point of the activity — PRP 6 fixes it.

const int SENSOR_PIN = A0;
const int OUTPUT_PIN = 13;
const int THRESHOLD  = 500;   // <-- your number, from your data

void setup() {
  pinMode(OUTPUT_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(SENSOR_PIN);
  Serial.println(reading);

  if (reading < THRESHOLD) {
    digitalWrite(OUTPUT_PIN, HIGH);
  } else {
    digitalWrite(OUTPUT_PIN, LOW);
  }

  delay(100);
}
Quick chips

IPO chart

Inputs

  • A sensor reading from PRP 3 or PRP 4

Processing

  • An if/else comparison against a threshold the student chooses

Outputs

  • An LED that changes state when the threshold is crossed
Stuck? Common things to check Tap to open

That the threshold is arbitrary. It is the central design decision in the whole system.

Take it slower

Give the working sensor code and have the student write only the if/else decision.

Push further

Add a second condition so the output acts only when both are met, and explain why that suits your project better than either alone.

Done when…

A working threshold decision, evidence of chattering, and a written justification.

Reflect

What conditions would make your threshold the wrong choice?

PRP 6

Hysteresis, and open versus closed loop

Fix the chattering you found in PRP 5, then compare the two ways a system can be controlled.

  1. Add hysteresis. Use two thresholds with a gap between them: switch on below one value, off above a higher one.
  2. Test at the switching point again and show the chattering has gone.
  3. Record before and after — this comparison is strong evidence.
  4. Build the same task open-loop: act on a fixed timer with no sensor at all.
  5. Change the conditions and compare. The open-loop version keeps doing the same thing regardless. The closed-loop version adapts.
  6. Write down which is appropriate for your project and why. Open-loop is not always wrong — it is simpler, cheaper and cannot be fooled by a failed sensor.

What you'll learn

Fix chattering with hysteresis, and choose between open and closed loop deliberately.

What you'll make

I can show before and after evidence, and say when open-loop would be the better choice.

What you need

Student kit, sensor, LED.

Build it up — 13 steps

The same circuit as PRP 5 — hysteresis is a change in code, not in wiring · plays 13 steps Open · Download .fz

Sample code

vce_u1_06_hysteresis.ino · Hysteresis — PRP 6 ✦ English View Raw
// PRP 6 — hysteresis.
//
// One threshold chatters because the sensor's own noise crosses it
// repeatedly. Two thresholds with a gap wider than that noise fixes it.
//
// Make the gap wider than the noise you measured in PRP 4, and no wider.

const int SENSOR_PIN = A0;
const int OUTPUT_PIN = 13;
const int TURN_ON_BELOW  = 480;   // <-- your numbers
const int TURN_OFF_ABOVE = 520;

bool outputOn = false;

void setup() {
  pinMode(OUTPUT_PIN, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(SENSOR_PIN);

  if (!outputOn && reading < TURN_ON_BELOW) {
    outputOn = true;
  } else if (outputOn && reading > TURN_OFF_ABOVE) {
    outputOn = false;
  }
  // Between the two thresholds nothing changes. That is the whole trick.

  digitalWrite(OUTPUT_PIN, outputOn ? HIGH : LOW);
  Serial.println(reading);
  delay(100);
}
Quick chips

IPO chart

Inputs

  • The same sensor, near the switching point

Processing

  • Two thresholds instead of one: switch on at a lower value, off at a higher value

Outputs

  • An output that switches cleanly and stays switched
Stuck? Common things to check Tap to open

That closed-loop is always better. A failed sensor makes closed-loop worse than useless.

Take it slower

Supply the hysteresis sketch with the two thresholds left blank for the student to fill from their own data.

Push further

Find the smallest gap that still works reliably and justify it against the noise floor from PRP 4. Too wide is a fault too, not a safe default.

Done when…

Hysteresis working with before/after evidence, plus an open-loop comparison.

Reflect

What happens to your closed-loop system if the sensor is disconnected?

PRP 7

Automatic watering — sensing, deciding, acting

The bridge to your project: a complete system that senses a resource, decides, and acts on the physical world.

  1. Calibrate the moisture sensor in dry soil, damp soil and saturated soil. Record all three.
  2. Choose a threshold from those readings and justify it.
  3. Drive the pump through the driver module. A board output pin supplies tens of milliamps; the pump wants hundreds. Wiring it straight to a pin destroys the pin, and often the board.

The driver module sits between them: the board sends a small control signal, and the module switches the pump's own supply. Three things you need to know about why it is built the way it is:

  • The pump's power does not come from the board. It comes from a separate supply into the module's screw terminals. The board and the pump supply must share a common ground or the control signal has no reference.
  • A motor is an inductor. When you switch it off, the collapsing magnetic field produces a large reverse voltage spike that will destroy an unprotected switching device. The module has flyback diodes built in to clamp it — which is exactly why you use a module rather than wiring a bare transistor.
  • Check the pump's current draw against the module's rating before you connect it. This module handles several amps; your pump will want far less.
    1. Add hysteresis so it does not cycle at the switching point.
    2. Add a maximum run time. If the sensor fails or falls out of the soil, an unlimited pump empties the reservoir onto the bench. This is a safety and reliability decision and it belongs in your record.
    3. Run it for a full lesson and record what happened.
    4. Measure the saving. How much water does it use compared with a fixed timer over the same period?

What you'll learn

Build a complete sense-decide-act system that fails safely.

What you'll make

My system runs unattended for a lesson, and cannot flood the bench if the sensor fails.

What you need

Soil moisture sensor, pump, driver, tray, soil, reservoir, student kit.

Build it up — 22 steps

Wiring the watering system · plays 22 steps Open · Download .fz

Sample code

vce_u1_07_watering.ino · Automatic watering — PRP 7 ✦ English View Raw
// PRP 7 — automatic watering.
//
// Pump is driven through a motor driver module, NOT directly from a pin.
// A board pin supplies tens of milliamps; the pump wants hundreds.
//
// MAX_RUN_MS is the safety limit. If the sensor falls out of the soil or
// fails open, an unlimited pump empties the reservoir onto the bench.
// A control system that cannot fail safely is not finished.

const int MOISTURE_PIN = A0;
const int PUMP_PIN     = 9;      // to the driver module input

const int DRY_BELOW  = 400;      // <-- your calibrated numbers
const int WET_ABOVE  = 600;
const unsigned long MAX_RUN_MS  = 15000UL;   // never pump longer than this
const unsigned long REST_MS     = 60000UL;   // let water soak in before re-reading

bool pumping = false;
unsigned long pumpStartedAt = 0;
unsigned long pumpStoppedAt = 0;

void setup() {
  pinMode(PUMP_PIN, OUTPUT);
  digitalWrite(PUMP_PIN, LOW);
  Serial.begin(9600);
}

void loop() {
  int moisture = analogRead(MOISTURE_PIN);
  unsigned long now = millis();

  if (pumping) {
    bool wetEnough = moisture > WET_ABOVE;
    bool runTooLong = (now - pumpStartedAt) >= MAX_RUN_MS;

    if (wetEnough || runTooLong) {
      digitalWrite(PUMP_PIN, LOW);
      pumping = false;
      pumpStoppedAt = now;
      if (runTooLong) {
        Serial.println("STOPPED ON RUN LIMIT - check the sensor");
      }
    }
  } else {
    bool dry = moisture < DRY_BELOW;
    bool rested = (now - pumpStoppedAt) >= REST_MS;

    if (dry && rested) {
      digitalWrite(PUMP_PIN, HIGH);
      pumping = true;
      pumpStartedAt = now;
    }
  }

  Serial.println(moisture);
  delay(500);
}
Quick chips

IPO chart

Inputs

  • Soil moisture, measured in a tray of soil

Processing

  • Threshold with hysteresis, plus a maximum run time as a safety limit

Outputs

  • A pump that runs only when the soil is genuinely dry
Stuck? Common things to check Tap to open

That the job is done when the pump runs. The job is done when it behaves correctly on the day something breaks.

Take it slower

Provide the watering sketch complete, and have the student calibrate the sensor and choose the three numbers. That is still the whole design decision.

Push further

Guard a second failure mode — a dry reservoir, or a reading that has not changed in an hour — and decide what the system should do in each case.

Done when…

Calibrated sensor, justified threshold, hysteresis, a maximum run time, and a measured comparison against a timer.

Reflect

List three ways this system could fail, and what it should do in each case.

Maddy, co-founder of Little Bird

Need help? We're here for you!

Hi, I'm Maddy. My team and I are ready to help with your order or any questions.