A simple alarm says "beep if the temperature goes above 8 °C". But every fridge is different, every sensor is a little off, and the right limit for one is wrong for another. Smart devices often work another way: they first learn what's normal, then notice when something isn't. That's called anomaly detection, and it's a real machine-learning technique, used to spot broken machines, strange bank payments and failing fridges. In this lesson you build a fridge watchdog with the UNO and a DHT11. For the first minute it only watches and learns. After that, the green LED means "all normal", and if the temperature does something unusual, like when the door is left open, the red LED and the buzzer go off.
What you'll learn
- What anomaly detection is, and why "normal" is learned instead of hard-coded
- How to calculate a running average and standard deviation, one reading at a time
- What a z-score is, and why more than 3 counts as unusual
- How to avoid false alarms by waiting for three unusual readings in a row
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- DHT11 temperature and humidity sensor (UNO R3 Starter Kit)
- 1 green LED, 1 red LED and 2 resistors, 220 Ω (UNO R3 Starter Kit)
- Active buzzer (UNO R3 Starter Kit)
- Breadboard and jumper wires (UNO R3 Starter Kit)
- The free Arduino IDE and the DHT sensor library by Adafruit from the Library Manager
Wiring
The DHT11 has three pins: S (signal), + and −. Follow the labels on your module. Each LED gets its own 220 Ω resistor. The active buzzer's longer leg (+) goes to the pin.

| From | To |
|---|---|
| DHT11 S | UNO pin 2 |
| DHT11 + and − | The red (+) rail and the blue (−) rail |
| UNO pin 6 | 220 Ω resistor → green LED long leg (+) |
| UNO pin 7 | 220 Ω resistor → red LED long leg (+) |
| UNO pin 8 | Active buzzer + (longer leg) |
| LED short legs (−) and buzzer − | The blue (−) rail |
| UNO 5V and GND | The red (+) rail and the blue (−) rail |
Build this circuit in EEMENTOR Lab
Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.
Build it yourself
- The UNO and this lesson’s parts are already placed. Wire them using the wiring table above. Pins with no wire show red.
- Missing a part? Add it from the parts list on the left.
- Press Run, then try the inputs: click, hold or slide them under Selected.
- Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.
EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab
The sketch
Copy the sketch into the Arduino IDE, choose Arduino Uno as the board and the right port, and click Upload. Then open the Serial Monitor at 9600 baud.
// Fridge watchdog: learns what is normal, then warns when something is unusual
#include <DHT.h>
const int DHT_PIN = 2;
const int GREEN_PIN = 6;
const int RED_PIN = 7;
const int BUZZER_PIN = 8;
const unsigned long READ_EVERY = 2000; // ms between readings (the DHT11 allows one per second)
const int LEARN_COUNT = 30; // readings used to learn what "normal" is
const float Z_LIMIT = 3.0; // how unusual a reading must be, in standard deviations
const int STRIKES_NEEDED = 3; // unusual readings in a row before the alarm
const float MIN_SPREAD = 0.5; // DHT11 readings are coarse: never use a spread below 0.5 C
DHT dht(DHT_PIN, DHT11);
int n = 0; // readings learned so far
float mean = 0; // running average (Welford's method)
float m2 = 0; // running sum of squared differences
int strikes = 0;
bool alarm = false;
unsigned long lastRead = 0;
float spread() { // the standard deviation, with a minimum
float sd = sqrt(m2 / (n - 1));
return sd > MIN_SPREAD ? sd : MIN_SPREAD;
}
void setup() {
pinMode(GREEN_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
dht.begin();
Serial.println("Learning what is normal. Leave the sensor where it will live...");
}
void loop() {
// Outputs: green = normal, red + beeping buzzer = alarm
bool beep = alarm && (millis() / 250) % 2;
digitalWrite(RED_PIN, alarm ? HIGH : LOW);
digitalWrite(BUZZER_PIN, beep ? HIGH : LOW);
if (millis() - lastRead < READ_EVERY) return;
lastRead = millis();
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println("No reading: check the DHT11 wiring");
return;
}
if (n < LEARN_COUNT) { // 1. Learning: update the average and the spread
n++;
float delta = t - mean;
mean += delta / n;
m2 += delta * (t - mean);
digitalWrite(GREEN_PIN, n % 2); // green blinks while learning
Serial.print("Learning ");
Serial.print(n);
Serial.print("/");
Serial.print(LEARN_COUNT);
Serial.print(": ");
Serial.print(t, 1);
Serial.println(" C");
if (n == LEARN_COUNT) {
Serial.print("Normal is ");
Serial.print(mean, 1);
Serial.print(" C, give or take ");
Serial.print(spread(), 1);
Serial.println(" C. Now watching.");
}
return;
}
float z = (t - mean) / spread(); // 2. Watching: how unusual is this reading?
if (abs(z) > Z_LIMIT) strikes++;
else strikes = 0;
alarm = strikes >= STRIKES_NEEDED;
if (strikes == 0) mean += 0.01 * (t - mean); // keep learning slowly while all is normal
digitalWrite(GREEN_PIN, alarm ? LOW : HIGH);
Serial.print(t, 1);
Serial.print(" C z = ");
Serial.print(z, 1);
if (alarm) Serial.println(" ALARM: something is unusual!");
else if (strikes > 0) Serial.println(" unusual...");
else Serial.println(" normal");
}
Try it
- Upload the sketch and leave the sensor alone. For one minute (30 readings, one every 2 seconds) the green LED blinks and the Serial Monitor says "Learning".
- When it prints "Normal is ... give or take ...", the green LED stays on. Readings near the normal value get a z-score close to 0.
- Now pretend the fridge door is open: hold the DHT11 in your hand or breathe on it. After a few seconds you see "unusual...", and on the third unusual reading in a row the red LED lights and the buzzer beeps.
- Let go. When the temperature comes back towards normal, the alarm stops by itself.
- For the real thing, power the UNO from a USB power bank inside the fridge, upload with it at room temperature, then put it in the fridge and press the UNO's reset button so it learns the fridge's normal.
What you should see

