All tutorials

2 · Inputs & sensors

Threshold with hysteresis

Advanced20 min

Goal

Stop the output from flickering when the reading hovers around the threshold.

Why it matters

Two thresholds (high and low) stabilise any real control loop: thermostat, night light, alarm.

Steps

  1. 01Define LOW_THRESHOLD and HIGH_THRESHOLD (e.g. 380 and 420).
  2. 02Switch on only below LOW_THRESHOLD, off only above HIGH_THRESHOLD.
  3. 03Remember the current state between loop iterations.

This tutorial runs inside the Circuitly simulator: your wiring and your code are checked automatically at every step.

Next tutorials