A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.
Gas / smoke alarm (hobby)
MQ-2 sensor with a buzzer and a web page. A hobby add-on, not a certified detector.
An MQ-7 sensor with a buzzer and display for a garage, boiler room or generator shed. A hobby add-on, not a certified CO alarm.
Safety Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 1–2 h
A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.
According to the datasheet the MQ-7 should have its heater switched between a high and a low voltage, in a cycle of a minute or two. Most modules just give it a constant 5 V, and then the reading is less meaningful. If you want to do it right, control the heater with PWM and a transistor and read the sensor at the end of the low phase.
It responds to alcohol, hydrogen and other gases too, so a hand sanitiser can raise an alarm. Calibrate in clean air and let the sensor burn in for a day. CO is slightly lighter than air and mixes with it, so mount at about head height, not on the floor.
| Module | Module pin | ESP32 pin | Note |
|---|---|---|---|
| MQ-7 VCC / GND | 5V / GND | warm-up 3 minutes, burn in 24-48 h when new | |
| MQ-7 AO | analog | GPIO34 via 10 kΩ / 20 kΩ divider | AO can reach 5 V |
| Buzzer + | signal | GPIO25 | |
| OLED SDA / SCL | I2C | GPIO21 / GPIO22 | optional display |
#include <WiFi.h>
const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";
bool connectWiFi(uint32_t timeoutMs = 20000) {
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
uint32_t t0 = millis();
while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
delay(250);
Serial.print(".");
}
Serial.println();
if (WiFi.status() == WL_CONNECTED) {
Serial.print("IP: ");
Serial.println(WiFi.localIP());
return true;
}
return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN"; // from @BotFather
const char* TG_CHAT = "YOUR_CHAT_ID";
// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
if (WiFi.status() != WL_CONNECTED) return false;
WiFiClientSecure client;
client.setInsecure(); // simple; pin the certificate for production
HTTPClient http;
String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
if (!http.begin(client, url)) return false;
http.addHeader("Content-Type", "application/json");
String body = String("{"chat_id":"") + TG_CHAT + "","text":"" + text + ""}";
int code = http.POST(body);
http.end();
return code == 200;
}
const int PIN_CO = 34, PIN_BUZ = 25;
const int ALARM_DELTA = 500; // set after watching clean-air values for a day
int baseline = 0; bool alarm = false; uint32_t lastMsg = 0;
int readCo() { long s = 0; for (int i = 0; i < 16; i++) { s += analogRead(PIN_CO); delay(5); } return s / 16; }
void setup() {
Serial.begin(115200); pinMode(PIN_BUZ, OUTPUT); analogReadResolution(12);
connectWiFi();
delay(180000); // warm-up
baseline = readCo();
}
void loop() {
int v = readCo(); bool a = v - baseline > ALARM_DELTA;
digitalWrite(PIN_BUZ, a ? HIGH : LOW);
if (a && (!alarm || millis() - lastMsg > 600000)) { telegramSend("WARNING: possible carbon monoxide. Ventilate and leave the room"); lastMsg = millis(); }
alarm = a;
if (!a && v < baseline) baseline = (baseline * 99 + v) / 100; // slow drift tracking
Serial.println(v);
delay(2000);
}
Buy the parts kit · Estimated total: $10.50
Other countries: order these parts on AliExpress with the links below.
Some links are affiliate links: we may earn a commission at no extra cost to you.
A hobby gas sensor is not a certified alarm and does not replace a real gas or smoke detector.
MQ-2 sensor with a buzzer and a web page. A hobby add-on, not a certified detector.
A DS18B20 probe inside the freezer; a buzzer and a message if the temperature rises or the door stays open.
A waterproof ultrasonic JSN-SR04T measures the level in a tank or well and reports percent.
Comments
No comments yet. Share your experience or ask a question.