Build a fridge alarm that tells Blynk when the door is left open

A reed switch notices when the fridge door opens and a DS18B20 measures the temperature inside. The fridge beeps when the door stays open, and Blynk warns you about an open door or a warm fridge.
  • Level:Advanced
  • Duration: 1 h 10 min
  • Ages 12+
  • Platform: ESP32

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Build a fridge alarm that tells Blynk when the door is left open – EEMENTOR STEM™ lesson

A fridge door that doesn't close properly wastes energy and spoils food, and nobody notices until the milk is warm. This alarm keeps watch. A reed switch on the fridge and a magnet on the door notice every opening. If the door stays open for 30 seconds, the fridge starts to beep; after a minute, Blynk sends a notification to your phone. A DS18B20 inside measures the temperature, so you also hear about it when the fridge gets too warm, for example after a power cut. And the app counts how many times the fridge was opened today.

What you'll learn

  • How a reed switch and a magnet sense an open door
  • How to read a DS18B20 over its one-wire bus
  • How to wait for a condition to last before warning
  • How to reset a counter at midnight with internet time

What you need

  • ESP32 board, expansion board and USB cable (ESP32 IoT Starter Kit)
  • Reed switch module (45-in-1 Sensor Kit)
  • 1 small magnet, about 10 mm across (a craft or fridge magnet)
  • DS18B20 temperature module (45-in-1 Sensor Kit)
  • Passive buzzer (ESP32 IoT Starter Kit)
  • 3 long, thin jumper wires for the DS18B20 (about 1 m)
  • A free Blynk account and the Blynk IoT app on a phone
  • A 2.4 GHz Wi-Fi network
  • The printed parts from the 3D printing box below (sensor case and magnet holder), or the cardboard version described there
  • Breadboard and jumper wires (ESP32 IoT Starter Kit)

Wiring

Breadboard wiring diagram: Build a fridge alarm that tells Blynk when the door is left open
Breadboard view. Check each connection against the wiring table below.
From To
Reed switch module S ESP32 GPIO 27
Reed switch module + and − 3V3 and GND
DS18B20 module S ESP32 GPIO 33
DS18B20 module + and − 3V3 and GND
Passive buzzer + ESP32 GPIO 26
Passive buzzer − GND

Sensor modules from different makers can label and order their pins differently. Always follow the labels printed on your module.

Thin jumper wires squeeze flat under the door seal without letting cold out. Check that the door still closes tightly.

Set up Blynk

In the Blynk console, create a new template called Fridge Alarm (hardware ESP32, connection type Wi-Fi) and add these datastreams. Lesson 1 of this path shows each step in detail.

Virtual pin Name Data type Range Phone widget
V0 Temperature Double -10–30 Gauge
V1 Door String – Label
V2 Openings today Integer 0–500 Value display

Then open the template's Events & Notifications tab, add an event with the code door_open (name: Fridge door open) and turn on push notifications for it. The event code must match the sketch exactly.

Save the template, create a new device from it and copy its three BLYNK_… lines. In the phone app, open the device, tap the wrench icon and add a Gauge (V0), a Label (V1) and a Value display (V2). Link each widget to its datastream.

Add a second event with the code fridge_warm (name: Fridge too warm) and turn on notifications for it too.

Example screen for: Build a fridge alarm that tells Blynk when the door is left open
Example screen with sample values. Yours may look different.

The sketch

Replace the three BLYNK_… lines at the top with the ones from your device's Device info page, and add your Wi-Fi name and password (2.4 GHz only). The BLYNK_… lines must stay above #include <BlynkSimpleEsp32.h>.

In the Arduino IDE, open Library Manager and install OneWire by Paul Stoffregen and DallasTemperature by Miles Burton.

Change TIME_ZONE to your own time zone. The example is for Central Europe; search for "POSIX time zone" and your city to find yours (for example EET-2EEST,M3.5.0/3,M10.5.0/4 for Estonia or GMT0BST,M3.5.0/1,M10.5.0 for the UK).

Start the sketch with the fridge door closed: it uses that reading as "closed".

#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Fridge Alarm"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#define BLYNK_PRINT Serial          // show connection messages in the Serial Monitor

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <time.h>

char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
const char* TIME_ZONE = "CET-1CEST,M3.5.0,M10.5.0/3";   // your time zone

const int REED_PIN = 27;
const int TEMP_PIN = 33;
const int BUZZER_PIN = 26;
const int BEEP_AFTER = 30;                 // seconds open before it beeps
const int ALERT_AFTER = 60;                // seconds open before the notification
const float WARM = 8.0;                    // a fridge should stay below 8 C
const int WARM_MINUTES = 15;               // ...for this long before a warning

OneWire oneWire(TEMP_PIN);
DallasTemperature sensor(&oneWire);
BlynkTimer timer;
int closedReading;                         // the reed reading with the door shut
bool doorOpen = false, openSent = false, beeping = false;
unsigned long openedAt = 0;
int openings = 0, warmFor = 0, today = -1;
bool warmSent = false;

