Posted on — Leave a comment

Control a 5V relay module safely with ESP32 (low voltage only)

By EEDIY Team. Difficulty ★★ · Written with electricians and beginners in mind Low voltage only. This tutorial switches a small DC load such as an LED strip on its own adapter. Do not connect mains (120/240 V) loads.

What you need

  • ESP32 board (set up with the LED blink post)
  • 1 relay module rated for a 5 V supply. A module is a relay plus a driver circuit on one small board
  • Jumper wires
  • A low-voltage DC load (for example a 5 V or 12 V LED strip) with its own power adapter
  • Multimeter (optional, to check connections)
Words to know
  • Relay: A relay is an electrically operated switch: current through a coil pulls the contacts (e.g. Songle SRD-05VDC: 5 V coil, about 71 mA). That’s more than an ESP32 pin can supply (about 40 mA on the C6), and the coil kicks back a voltage spike when switched off, so it needs a transistor driver and a flyback diode. A relay module includes both. (The C6 datasheet lists a typical 40 mA source current at the strongest drive setting. The default drive strength is 20 mA on most pins.)
  • COM / NO / NC: the relay’s switch terminals: common, normally open (connected only when the relay is on), and normally closed.
  • Flyback diode: a diode that absorbs the coil’s turn-off spike. TE’s relay coil suppression note says a 12 VDC relay may generate 1,000 to 1,500 V at turn-off without suppression. Example driver chip: TI’s ULN2003A has these diodes built in.

Why use a module, not a bare relay?

A relay coil needs a driver circuit, and dedicated relay-driver chips such as the ULN2003A include clamp diodes for inductive loads. A module puts the driver next to the relay. Check your module’s documentation for what it includes. The ESP32 works at 3.3 V (the ESP32-C6 datasheet recommends a 3.0-3.6 V supply), so check that your module’s input accepts a 3.3 V control signal. If your module’s documentation asks for a 5 V input signal, choose a different module or ask on the forum. Caution: some active-low optocoupler modules have their input pulled up to 5 V. A 3.3 V “high” may not fully switch them off. Prefer modules rated for a 3.3 V input. Tip for active-low modules: call digitalWrite(RELAY_PIN, HIGH); before pinMode(RELAY_PIN, OUTPUT); in setup() to help avoid a click at boot.

Step 1: Read your module’s label and documentation

Find the VCC, GND and IN pins, and whether the input is “active high” or “active low”.

Step 2: Wire the control side

  1. Module VCC to the board’s 5V pin (or the supply named in the module’s documentation).
  2. Module GND to ESP32 GND.
  3. Module IN to a GPIO. The example uses GPIO 18, which is not a strapping pin on the classic ESP32 or the ESP32-C6. Avoid strapping pins: a relay module connected to one can affect how the board boots.

Step 3: Wire the load side (low voltage only)

  1. Unplug the load’s adapter.
  2. Run one wire of the load through COM and NO, so the relay breaks that wire.
  3. Plug the adapter back in.

Step 4: Upload

const int RELAY_PIN = 18;  // not a strapping pin on classic ESP32 or C6
void setup() { pinMode(RELAY_PIN, OUTPUT); }
void loop() {
  digitalWrite(RELAY_PIN, HIGH); delay(2000);   // relay on (or off, if your module is active-low)
  digitalWrite(RELAY_PIN, LOW);  delay(2000);
}

Expected result

Every 2 seconds you hear a click, the module’s LED changes, and the load turns on or off.

Common mistakes

Problem Fix
Module LED lights but no click Check the module’s documentation for the VCC and input voltages it needs
Load is on when it should be off (works backwards) Load is wired to NC; move it to NO, or the module is active-low
Load never turns on Adapter not plugged in, or the load wire isn’t actually through COM/NO. Check continuity between COM and NO when the relay clicks.
Board resets when the relay clicks Tip: this can be a power problem. Power the module from a separate 5 V supply with a common ground (GND connected to the ESP32’s GND), or add a capacitor across the module’s supply (around 100 µF is a typical value)

About mains wiring (read this)

In Canada, electrical installation work is regulated provincially, and the rules differ. In Quebec, electrical installation work, even in your own home, must be done by a licensed electrical contractor (RBQ/CMEQ). In Ontario, a homeowner may do some work in their own home but must file a notification with ESA and have it inspected. Check your province’s rules before wiring anything to mains; when in doubt, hire a licensed electrician. Don’t treat a hobby relay module as a safety device. Check its documentation for its ratings, and never rely on code for an emergency stop.

Sources

Keep going

Leave a Reply