Build a cooking guard that warns when the hob is left alone

Leaving the kitchen while something is cooking starts many house fires. This guard notices a flame on the hob and checks that someone is nearby. If the cook has been away too long, it warns first and then sounds the...
  • Level:Advanced
  • Duration: 1 h 30 min
  • Ages 12+
  • Platform: Arduino

Tools for this lesson

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

Build a cooking guard that warns when the hob is left alone – EEMENTOR STEM™ lesson

Fire services say that cooking left unattended is one of the most common causes of fires at home. Somebody puts a pan on the hob, goes to answer the phone or the door, and forgets it. Smoke alarms only react once there is already smoke. This project tries to warn earlier. A flame sensor watches the hob, and a PIR motion sensor watches the kitchen. As long as someone moves around near the cooker, everything is fine. When the flame is on and nobody has been seen for a while, the guard flashes and beeps a warning. If still nobody comes, it sounds a loud alarm. One press on the button, or simply walking back into the kitchen, says "I'm here".

What you'll learn

  • How to combine two sensors into one decision
  • How to build a program as a state machine: off, cooking, warning, alarm
  • How to time "nobody seen for 5 minutes" with millis()
  • How to test a flame sensor safely with a TV remote

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • Flame sensor module (45-in-1 Sensor Kit)
  • PIR motion sensor HC-SR501 (UNO R3 Starter Kit)
  • Active buzzer (UNO R3 Starter Kit)
  • Green LED, red LED and 2 resistors, 220 Ω (UNO R3 Starter Kit)
  • 1 push button (UNO R3 Starter Kit)
  • A TV remote control, to test the flame sensor without fire
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Important: this is a learning project, not a certified safety device. Never leave cooking alone because of it, and keep your smoke alarms. Only test with real flames together with an adult.

Wiring

Breadboard wiring diagram: Build a cooking guard that warns when the hob is left alone
Breadboard view. Check each connection against the wiring table below.
From To
Flame sensor G GND
Flame sensor + 5V
Flame sensor DO UNO pin 2
PIR VCC 5V
PIR OUT UNO pin 3
PIR GND GND
Push button, one leg UNO pin 4
Push button, the diagonally opposite leg GND
UNO pin 5 220 Ω resistor, then the green LED's long leg
UNO pin 6 220 Ω resistor, then the red LED's long leg
Both LEDs' short legs GND
Active buzzer + (longer leg) UNO pin 8
Active buzzer − GND

The flame sensor's AO pin stays unused. Point the flame sensor at the hob from about 1 m away, out of the steam and the heat, and point the PIR sensor at the area where the cook stands.

The sketch

const int FLAME_PIN = 2, PIR_PIN = 3, BUTTON_PIN = 4;
const int GREEN_PIN = 5, RED_PIN = 6, BUZZER_PIN = 8;

// Short times to test it. For a real kitchen use 5 minutes (300000) and 2 minutes (120000).
const unsigned long AWAY_LIMIT = 30000;     // nobody seen this long: warning
const unsigned long ALARM_AFTER = 20000;    // still nobody after the warning: alarm
const unsigned long FLAME_MEMORY = 10000;   // the flame counts as "on" for 10 s after it was seen
const unsigned long COOK_REMINDER = 2700000; // a reminder beep every 45 minutes of cooking

enum State { OFF, COOKING, WARNING, ALARM };
State state = OFF;
const char* NAMES[] = {"Hob off", "Cooking", "WARNING: nobody here", "ALARM!"};

int flameRest;                     // the flame sensor's output when there is no flame
unsigned long lastFlame = 0, lastPerson = 0, cookingSince = 0, lastReminder = 0;

void setState(State s) {
  if (s == state) return;
  state = s;
  Serial.println(NAMES[s]);
  digitalWrite(BUZZER_PIN, LOW);
}

void imHere() {                    // someone is near the cooker
  lastPerson = millis();
  if (state == WARNING || state == ALARM) {
    setState(COOKING);
    digitalWrite(BUZZER_PIN, HIGH); // short "OK" chirp
    delay(60);
    digitalWrite(BUZZER_PIN, LOW);
  }
}

void setup() {
  Serial.begin(9600);
  pinMode(FLAME_PIN, INPUT);
  pinMode(PIR_PIN, INPUT);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  flameRest = digitalRead(FLAME_PIN);   // no flame at start-up: remember this level
  Serial.println("Cooking guard: give the PIR sensor a minute to settle");
}

