The ESP32-C6 is a good base for a smart switch — it speaks Wi-Fi 6, and Zigbee/Thread for Matter. This guide wires the relay part: coil on 5V, trigger on GPIO11.
Type 1 or 0 in the Serial Monitor on the UART port. Keep the switched load within the relay’s rating and leave any mains wiring to an electrician.
Wiring
| ESP32-C6-DevKitC-1 | 1-channel relay module (5 V coil) | Wire | Notes |
|---|---|---|---|
| 5V | DC+ | red | Coil supply from USB 5 V. |
| GND | DC− | black | |
| 11 | IN | yellow | 3.3 V-trigger, opto-isolated, active-LOW module. |
5 V coil part, 3.3 V board. The coil runs on 5 V, but the IN pin only drives an optocoupler LED. Pick a module rated for a 3.3 V trigger when using ESP32 or Pico — some 5 V-only modules never fully release from a 3.3 V HIGH.
Board quirks
- GPIO11 is boot-safe. Avoid the C6’s strapping pins (4, 5, 8, 9, 15), the UART pins TX/RX (GPIO16/17, boot messages) and GPIO12/13 (USB).
- The relay pin is set HIGH (off) before it becomes an output, so the relay doesn’t pulse at power-up.
- The on-board RGB LED turns red while the relay is on — handy when the relay is inside a box.
- COM/NO/NC switch the load. Keep to low-voltage loads or a small lamp within the relay’s rating; motors and pumps go through a contactor, and 230 V wiring with MCB, RCCB and earth is for an electrician.
Code
// 1-channel relay module (3.3 V-trigger, active-LOW) on an ESP32-C6-DevKitC-1.
// DC+ → 5V, DC− → GND, IN → GPIO11. Type 1 / 0 in the Serial Monitor (UART port, 115200).
// Low-voltage or small resistive loads only; motors and pumps go through a contactor,
// and any 230 V wiring is for an electrician.
constexpr uint8_t RELAY_PIN = 11;
constexpr bool RELAY_ACTIVE_LOW = true;
void setRelay(bool on) {
digitalWrite(RELAY_PIN, (on != RELAY_ACTIVE_LOW) ? HIGH : LOW);
rgbLedWrite(RGB_BUILTIN, on ? 25 : 0, 0, 0); // on-board LED red while the relay is on
Serial.println(on ? "Relay ON" : "Relay OFF");
}
void setup() {
digitalWrite(RELAY_PIN, RELAY_ACTIVE_LOW ? HIGH : LOW); // off before the pin becomes an output
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Send 1 = on, 0 = off");
}
void loop() {
if (Serial.available()) {
char c = Serial.read();
if (c == '1') setRelay(true);
if (c == '0') setRelay(false);
}
}// 1-channel relay module (3.3 V-trigger, active-LOW) on an ESP32-C6-DevKitC-1.
// DC+ → 5V, DC− → GND, IN → GPIO11. Type 1 / 0 in the Serial Monitor (UART port, 115200).
// Low-voltage or small resistive loads only; motors and pumps go through a contactor,
// and any 230 V wiring is for an electrician.
constexpr uint8_t RELAY_PIN = 11;
constexpr bool RELAY_ACTIVE_LOW = true;
void setRelay(bool on) {
digitalWrite(RELAY_PIN, (on != RELAY_ACTIVE_LOW) ? HIGH : LOW);
rgbLedWrite(RGB_BUILTIN, on ? 25 : 0, 0, 0); // on-board LED red while the relay is on
Serial.println(on ? "Relay ON" : "Relay OFF");
}
void setup() {
digitalWrite(RELAY_PIN, RELAY_ACTIVE_LOW ? HIGH : LOW); // off before the pin becomes an output
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Send 1 = on, 0 = off");
}
void loop() {
if (Serial.available()) {
char c = Serial.read();
if (c == '1') setRelay(true);
if (c == '0') setRelay(false);
}
}Expected serial output
Send 1 = on, 0 = off
Relay ON
Relay OFFSend 1 = on, 0 = off Relay ON Relay OFF
Common errors
Relay clicks, load stays off
Load on NC. Full fix.
Relay never releases
The module needs a 5 V trigger. Use a 3.3 V-trigger module.