How to wire a relay module to an ESP32
To wire a relay module to an ESP32, connect VCC to VIN (5 V from USB), GND to GND and IN to GPIO 4 (D4). The coil runs from the 5 V pin while IN comes from a 3.3 V pin; most single-channel modules switch fine from 3.3 V, but some need closer to 5 V. The sketch below uses no library beyond what the board includes. It switches the relay on for a second and off for a second, printing a line each time.
- 3.3 V logic
- Library: none
- 3 wires
Relay module to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| Relay module pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VCC | VIN (5 V from USB) | red | The coil is a 5 V coil; it takes about 70 mA, more than any pin can give. |
| GND | GND | blue | Common ground. |
| IN | GPIO 4 (D4) | amber | A logic signal only: the module’s transistor switches the coil. |
| COM, NO, NC | The load | n/a | The switched side: COM is the arm, NO is open until the relay pulls in, NC is closed until then. |
Power and logic levels for relay module and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The relay module takes 5 V from VIN (5 V from USB). That is the ESP32’s 5 V rail, so the part shares the current the board can supply.
This pair
The coil runs from VIN (5 V from USB) and IN comes from a 3.3 V pin. Most single-channel modules switch fine from 3.3 V; some optocoupler modules with a high-side jumper need closer to 5 V, and then a small transistor or a 3.3 V relay module solves it. The simulated relay is set to trigger on HIGH.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: none, the sketch drives IN with digitalWrite(). This is the sketch the circuit above runs.
const int RELAY_PIN = 4; // IN, GPIO 4 (D4)
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // released at start
}
void loop() {
digitalWrite(RELAY_PIN, HIGH); // pull in: COM to NO
Serial.println("Relay on");
delay(1000);
digitalWrite(RELAY_PIN, LOW); // release: COM back to NC
Serial.println("Relay off");
delay(1000);
}
What you should see
The relay clicks closed and open. The serial monitor, at 115200 baud, prints Relay on when the coil is energized and Relay off when it is released.
The switched side, COM, NO and NC, is isolated from the board: wire the load there, never to the board’s pins.
The serial monitor prints this, recorded from the circuit above running for two and a half seconds:
Relay on
Relay off
Relay onCommon mistakes wiring a relay module to an ESP32
Active low modules
Many cheap relay boards switch when IN goes LOW, so the sketch seems inverted and the relay is on at boot. Check the board, and in the editor set the relay’s trigger property to low to match.
Driving a coil straight from a pin
A bare relay coil wants 70 mA or more and kicks back a voltage spike when it releases. On a module the transistor and the flyback diode are already there; a bare relay needs both, and never a direct pin.
Switching mains without care
The contacts are rated for mains, but the board and the screw terminals are not a safe enclosure. Learn on a 12 V lamp or a motor, and leave mains to a properly rated, enclosed module.
Build your own relay module project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.