void loop() {
  unsigned long now = millis();

  if (digitalRead(FLAME_PIN) != flameRest) lastFlame = now;
  bool flameOn = lastFlame != 0 && now - lastFlame < FLAME_MEMORY;
  if (digitalRead(PIR_PIN) == HIGH || digitalRead(BUTTON_PIN) == LOW) imHere();

  if (!flameOn) {
    setState(OFF);
  } else if (state == OFF) {
    setState(COOKING);
    cookingSince = now;
    lastReminder = now;
    lastPerson = now;              // whoever lit the hob is here
  } else {
    unsigned long away = now - lastPerson;
    if (away > AWAY_LIMIT + ALARM_AFTER) setState(ALARM);
    else if (away > AWAY_LIMIT) setState(WARNING);
  }

  // Lights and sound for each state
  bool blinkFast = (now / 150) % 2, blinkSlow = (now / 500) % 2;
  digitalWrite(GREEN_PIN, state == COOKING);
  digitalWrite(RED_PIN, state == ALARM ? blinkFast : state == WARNING ? blinkSlow : LOW);
  if (state == WARNING) digitalWrite(BUZZER_PIN, now % 2000 < 100);   // short beep every 2 s
  if (state == ALARM) digitalWrite(BUZZER_PIN, blinkFast);            // loud pulsing alarm

  // A gentle reminder for long cooking, even when someone is there
  if (state == COOKING && now - lastReminder > COOK_REMINDER) {
    lastReminder = now;
    Serial.print("Reminder: the hob has been on for ");
    Serial.print((now - cookingSince) / 60000);
    Serial.println(" minutes");
    for (int i = 0; i < 2; i++) {
      digitalWrite(BUZZER_PIN, HIGH);
      delay(100);
      digitalWrite(BUZZER_PIN, LOW);
      delay(150);
    }
  }
}

Test it safely

  1. Upload the sketch and open the Serial Monitor at 9600 baud. Wait about a minute: the PIR sensor needs time to settle after power-up.
  2. You don't need a fire to test the flame sensor. Flames give off infrared light, and so does a TV remote. Point the remote at the flame sensor from close by and hold a button down: the sensor's small LED lights, the green LED comes on and the Serial Monitor says "Cooking".
  3. Keep holding the remote button (or ask a helper to), and step out of the PIR sensor's view. After 30 seconds the red LED blinks and the buzzer beeps every 2 seconds.
  4. Wait 20 more seconds: the alarm sounds. Wave at the PIR sensor or press the button, and it goes back to "Cooking".
  5. Release the remote button. Ten seconds later the guard says "Hob off". If the flame sensor reacts too easily or not at all, turn its blue sensitivity knob a little.

How it works

The guard is a state machine: at every moment it is in exactly one of four states, OFF, COOKING, WARNING or ALARM, and clear rules say when it moves to another one. This makes the program easy to follow and easy to change. For each state the bottom part of loop() decides what the lights and the buzzer do.

The two sensors answer two different questions. The flame sensor answers "is the hob on?". Its output changes when it sees infrared light, but a real flame flickers, so the sketch says the hob is on until 10 seconds after the flame was last seen. The PIR sensor and the button answer "is someone here?". Every time one of them reports a person, lastPerson gets the current time. Then now - lastPerson is simply "how long nobody has been seen".

Different flame sensor modules switch their output in different directions. That's why the sketch reads the output once at start-up, when there is no flame, and later treats any change from that level as "flame". Just make sure the hob is off when you plug it in.

A flame sensor only works with gas hobs. For an electric or induction hob, use a temperature sensor such as the DS18B20 near the pan instead; see the ideas below.

Try this next

  • Make it work with an electric hob: replace the flame sensor with the DS18B20 temperature sensor in a metal clip near the hob, and treat "warmer than 45 °C" as "the hob is on".
  • Show the state and the cooking time on the 1602 LCD.
  • Send the alarm to a phone: rebuild it on the ESP32 and use ntfy, as in the gas leak alarm capstone.
  • Count how many times the guard had to warn, and save it in EEPROM.

Troubleshooting

  • It says "Cooking" without a flame: sunlight and warm lamps also give off infrared light. Point the flame sensor away from windows and turn its sensitivity knob down a little. Restart with no flame in view.
  • It never goes to "Cooking": check that DO goes to pin 2, and hold the remote very close to the sensor. Turn the knob until the sensor's LED just lights when you press a remote button.
  • The warning comes even when you are there: the PIR sensor doesn't see you. Point it at where you stand and turn its sensitivity knob up; its time knob can stay at the minimum.
  • The alarm doesn't stop: check the button wiring (pin 4 and the diagonally opposite leg to GND).

Build it yourself

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

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit