Hardware Wiring Safety
Most dead ESP32 boards and most electrical accidents come from the same few mistakes. This guide covers them: relay modules, inductive loads, logic levels, strapping pins and mains safety. Pin numbers for your board are in the pinout reference. To practise logic first without risk, use the Wokwi lab.
1. Opto-isolated relay modules
Section titled “1. Opto-isolated relay modules”A typical 1/2/4/8-channel relay board has three parts: input pins (IN1..), an optocoupler per channel, and a relay coil driven by a transistor. Many boards use 5 V coils.
Header pins you will usually see: VCC, GND, IN1..INn on the control side, and often a three-pin jumper block with JD-VCC, VCC and GND.
JD-VCC jumper and a separate relay supply
Section titled “JD-VCC jumper and a separate relay supply”With the jumper fitted, the relay coil and the optocoupler LED share the same 5 V rail, so there is no real isolation between the MCU and the relay side. To get true isolation:
- Remove the jumper between
JD-VCCandVCC. - Power
VCC(optocoupler side) from the ESP32 logic supply. - Power
JD-VCCand the relay-sideGNDfrom a separate 5 V supply sized for all coils. - Do not connect the two grounds together if you want full isolation. The optocoupler needs no common ground to work.
ESP32 side (isolated) Relay side +-----------------+ +--------------------+ | 3V3/5V --- VCC | (jumper | JD-VCC <-- 5V PSU2 | | GPIO ---- IN1 | removed) | GND(relay) <-- PSU2| | GND ----- GND | | coil + transistor | +-----------------+ +--------------------+Common ground notes
Section titled “Common ground notes”- If you share power (jumper fitted), you share ground. That is fine for a bench demo with a low-voltage load, but it removes isolation.
- Whatever supplies the relay coil should be able to deliver the coil current without sagging; a dip during switching resets the ESP32 when the rails are shared. Add bulk capacitance (for example 100 to 470 uF) near the relay supply.
- Isolation is on the control side only. The load side of the relay (COM, NO, NC) is a separate circuit and must never touch logic pins.
Active-low behaviour
Section titled “Active-low behaviour”Many common modules are active-low: the relay energises when the input is pulled LOW. That means:
- At boot, floating or HIGH-Z pins can cause the relay to click or turn on briefly. Set the pin HIGH (off) before setting it as an output, so it does not pulse.
- Test with the load disconnected and confirm which level turns it on.
const int RELAY_PIN = 26; // choose a safe GPIO for your boardconst bool ACTIVE_LOW = true;
void relayWrite(bool on) { digitalWrite(RELAY_PIN, (on ^ ACTIVE_LOW) ? HIGH : LOW);}
void setup() { digitalWrite(RELAY_PIN, ACTIVE_LOW ? HIGH : LOW); // off first pinMode(RELAY_PIN, OUTPUT);}2. Flyback diodes for inductive loads
Section titled “2. Flyback diodes for inductive loads”Motors, solenoids, contactor coils and relay coils store energy. When current is interrupted they produce a voltage spike that can damage transistors, relay contacts and the MCU.
For a DC inductive load switched by a transistor or relay contact:
- Place a diode (for example 1N4007 for small loads, or a Schottky for faster response) across the load, as close to it as possible.
- Polarity: the cathode (band) goes to the positive supply side and the anode to the switched (negative) side, so the diode is reverse-biased in normal operation and conducts only during the spike.
+V ----+-----------+ | | [ LOAD ] cathode (band) coil/motor | | [ DIODE ] | | +-----------+ anode | switch / transistor | GNDNotes:
- The relay’s own coil on a module normally already has a flyback diode. You still need one across the load if it is a DC motor, solenoid or another coil switched through the relay contacts.
- For AC inductive loads, a diode does not work. Use a snubber (RC) or MOV rated for the load, chosen by a qualified person.
- Reversed diodes cause a short circuit when you power up. Double-check the band before applying power.
- Do not drive a motor or solenoid directly from a GPIO. Use a transistor or MOSFET (logic-level) or a driver module.
3. 3.3 V vs 5 V logic
Section titled “3. 3.3 V vs 5 V logic”ESP32 GPIOs run at 3.3 V and are not 5 V tolerant. Applying 5 V to an input can damage the pin or the chip.
| Situation | Safe approach |
|---|---|
| 5 V sensor output into ESP32 input | Bidirectional level shifter, or a resistor divider for slow one-way signals (for example 2 kohm over 3.3 kohm roughly gives 3 V from 5 V) |
| ESP32 output to 5 V input (for example some LED strips) | Level shifter (74AHCT125 or similar), or check if the device accepts 3.3 V as HIGH |
| I2C with mixed voltages | Bidirectional I2C level shifter module; do not rely on 5 V pull-ups |
| Relay module with 5 V supply and 3.3 V input | Often works because the optocoupler LED is driven through a resistor; verify the module and current draw |
| USB / 5 V pin on a DevKit | Output of the regulator input, not a GPIO; never feed it into a GPIO |
Rules of thumb:
- Keep GPIO current low (a few mA is typical; check the datasheet for limits) and use a series resistor with LEDs.
- Several ESP32 GPIOs (for example 34 to 39 on the classic ESP32) are input-only and have no internal pull-up/pull-down.
- Share a common ground between devices that exchange logic signals directly.
4. Strapping pins
Section titled “4. Strapping pins”At reset the chip samples certain pins to decide boot mode and some internal settings. If your circuit forces them to the wrong level during reset, the board will not boot or will not flash. Common cases (confirm for your exact module in the Espressif datasheet; see docs.espressif.com):
| Chip | Strapping pins to be careful with | Practical caution |
|---|---|---|
| ESP32 (classic) | GPIO0, GPIO2, GPIO12 (MTDI), plus GPIO5 and GPIO15 | GPIO0 low at reset enters download mode; GPIO12 affects flash voltage selection, so do not hold it high at boot on boards that need it low; avoid attaching relays or strong pull-ups/downs to these |
| ESP32-S3 | GPIO0, GPIO3, GPIO45, GPIO46 | GPIO0 selects boot mode; GPIO45 and GPIO46 also affect boot configuration; GPIO3 relates to JTAG source selection |
Practical advice:
- Prefer ordinary GPIOs for relays and buttons. Pins like GPIO16/17 on a classic ESP32 DevKit (check yours; WROVER modules use them for PSRAM), or GPIO 25, 26, 27, 32, 33 are commonly used; confirm against your datasheet.
- Avoid GPIOs connected to the on-board flash (the classic ESP32 GPIO6 to GPIO11) completely.
- On boards with an on-board LED or auto-reset circuitry, behaviour on GPIO0/GPIO2 can vary by board.
- If a board will not flash after you attached something, disconnect the extra wiring from the strapping pins first.
Pre-power checklist
Section titled “Pre-power checklist”- All wiring re-checked with power off; polarity of diodes, electrolytic capacitors and supplies verified.
- Relay module tested without a load first; active-low behaviour understood.
- Separate relay supply and JD-VCC jumper decision made.
- No 5 V signal connected to an ESP32 GPIO.
- Strapping pins not used for loads.
- Load fuse or current limit in place; supply rated for the load.
- Mains: not on a breadboard, enclosed, fused, earthed, handled by a qualified person.
Next: practise the logic in the Wokwi lab, then see parts in the India BOM guide. Return to IoT start.