Control a Relay Module with an ESP32
Switch a 12 V solenoid with a relay module and an ESP32, with the same code as on an Arduino. The module's coil runs on 5 V, from the ESP32 board's 5V pin, and a GPIO's 3.3 V HIGH switches it, as long as the module's jumper is on H.
This guide uses an ESP32 DevKitC, the common ESP32-WROOM-32 board. For the module itself, its terminals and the diode across the solenoid, see the relay guide.
Try it first, no parts needed
This is the whole project on an ESP32, running in your browser. Press Run simulation: every three seconds the relay turns on for one, its red LED lights, and the solenoid pulls its rod in.
ESP32 serial monitor115200 baud
What the code does
The sketch is the Arduino one with a different pin: it turns the relay on for a second, then off for two, and says which on the Serial Monitor.
// A relay module switches a 12 V solenoid from an ESP32: on for 1 s, off for 2 s.
// GPIO 23 drives the relay's IN (jumper on H); the relay's contacts switch the 12 V supply.
const int RELAY_PIN = 23;
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
digitalWrite(RELAY_PIN, HIGH); // relay on: COM connects to NO
Serial.println("Relay on");
delay(1000);
digitalWrite(RELAY_PIN, LOW); // relay off: COM goes back to NC
Serial.println("Relay off");
delay(2000);
}const int RELAY_PIN = 23;The GPIO wired to the relay's IN. Any GPIO that can be an output works; see which GPIO.
digitalWrite(RELAY_PIN, HIGH); // relay on: COM connects to NOGPIO 23 goes HIGH, 3.3 V. With the jumper on H that is plenty: the relay turns on, and COM joins NO.
digitalWrite(RELAY_PIN, LOW); // relay off: COM goes back to NCGPIO 23 goes LOW, 0 V: the relay turns off and COM goes back to NC. Then a two-second rest before it starts over, as in the relay guide.
Why the jumper goes on H
On the common H/L module, as K6JCA traced it, IN drives the LED of an optocoupler, through a resistor to the jumper. On H the LED's other end goes to DC−, so the input sees the GPIO's own voltage: 3.3 V when HIGH, none when LOW. On L it goes to DC+, 5 V, so the input sees the difference between DC+ and the GPIO.
K6JCA traced the 12 V version of that module and worked out that its relay turns on with about 1.5 V across its input, which he expects of the 5 V version too, its parts otherwise the same. On L, a 3.3 V HIGH still leaves 1.7 V across it, so the relay may never turn off. On H, the GPIO's 0 V and 3.3 V are well either side of that. Modules differ: if yours has no jumper, see the FAQ. The coil itself always runs on DC+, which is why DC+ goes to 5V, not 3V3.
- Across the input
- 3.3 V
- Coil
- Energized
3.3 V across the input from GPIO 23: the coil is energized, COM connects to NO, and the solenoid gets 12 V.
Build it on an ESP32
Now with real parts. Set the jumper, wire it, get the code, then upload it and listen for the click. Not sure which terminal is which? See the relay module pinout.
You'll need
- An ESP32 DevKitC (ESP32-WROOM-32) and a USB cable that carries data
- A 5 V relay module with an H/L jumper, set to H
- A 12 V solenoid, such as Adafruit's product 412, a 12 V DC supply that gives at least 300 mA, and a 1N4001 to 1N4007 diode
- Jumper wires, and a small screwdriver for the terminals
- Software to upload the sketch: either Cirkit Designer, free in your browser, or the Arduino IDE with Espressif's ESP32 boards installed
Wire it
Set the jumper on H first. Then three wires from the module's inputs to the ESP32, and the 12 V circuit through its contacts, as on the Arduino. Wire the 12 V side with the supply unplugged.
Tap a connection to see it on the circuit.
From Connect to Show Relay DC+ ESP32 5V Show Relay DC− ESP32 GND Show Relay IN ESP32 GPIO 23 Show 12 V supply + Relay COM Show Relay NO Solenoid + Show Solenoid − 12 V supply − Show 1N4007 Across the solenoid, band on + Show DC+ goes to 5V, not 3V3. The relay's coil is a 5 V coil. On a DevKitC the 5V pin is fed from the USB socket through a diode, so it has power whenever the board is plugged in.
Get the code
I'm using:Steps 2 and 3 follow the tool you pick.
The project opens with the sketch already in it. It needs no library.
Install the ESP32 boards first, as Espressif's installing guide shows. In Preferences, add
https://espressif.github.io/arduino-esp32/package_esp32_index.jsonto Additional Board Manager URLs; then open Tools → Board → Boards Manager, search for esp32 and install it. The sketch needs no library. Copy it into a new sketch:How it works line by line is in what the code does, above.
Upload and check it
Plug in your ESP32. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your board, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor. Plug in the 12 V supply last.
Show me where to click in Cirkit
Select a device port
Select the device port for uploading code and serial communications
COM4 (ESP32)- Open Build
- Choose Upload firmware
- Click Select Port
- Pick COM4 (ESP32)
- Click Upload to Device
- Open Upload output and serial monitor
- Click Start Serial Monitor
In the Arduino IDE, choose ESP32 Dev Module and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud. Plug in the 12 V supply last.
Show me where to click in the Arduino IDE
esp32_relay_solenoid.ino// A relay module switches a 12 V solenoid from an ESP32: on for 1 s, off for 2 s. // GPIO 23 drives the relay's IN (jumper on H); the relay's contacts switch the 12 V supply. const int RELAY_PIN = 23; void setup() { Serial.begin(115200);OutputSerial Monitor115200 baudCompiling sketch...
Sketch uses 282203 bytes (21%) of program storage space. Maximum is 1310720 bytes.
Global variables use 20800 bytes (6%) of dynamic memory, leaving 306880 bytes for local variables. Maximum is 327680 bytes.
Uploading...
Relay on
Relay off
Relay on
Relay off
Relay on
Done uploading.
- Click the board menu
- Pick ESP32 Dev Module on COM4
- Click Upload
- Wait for Done uploading
- Click Serial Monitor
- Check it is set to 115200 baud
It works when:
- The module's green PWR LED is on while the ESP32 has USB power.
- Every three seconds the relay clicks on for one second, its red LED lights, and the solenoid's rod pulls in; then it clicks off.
- After a few start-up lines from the ESP32's boot ROM, the Serial Monitor prints
Relay on, thenRelay off, in step with the clicks.
Device SerialRelay onRelay offRelay onRelay offRelay onOutputSerial MonitorRelay onRelay offRelay onRelay offRelay onNot working? See troubleshooting.