How it works

Learning normal: during the first 30 readings, the sketch keeps two numbers up to date: the mean (the average) and the standard deviation (how much the readings usually wobble around it). It uses Welford's method, which updates both with each new reading, so the UNO never needs to store the readings themselves: important on a board with only 2 KB of memory.
Spotting the unusual: for every new reading, the z-score says how many standard deviations it is from the mean: z = (reading − mean) ÷ standard deviation. If the readings follow the usual bell-shaped curve, about 99.7 % of normal readings land within 3 standard deviations, so a z-score above 3 (or below −3) is a sign that something has changed. The DHT11 is only accurate to about ±2 °C and gives quite coarse readings, so the sketch never lets the spread shrink below 0.5 °C; otherwise a fridge that always reads exactly 4.0 would panic at 5.0.
No false alarms: a single odd reading can be a glitch, so the alarm needs three unusual readings in a row. And while everything is normal, the mean keeps creeping 1 % towards each new reading, so the watchdog slowly adapts if the room gets warmer in summer. A fixed limit can't do that. That's the big idea of anomaly detection: the machine learns "normal" from data, and you don't have to guess the right number. Food-safety advice in the UK is to keep a fridge between 0 and 5 °C, and this watchdog tells you when yours stops behaving like itself.
Try this next
- Watch the humidity too, with its own mean and spread. Opening the door changes the humidity before the temperature.
- Show the normal value, the reading and the z-score on the 1602 LCD from the LCD lesson.
- Change
Z_LIMITto 2 and to 4, and see how it changes the number of alarms. Which would you choose for a fridge full of medicine? - Explore another kind of machine learning: let the ESP32 find groups on its own with k-means.
Troubleshooting
- "No reading: check the DHT11 wiring": S must go to pin 2, + to 5V and − to GND, and the DHT sensor library must be installed.
- It raises the alarm straight after learning: the sensor was still warming up or cooling down while it learned. Press reset and let it learn again once the temperature is steady.
- It never raises the alarm: warm the sensor more, or for longer: it needs three unusual readings in a row, 6 seconds in total.
-
The buzzer is silent but the red LED lights: the buzzer is the wrong way round (longer leg to pin 8), or it is a passive buzzer, which needs
tone()instead. -
Readings only change in whole degrees: many DHT11 sensors measure in steps of 1 °C. That's why
MIN_SPREADis there.