Generator start and runtime controller

Starts and stops a generator through a relay, counts engine hours, watches the load with a current clamp and warns about carbon monoxide.

Power & batteries Board: ESP32 DevKit V1 (WROOM-32) Level: ●●● ⏱ 4–6 h

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

The part that must never be software only

Starting a generator with a relay is the easy part. The dangerous part is connecting its output to a house that is also connected to the grid. Back-feeding the line can kill the people who repair it. Always use a proper changeover switch with a mechanical interlock; the ESP32 may request the change, but it must never be the only thing that prevents both sources from being connected.

A start routine that looks after the engine

Crank for at most a few seconds, wait, and retry only a limited number of times. After a successful start let the load connect after a warm-up, and before a stop let the engine cool without load. Count the engine hours in flash and show when oil and spark plug are due.

  • A split-core CT such as the SCT-013 with a burden resistor gives the load and lets you see overload.
  • Carbon monoxide kills. Run the generator outdoors, far from windows. The MQ-7 is a hobby hint, not a certified alarm.
  • Keep a manual way to start and stop it, and a fuel level reminder.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • Relay module 1-channel 5V (opto-isolated)
  • SCT-013 current clamp (30-100 A) + burden resistor
  • MQ-7 carbon monoxide (CO) sensor module
  • Active buzzer module 5V
  • Inline fuse holder + fuses
  • Plastic project enclosure

Wiring

Module Module pin ESP32 pin Note
Relay IN signal GPIO26 dry contact only: connect to the generator REMOTE START input, never to the starter or ignition
SCT-013 (voltage-output type) via bias divider analog GPIO35 (ADC1) bias 1.65 V with 2 x 10 kΩ and 10 µF; clamp around ONE live wire only
MQ-7 AO via 10k/20k divider analog GPIO34 (ADC1) AO can reach 5 V
Buzzer + signal GPIO25

Code

#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;
}
#include <WebServer.h>

WebServer server(80);
String buildJson();   // implemented in the project below

String num(float v, int d = 1) { return isnan(v) ? String("null") : String(v, d); }

const char PAGE_HTML[] PROGMEM = R"rawliteral(
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32</title>
<style>body{font-family:system-ui;background:#0b0f14;color:#e6edf3;margin:20px}table{border-collapse:collapse}td{padding:6px 14px;border-bottom:1px solid #223}button,input{font-size:16px;padding:8px 14px;margin:4px;border-radius:6px;border:1px solid #345;background:#16202b;color:#e6edf3}</style>
</head><body><h2>ESP32</h2><table id="t"></table>
<script>
function load(){fetch('/json').then(r=>r.json()).then(d=>{let h='';for(const k in d){h+='<tr><td>'+k+'</td><td>'+d[k]+'</td></tr>';}document.getElementById('t').innerHTML=h;});}
load();setInterval(load,2000);
</script></body></html>
)rawliteral";

void startWeb() {
  server.on("/", []() { server.send_P(200, "text/html", PAGE_HTML); });
  server.on("/json", []() { server.send(200, "application/json", buildJson()); });
  server.begin();
}
const int PIN_START = 26, PIN_CT = 35, PIN_CO = 34, PIN_BUZ = 25;
const float CT_CAL = 30.0;          // A per 1 V RMS (SCT-013-030: 30 A / 1 V). Calibrate with a known load.
const float RUN_AMPS = 0.5;         // above this the generator is considered running
const int   CO_ALARM_DELTA = 600;   // rise over the clean-air baseline that triggers the alarm
bool startOn = false, running = false, coAlarm = false;
uint32_t runStart = 0, totalRunSec = 0;   // runtime since boot; keep long term totals in Home Assistant
float amps = 0; int coBase = 0, coNow = 0;

float readAmps() {
  const int N = 1000; double sum = 0, sq = 0;
  for (int i = 0; i < N; i++) { sum += analogRead(PIN_CT); delayMicroseconds(150); }
  double mean = sum / N;
  for (int i = 0; i < N; i++) { double d = analogRead(PIN_CT) - mean; sq += d * d; delayMicroseconds(150); }
  return sqrt(sq / N) * (3.3 / 4095.0) * CT_CAL;
}
void setStart(bool on) { startOn = on; digitalWrite(PIN_START, on ? HIGH : LOW); }
String buildJson() {
  return String("{"start":") + (startOn ? 1 : 0) + ","running":" + (running ? 1 : 0) + ","amps":" + num(amps, 2) + ","run_hours":" + num((totalRunSec + (running ? (millis() - runStart) / 1000 : 0)) / 3600.0, 2) + ","co":" + coNow + "}";
}
void setup() {
  Serial.begin(115200);
  pinMode(PIN_START, OUTPUT); pinMode(PIN_BUZ, OUTPUT); setStart(false);
  analogReadResolution(12);
  connectWiFi();
  startWeb();
  server.on("/start", []() { setStart(true); server.send(200, "text/plain", "start signal ON"); });
  server.on("/stop", []() { setStart(false); server.send(200, "text/plain", "start signal OFF"); });
  delay(2000); coBase = analogRead(PIN_CO);
}
void loop() {
  server.handleClient();
  static uint32_t t = 0;
  if (millis() - t > 2000) {
    t = millis();
    amps = readAmps(); coNow = analogRead(PIN_CO);
    bool nowRun = amps > RUN_AMPS;
    if (nowRun && !running) { running = true; runStart = millis(); telegramSend("Generator is running"); }
    if (!nowRun && running) { running = false; totalRunSec += (millis() - runStart) / 1000; telegramSend("Generator stopped"); }
    bool co = coNow - coBase > CO_ALARM_DELTA;
    digitalWrite(PIN_BUZ, co ? HIGH : LOW);
    if (co && !coAlarm) telegramSend("WARNING: carbon monoxide detected near the generator");
    coAlarm = co;
  }
}

Build tips

  • Use a generator that has a proper remote-start or auto-start input; do not build your own starter control.
  • Calibrate CT_CAL with a known load such as a kettle and a clamp meter.

What you need

Buy the parts kit · Estimated total: $17.50

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
Relay module 1-channel 5V (opto-isolated) × 1
$1.50
Buy at AliExpress Buy at Amazon.com
SCT-013 current clamp (30-100 A) + burden resistor × 1
$5.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
Inline fuse holder + fuses × 1
$1.50
Buy at AliExpress Buy at Amazon.com
Plastic project enclosure × 1
$3.00
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.

Related ideas

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Inverter load manager

Watches the inverter / battery current and switches off non-critical loads with a relay when the battery runs low.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–5 h
Pick parts / kit
⛔ High risk

Never replace the boiler's own safety thermostat or overheat protection. The ESP32 is only an extra controller; use a certified relay and a fuse.

Solid-fuel boiler controller

Controls the draft fan and circulation pump by flue-gas and water temperature, with overheat and CO warnings.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 4–6 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Mains voltage monitor

Measures 220 V mains through an isolated ZMPT101B module and warns about sags, surges and blackouts in Telegram or Home Assistant.

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

← All module ideas →

Comments

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

Comments are moderated.