Which ESP32 pins can drive the relay
Most ESP32 pins can be an output and drive IN; GPIO 23 is one. Change RELAY_PIN to match yours, and avoid the pins in this table: some can't be outputs, and the others are used for flash memory, for choosing how the chip boots (a relay module on one can change how it starts), or for the USB serial connection.
| Avoid | Classic ESP32 | ESP32-S3 |
|---|---|---|
| Input only | GPIO 34 to 39 | None |
| Flash | GPIO 6 to 11 | GPIO 26 to 32, and 33 to 37 on modules with octal flash or octal PSRAM |
| Boot (strapping) | GPIO 0, 2, 5, 12 and 15 | GPIO 0, 3, 45 and 46 |
| Serial (UART0) | GPIO 1 (TX) and 3 (RX) | GPIO 43 (TX) and 44 (RX) |
| Native USB | None | GPIO 19 and 20, the USB port's D− and D+ |
Troubleshooting
Find your symptom; the checks under it run from the most common cause down.
The relay turns on and never turns off
- The jumper is on L. Move it to H; see why.
- Your module has no jumper and turns on with IN LOW. A 3.3 V HIGH may not turn it off. Use a module with an H/L jumper, or one made for 3.3 V.
The relay doesn't click
- The green PWR LED is on. If not, DC+ isn't on 5V or DC− isn't on GND.
- DC+ is on 5V, not 3V3. The 5 V coil may not pull in on 3.3 V.
- IN is on GPIO 23, the pin in
RELAY_PIN.
The relay clicks, but the solenoid doesn't move
- The 12 V side is wired as in the relay guide: the solenoid on NO, the supply's + on COM, the diode's band on the + side.
Nothing in the Serial Monitor, or garbled characters
- The Serial Monitor is set to 115200 baud. It must match
Serial.begin(115200). The ESP32's own start-up lines come first at the same speed.
No port appears for the ESP32
- The cable carries data. The board's USB port carries its serial link as well as power, so a cable that only charges powers the board but shows no port.
- The board's USB-to-serial chip has its driver. Its name, such as CP210x, is printed on the chip near the USB socket. The driver usually installs itself; if no port appears, install it from the chip's maker (Silicon Labs for a CP210x).
The upload stops at Connecting..., then says Failed to connect
Connecting..., then says Failed to connect- The board is in download mode. Hold BOOT, press and release EN (RST on some boards), then release BOOT, and upload again.
FAQ
Can I power the relay module from the ESP32's 3V3 pin?
Not a 5 V module: its coil is rated for 5 V and Songle's datasheet asks for at least 75 % of that, 3.75 V, to be sure it pulls in. Use 5V, or a module whose relay has a 3 V coil.
What if my module has no jumper?
A three-pin module that turns on with IN LOW, like Handson Technology's, works like the jumper's L setting: a 3.3 V HIGH may not turn it off. On its modules with a JD-VCC jumper, Handson says it is sometimes possible to use 3.3 V signals by powering the input side, VCC, from 3.3 V and the coils, JD-VCC, from 5 V, with the jumper off. Otherwise use a module with an H/L jumper.
Does the same code work on an ESP32-S3?
Yes, with the GPIO changed to one the S3 board brings out, such as GPIO 4. In the Arduino IDE, choose ESP32S3 Dev Module.
Simulating the relay on an ESP32 in Cirkit Designer
Cirkit runs your compiled sketch on an emulated ESP32, and its relay module models the input as in the relay guide: it pulls in at 1.8 V across the input and lets go under 1.3 V. On H, GPIO 23's 3.3 V HIGH pulls it in and its 0 V lets it go.
Try L in the part's properties: a LOW puts 5 V across the input and pulls the relay in, and the next HIGH leaves 1.7 V, between the two points, so the relay stays on, as a real module may. Those points are typical values, so a real module on L may behave either way: keep the jumper on H.
References
- K6JCA: schematic of a 1-channel relay module with high/low trigger (the input's thresholds)
- Songle SRD relay datasheet (coil voltage, pull-in at 75 %)
- Handson Technology: 1-channel 5 V relay module user guide (active LOW; 3.3 V signals)
- Espressif ESP32 datasheet (I/O voltage, input-only and strapping pins)
- Espressif ESP32-S3 datasheet (strapping pins)
- Espressif ESP32-DevKitC user guide (its 5V pin, Boot and EN)
- Espressif ESP32-DevKitC V4 schematic (USB to the 5V pin through a diode)
- Arduino-ESP32: installing the ESP32 boards in the Arduino IDE
- Adafruit: small push-pull solenoid, 12 V (product 412)
- ESP-IDF: establishing a serial connection (the USB-to-UART bridge and its driver)
- Espressif esptool: troubleshooting (download mode, the boot log at 115200 baud)

