Smart Safety Systems: MQ-2 Gas Alerts
Learning Objective
Apply everything you have learned about Condition rules and Threshold Windows to build a real-world safety system: a gas leak detector that automatically triggers a multi-stage alarm.
Theory: Designing for Safety
When Code Saves Lives
Safety systems use sensors to watch for danger, like smoke, fire, floods or toxic gas.
A safety system must be reliable. It cannot miss a danger, and it cannot give false alarms every second.
The Gas Sensor (MQ-2)
The MQ-2 is an electronic nose. It reacts to smoke and to gases that burn, like LPG. MakerBuddy shows a relative reading from 0 to 100. Your clean-air reading in your own room is your baseline. Near a suitable test vapor the number should rise, so the rule watches for a value clearly above it.
For Teachers
Discuss Fail-Safe design: if the power fails or a cable is cut, the system should fall back to the safest state, not the easiest one.
Try It: Build a Gas Alarm
Interactive demo · no kit needed
Play with a pretend MakerBuddy board and its web page, right here.
Follow the steps one at a time. Got the kit? You can switch to the real board inside.
Activity: Building the Gas Alarm
Make a gas detector with a threshold window, so it does not alarm all the time.
Step-by-Step Instructions:
- Find the Baseline: With the USB cable unplugged, plug the MQ-2 into the connector marked 33. Power up and wait about 3 minutes while its heater warms up. Note the Gas Level (MQ-2) reading in clean air (0 to 100). Do not touch its metal cover: it gets hot.
- Set the Danger Zone: Pick a threshold well above your baseline, e.g. 60 for a baseline of 20.
- Rule 1 — Alarm: Create a Condition rule named "GasDanger". Sensor → 💨 Gas Level (MQ2), Operator → Greater or equal (>=), Threshold → your danger value. Step 1: Action → 🔊 Buzzer On, Delay (ms) → 100. Step 2: Action → 🔴 RGB Red, Delay (ms) → 100. Save!
- Rule 2 — All Clear: Create a Condition rule named "GasClear". Sensor → 💨 Gas Level (MQ2), Operator → Less than (<), Threshold → your clear value. Step 1: Action → 🔇 Buzzer Off, Delay (ms) → 100. Step 2: Action → 🟢 RGB Green, Delay (ms) → 100. Save!
Leave a gap between the clear and danger values so small changes do not make the alarm flicker. Inside the gap neither rule runs, so the alarm keeps sounding until the air is properly clear. - Test it: In a room with good airflow, your teacher may hold a tissue with a little alcohol-based hand sanitizer 10–15 cm from the sensor. Never use flames, lighters or released fuel gas. Does the alarm sound?
Challenge: The Multi-Stage Alert
Early Warning System
Good safety systems warn you before danger. Build a three-stage alert:
A rule that stops leaves its last step in place, so every quieter stage must switch the buzzer off itself.
Use three separate Condition rules. The Warning rule tests the same MQ-2 twice, so it does not overlap Danger. Adjust all values to your baseline.
Coming Up Next Week:
Week 29: Complex Automation: The Parking Gate (Part 1)
Learning Support
Use this quick guide to help every learner understand, practice and explain Smart Safety Systems: MQ-2 Gas Alerts.
For students
Try it in three steps
- Say it: explain the main idea in one simple sentence.
- Show it: Build a gas alarm: one Condition rule sounds the buzzer and shows red when Gas Level (MQ2) rises well above your clean-air reading, and a second rule silences it and shows green when the air is clear again.
- Stretch it: Build a three-stage early warning: green when safe, yellow in a warning band, and red with the buzzer for danger.
Checkpoint: What was your input, what decision or process happened, and what was the output?
For teachers & parents
Coach without taking over
- Before: ask the learner to predict what will happen and why.
- During: use prompts such as “What changed?” and “What could you test next?” instead of giving the answer.
- After: ask for a 30-second explanation, one piece of evidence and one improvement.
Look for: safe handling, correct vocabulary, a result the learner can demonstrate, and an explanation in their own words.
Did you get it?
Three quick questions. Pick an answer to see whether it is right, and why.
1What should a good safety system avoid doing?
2Why is there a gap between the gas alarm's danger and all-clear values?
3In the three-stage alert, why must each quieter stage switch the buzzer off itself?
