Here’s a ready-to-adapt ESP-IDF template for an 8-gang smart switch on an ESP32-C6. It debounces eight rocker-switch inputs (through your isolation/sense stage) and drives eight relay outputs. Each channel supports:
Manual toggle on input edge
Optional 1-second on/off pattern for endurance testing
Safe default state on boot
Notes:
Replace PIN placeholders with your real GPIOs. Avoid bootstrapped pins on ESP32-family devices when possible.
If your input is active-low (typical opto to GND), set active_low = true for that channel.
init_gpio();
// Timers
scan_timer = xTimerCreate("scan", pdMS_TO_TICKS(DEBOUNCE_INTERVAL_MS), pdTRUE, NULL, scan_timer_cb);
pattern_timer = xTimerCreate("pattern", pdMS_TO_TICKS(100), pdTRUE, NULL, pattern_timer_cb);
xTimerStart(scan_timer, 0);
xTimerStart(pattern_timer, 0);
// Example: enable pattern on channel 0 and 1
// enable_pattern(0, true);
// enable_pattern(1, true);
// Main task can handle commands, comms, etc.
while (true) {
vTaskDelay(pdMS_TO_TICKS(1000));
// Heartbeat / stats
// ESP_LOGI(TAG, "Alive");
}
}
How to adapt
Assign pins: replace relay_pins[] and input_pins[] with your real GPIOs. On ESP32-family, avoid bootstrapped pins for GPIO assignments when possible.
Polarity: set active_low_inputs[] and invert_relay[] to match your hardware.
Pattern: enable per channel with enable_pattern(i, true). The period is exactly 1000 ms.
Debounce: DEBOUNCE_STABLE_COUNT x DEBOUNCE_INTERVAL_MS gives exactly 25 ms now; adjust as needed.
Safety reminders
Your hardware already isolates 230 Vac; keep that isolation boundary in firmware assumptions too: never bind mains and logic grounds.
Default all relays OFF at boot and on faults.
For I2C displays later, remember: use 4.7k pull-ups and keep total bus capacitance at or below 400 pF.
If you prefer Arduino instead of ESP-IDF, say “Arduino version” and I’ll provide it. If you share your exact pin map and active levels, I’ll tailor this code to your design.
Wirelessly connects nets on schematic. Used to organize schematics and separate functional blocks. To wirelessly connect net portals, give them same designator. #portal
Wirelessly connects power nets on schematic. Identical to the net portal, but with a power symbol. Used to organize schematics and separate functional blocks. To wirelessly connect power net portals, give them the same designator. #portal #power
A generic fixed inductor for rapid developing circuit topology. *You can now change the footprint and 3D model at the top level anytime you want. This is the power of #generics