void door() {                              // 5 times a second
  bool nowOpen = digitalRead(REED_PIN) != closedReading;
  if (nowOpen && !doorOpen) {
    openedAt = millis();
    openings++;
    Blynk.virtualWrite(V1, "Open");
    Blynk.virtualWrite(V2, openings);
  }
  if (!nowOpen && doorOpen) {
    Blynk.virtualWrite(V1, "Closed");
    openSent = false;
  }
  doorOpen = nowOpen;

  unsigned long seconds = doorOpen ? (millis() - openedAt) / 1000 : 0;
  bool beep = doorOpen && seconds >= BEEP_AFTER && (millis() / 500) % 2;
  if (beep != beeping) {                   // only change the buzzer when needed
    beeping = beep;
    if (beep) tone(BUZZER_PIN, 2000);
    else noTone(BUZZER_PIN);
  }
  if (doorOpen && seconds >= ALERT_AFTER && !openSent) {
    Blynk.logEvent("door_open", "The fridge door has been open for a minute.");
    openSent = true;
  }
}

void everyMinute() {
  sensor.requestTemperatures();
  float t = sensor.getTempCByIndex(0);
  if (t != DEVICE_DISCONNECTED_C) {
    Blynk.virtualWrite(V0, t);
    warmFor = (t > WARM) ? warmFor + 1 : 0;
    if (warmFor >= WARM_MINUTES && !warmSent) {
      Blynk.logEvent("fridge_warm", String("The fridge has been at ") + String(t, 1) + " C for 15 minutes.");
      warmSent = true;
    }
    if (t < WARM - 1) warmSent = false;    // cold again: ready for the next warning
  }

  time_t now = time(nullptr);              // new day: start counting again
  if (now > 1700000000) {
    struct tm local;
    localtime_r(&now, &local);
    if (today != -1 && local.tm_yday != today) {
      openings = 0;
      Blynk.virtualWrite(V2, 0);
    }
    today = local.tm_yday;
  }
}

BLYNK_CONNECTED() {
  Blynk.virtualWrite(V1, doorOpen ? "Open" : "Closed");
  Blynk.virtualWrite(V2, openings);
}

void setup() {
  Serial.begin(115200);
  pinMode(REED_PIN, INPUT);
  closedReading = digitalRead(REED_PIN);
  sensor.begin();
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  configTzTime(TIME_ZONE, "pool.ntp.org");
  timer.setInterval(200L, door);
  timer.setInterval(60000L, everyMinute);
  everyMinute();
}

void loop() {
  Blynk.run();
  timer.run();
}

Try it

  1. Open the fridge: the app says "Open" and the counter goes up. Close it: "Closed".
  2. Leave the door open: after 30 seconds the fridge beeps, after a minute your phone gets "Fridge door open". Close the door and the beeping stops.
  3. Watch the temperature over a day. Most fridges sit between 3 and 5 °C, and rise for a while after the door has been open.

How it works

A reed switch is two thin metal strips in a glass tube. A magnet nearby pulls them together and closes the switch. Some modules give HIGH with the magnet and some LOW, so the sketch reads the switch at the start, with the door shut, and calls anything different "open".

The DS18B20 talks over a one-wire bus: power, ground and a single data wire. It measures the temperature itself and sends the result as a number, so long wires don't make the reading less accurate, unlike an analog sensor.

Neither alarm fires at the first sign of trouble. The beep waits 30 seconds, because opening the door to take something out is normal. The warm warning needs 15 readings in a row above 8 °C, because the temperature always rises for a few minutes after the door was open. warmFor counts those minutes and goes back to 0 as soon as it is cold again.

At midnight the day number from internet time changes, and the openings counter starts again at 0.

Try this next

  • Send the number of seconds the door was open in total today to the app.
  • Add a Superchart to see the temperature over a week.
  • Use a second reed switch for the freezer door.

Troubleshooting

  • The device shows "Offline" in the app: check the Serial Monitor at 115200 baud. "Invalid auth token" means the BLYNK_AUTH_TOKEN line is wrong; if it never connects, check the Wi-Fi name and password, and that your network is 2.4 GHz.
  • It says "Open" with the door shut: the magnet is too far from the reed. Bring it within 10 mm, and restart the ESP32 with the door shut.
  • The temperature shows -127: the DS18B20 isn't answering. Check its three wires, especially where they pass the door seal.
  • No notifications: check both event codes, door_open and fridge_warm, and that notifications are on.
  • A printed part is too tight or too loose: printers differ by a few tenths of a millimetre. Sand or file a tight hole, and fix a loose part with a drop of hot glue or a strip of tape.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

ESP32 IoT Starter Kit | Arduino IDE Compatible | Free Tutorials $57.00

Shop the kit