Build an ESP32 alarm that tolerates noisy readings

Use separate on/off thresholds and report measurement faults without losing alarm state.

Guided walkthrough·Intermediate·3 min read

Opening the Python editor…

Requirements and learning goals

Before you start

  • ESP32-C6 project from the first app lesson
  • Potentiometer on GPIO 0 and external LED on GPIO 11 from the PWM lesson

You will be able to

  • Implement hysteresis on the ESP32
  • Publish timestamped readings and explicit faults in logs

Stop the alarm flickering

A sensor near one threshold can switch an alarm on and off with every noisy reading. Hysteresis uses different thresholds to enter and leave alarm. Readings between them keep the previous state.

This makes a demo's behavior predictable. The ESP32 makes the decision locally, so closing a dashboard cannot stop the alarm. A potentiometer stands in for a sensor; these thresholds are raw counts, not temperature or voltage limits.

Send a copy of these measurements to an edge dashboard, while keeping the alarm decision local when the network disconnects.

Run the alarm

Use the PWM lesson's wiring: knob to 3.3V/GND with its middle terminal on 0; external LED through 330 ohms on GPIO 11.

In the first app's project, save the alarm example as main/main.c and the component configuration as main/CMakeLists.txt. Keep the other files and wendy_core dependency.

wendy run

The important decision is small:

if (valid) {
    if (raw >= 2800) alarm = true;
    else if (raw < 2400) alarm = false;
}

Invalid samples leave the remembered alarm state unchanged, but the output reports FAULT. The LED stays on for either a fault or an alarm; the log distinguishes them.

Checkpoint 1 of 2

The LED is lit and the latest log line reports FAULT. What do you know about the remembered alarm state?

Commit to an answer. Every choice explains itself.

Expected result

Turn the knob above 2800 counts: the LED lights. Reduce to 2600: it stays lit. Drop below 2400: it turns off. Returning to 2600 now leaves it off.

Logs include the count, validity, state, sequence number, and capture time in milliseconds since boot. Sequence and time restart after a reboot. A dashboard can use them to notice missing updates.

Checkpoint 2 of 2

The alarm is off. Four valid but noisy readings arrive in order: 2790, 2810, 2790, 2810. How many times does the alarm state change?

Commit to an answer. Every choice explains itself.

Troubleshooting

To test faults, temporarily set valid = false after the ADC read. Expect FAULT and raw: null, then restore the code. Unplugging the knob is not a reliable fault test: a floating ADC pin can still return plausible counts.

A real sensor needs its own validity checks and a deadline for missing samples. Keep network publishing separate from this local decision.

Reference: ESP32-C6 ADC readings.

Can you do each of these?

  • Implement hysteresis on the ESP32
  • Publish timestamped readings and explicit faults in logs

0 of 2 checkpoints solved. Try checkpoint 1

Previous: Handle ESP32 interrupts and detect stalled work
All lessons in this topic
  1. 1. Install Wendy Lite on your ESP32
  2. 2. Update an ESP32 app over Wi-Fi
  3. 3. Find your ESP32 on Wendy Net
  4. 4. Use ESP32 sensors from Linux and ROS 2
  5. 5. Run your first ESP32 app
  6. 6. Read a button, sample voltage, and drive PWM
  7. 7. Connect ESP32 sensors with I²C, SPI, and UART
  8. 8. Handle ESP32 interrupts and detect stalled work
  9. 9. Build an ESP32 alarm that tolerates noisy readings

Ask Wendy

Answers about Wendy and WendyOS

What would you like to know?

Ask about devices, development, and deployment.

Check linked docs for technical details.