Carbon monoxide watcher with buzzer and Telegram (hobby)

An MQ-7 reads relative CO level, a buzzer sounds and Telegram is notified when it rises above the clean-air baseline.

Level: Intermediate Board: ESP32 DevKit V1 (WROOM-32) Safety & alarms

⚠ Safety

A hobby CO sensor is not a certified alarm. Always install a certified CO detector as well and never rely on this build for your safety.

What you need

Buy the parts kit · Estimated total: $10.50

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
MQ-7 carbon monoxide (CO) sensor module × 1
$2.00
Buy at AliExpress Buy at Amazon.com
Active buzzer module 5V × 1
$0.50
Buy at AliExpress Buy at Amazon.com
OLED 0.96" 128x64 I2C SSD1306 × 1
$2.50
Buy at AliExpress Buy at Amazon.com
Resistor kit 1/4W (1k, 2k, 4.7k, 10k, 22k, 100k) × 1
$1.50
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

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.

Wiring

ModuleModule pinESP32 pinNote
MQ-7 VCC / GND5V / GNDwarm-up 3 minutes, burn in 24-48 h when new
MQ-7 AOanalogGPIO34 via 10 kΩ / 20 kΩ dividerAO can reach 5 V
Buzzer +signalGPIO25
OLED SDA / SCLI2CGPIO21 / GPIO22optional display

Code

co-guard.ino
#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);
}

Flash a ready firmware from the browser Paste your error into the AI Error Doctor

Build tips

  • Burn the new sensor in for 24-48 hours and watch the clean-air value before setting ALARM_DELTA.
  • MQ-7 is meant to be heat-cycled; this simple version trades accuracy for simplicity. Install a certified detector too.

← ← All projects

Related ideas

⚠ Caution

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.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 1–2 h
Pick parts / kit

Freezer / fridge alarm

A DS18B20 probe inside the freezer; a buzzer and a message if the temperature rises or the door stays open.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit

Water tank level sensor

A waterproof ultrasonic JSN-SR04T measures the level in a tank or well and reports percent.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 1–2 h
Pick parts / kit

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.