Relay guides · On this page

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.

Ready
Open in Cirkit Designer

ESP32 serial monitor115200 baud

Output appears here when the simulation runs.

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.

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);
  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);
}
  1. const int RELAY_PIN = 23;

    The GPIO wired to the relay's IN. Any GPIO that can be an output works; see which GPIO.

  2. digitalWrite(RELAY_PIN, HIGH);  // relay on: COM connects to NO

    GPIO 23 goes HIGH, 3.3 V. With the jumper on H that is plenty: the relay turns on, and COM joins NO.

  3. digitalWrite(RELAY_PIN, LOW);   // relay off: COM goes back to NC

    GPIO 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.

The H/L jumper with a 3.3 V board; the relay turns on with about 1.5 V across its input. Jumper on H, use this: GPIO HIGH, 3.3 V, puts 3.3 V across it, on; GPIO LOW puts 0 V, off. Jumper on L: GPIO LOW puts 5 V across it, on; GPIO HIGH, 3.3 V, still leaves 1.7 V across it, so the relay may stay on.

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.

The relay, liveExample: GPIO 23 HIGH, 3.3 VRun the circuit at the top, and this follows the relay as the sketch switches it.
Across the input
3.3 V
Coil
Energized
COMNCNO

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
  1. 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.

    FromConnect toShow
    Relay DC+ESP32 5VShow
    Relay DC−ESP32 GNDShow
    Relay INESP32 GPIO 23Show
    12 V supply +Relay COMShow
    Relay NOSolenoid +Show
    Solenoid −12 V supply −Show
    1N4007Across 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.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    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.json to 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.

  3. 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
    Relay module on an ESP32FileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    ESP32Edit Code

    Board

    ESP32 Change

    2

    Port

    Select PortCOM4 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    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.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM4 at 115200 baud
    Relay on
    Relay off
    Relay on
    Relay off
    Relay on
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM4 (ESP32)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM4 (ESP32)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. 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
    Select BoardESP32 Dev Module

    Select other board and port

    ESP32 Dev ModuleCOM4
    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 baud

    Compiling 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.

    1. Click the board menu
    2. Pick ESP32 Dev Module on COM4
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. 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, then Relay off, in step with the clicks.

    Not working? See troubleshooting.

    Next guideNext: water a plant automaticallyA soil moisture sensor and the same relay on an Arduino Uno: a 12 V water valve opens while the soil is dry.

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.

AvoidClassic ESP32ESP32-S3
Input onlyGPIO 34 to 39None
FlashGPIO 6 to 11GPIO 26 to 32, and 33 to 37 on modules with octal flash or octal PSRAM
Boot (strapping)GPIO 0, 2, 5, 12 and 15GPIO 0, 3, 45 and 46
Serial (UART0)GPIO 1 (TX) and 3 (RX)GPIO 43 (TX) and 44 (RX)
Native USBNoneGPIO 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

  • 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