OC
OceanRemote
Low-code IoT platform
← Back to Tutorials
← Previous Next →

Tutorial 21: Connecting Relays to Your Board

📖 What You'll Learn in This Tutorial:
  • ✓ What a relay is and how it works (electromechanical switch)
  • ✓ Different types of relay modules (optocoupler vs transistor, 5V vs 3.3V)
  • ✓ Wiring relays to ESP8266, ESP32, and Pico W
  • ✓ Understanding flyback diodes and why they matter
  • ✓ Power supply requirements for multiple relays
  • ✓ Common relay wiring mistakes and how to avoid them

What is a Relay?

A relay is an electromechanical switch that allows a low-power signal (from your ESP8266, ESP32, or Pico W) to control a high-power device (like a light bulb, pump, fan, or appliance).

Simple Explanation:

Your microcontroller (3.3V logic) → Relay Module → High-voltage device (110V/220V AC or 12V/24V DC)

The relay provides electrical isolation - your microcontroller never touches dangerous voltages!
🔧 How a Relay Works Inside:

Inside a relay there is an electromagnet (coil) and a mechanical switch (contacts). When you apply a small voltage to the coil, it creates a magnetic field that pulls the switch closed (or open). When you remove the voltage, a spring returns the switch to its resting position.

This mechanical "click" you hear is the switch moving!

Relay Module Types

OceanRemote works with standard 5V relay modules. Here are the common types:

Type Pros Cons Best For
1-Channel Module Simple, cheap ($2-3) Need one per device, messy wiring for multiple Single device control (one light, one pump)
4-Channel Module Clean wiring, 4 relays on one board) Slightly larger, $5-7 4 devices (lights, fans, etc.)
5-Channel Module Perfect for OceanRemote (matches 5 relays) $7-10 Recommended! All 5 relays on one board
8-Channel Module More relays Larger, more power draw, some pins unused Projects needing 6-8 relays
Solid State Relay (SSR) Silent, faster, longer life More expensive ($10-20), AC only usually Frequent switching, noise-sensitive applications
💡 Recommended Module:

Buy a 5V 5-channel relay module (active LOW, optocoupler isolated). These match OceanRemote's default configuration perfectly and cost about $7-10 on Amazon, eBay, or AliExpress.

Relay Module Pinout

Most 5V relay modules have these connections:

Standard 5V Relay Module (1-channel example - same pins on multi-channel):

    ┌─────────────────────────────────────┐
    │           5V Relay Module           │
    ├─────────────────────────────────────┤
    │                                     │
    │  ┌─────┐  ┌─────┐  ┌─────┐         │
    │  │  DC │  │  IN │  │  NC │──●      │
    │  │  +  │  │     │  │     │         │
    │  └─────┘  └─────┘  └─────┘         │
    │  ┌─────┐  ┌─────┐  ┌─────┐         │
    │  │ DC  │  │ GND │  │ COM │──●      │
    │  │  -  │  │     │  │     │         │
    │  └─────┘  └─────┘  └─────┘         │
    │  ┌─────┐  ┌─────┐  ┌─────┐         │
    │  │ VCC │  │ VCC │  │ NO  │──●      │
    │  │     │  │     │  │     │         │
    │  └─────┘  └─────┘  └─────┘         │
    │                                     │
    └─────────────────────────────────────┘

Pin Descriptions:
- DC+ / VCC: 5V power (connect to board's 5V pin)
- DC- / GND: Ground (connect to board's GND)
- IN (IN1, IN2, etc.): Control signal from GPIO pin
- NC (Normally Closed): Connected to COM when relay is OFF
- COM (Common): The moving contact
- NO (Normally Open): Connected to COM when relay is ON

Understanding NC, COM, and NO

Terminal Meaning Connection When Relay OFF Connection When Relay ON
COM Common Connected to NC Connected to NO
NO Normally Open Disconnected (open circuit) Connected to COM
NC Normally Closed Connected to COM Disconnected (open circuit)
💡 Most Common Wiring:

For most applications, you'll use COM and NO. When the relay turns ON, the circuit closes and your device turns ON. When the relay turns OFF, the circuit opens and your device turns OFF.

Use COM and NC if you want your device ON when the relay is OFF (e.g., fail-safe applications).

Wiring Relays to ESP8266 (D1 Mini / D1 Large)

OceanRemote's default pin mapping for ESP8266 boards:

Relay D1 Mini Pin GPIO Relay Module IN Pin
Relay 1 D1 GPIO5 IN1
Relay 2 D2 GPIO4 IN2
Relay 3 D3 GPIO0 IN3
Relay 4 D4 GPIO2 IN4
Relay 5 D5 GPIO14 IN5
ESP8266 D1 Mini + 5-Channel Relay Wiring:

    ┌─────────────┐                    ┌─────────────────────────┐
    │  ESP8266    │                    │   5-Channel Relay       │
    │  D1 Mini    │                    │   Module                │
    ├─────────────┤                    ├─────────────────────────┤
    │             │                    │                         │
    │   5V        ├────────────────────┤ VCC (DC+)               │
    │   GND       ├────────────────────┤ GND (DC-)               │
    │   D1 (GPIO5)├────────────────────┤ IN1                     │
    │   D2 (GPIO4)├────────────────────┤ IN2                     │
    │   D3 (GPIO0)├────────────────────┤ IN3                     │
    │   D4 (GPIO2)├────────────────────┤ IN4                     │
    │   D5 (GPIO14)├───────────────────┤ IN5                     │
    │             │                    │                         │
    └─────────────┘                    └─────────────────────────┘

Power Notes:
- Connect relay VCC to ESP8266 5V pin (NOT 3.3V!)
- Connect relay GND to ESP8266 GND
- All IN pins connect to D1-D5
⚠️ Critical ESP8266 Boot Pin Notes:

ESP8266 has special boot pins:

  • GPIO0 (D3) - Must be HIGH at boot. If pulled LOW, board enters flash mode.
  • GPIO2 (D4) - Must be HIGH at boot.
  • GPIO15 (D8) - Must be LOW at boot (not used by OceanRemote).

OceanRemote's default pin mapping works correctly because the relay module's optocoupler pulls the pin HIGH (through internal pull-up) when OFF.

Wiring Relays to ESP32

OceanRemote's default pin mapping for ESP32:

Relay ESP32 GPIO Physical Pin (30-pin board) Relay Module IN Pin
Relay 1 GPIO2 D4 / Pin 4 IN1
Relay 2 GPIO4 D4 / Pin 4 (different board) IN2
Relay 3 GPIO5 D5 / Pin 5 IN3
Relay 4 GPIO12 D12 / Pin 12 IN4
Relay 5 GPIO13 D13 / Pin 13 IN5
ESP32 + 5-Channel Relay Wiring:

    ┌─────────────┐                    ┌─────────────────────────┐
    │   ESP32     │                    │   5-Channel Relay       │
    │  Dev Module │                    │   Module                │
    ├─────────────┤                    ├─────────────────────────┤
    │             │                    │                         │
    │   5V        ├────────────────────┤ VCC (DC+)               │
    │   GND       ├────────────────────┤ GND (DC-)               │
    │   GPIO2     ├────────────────────┤ IN1                     │
    │   GPIO4     ├────────────────────┤ IN2                     │
    │   GPIO5     ├────────────────────┤ IN3                     │
    │   GPIO12    ├────────────────────┤ IN4                     │
    │   GPIO13    ├────────────────────┤ IN5                     │
    │             │                    │                         │
    └─────────────┘                    └─────────────────────────┘

Note: ESP32 has no boot pin issues with these GPIOs when used as outputs.
🔧 ESP32 Safe GPIOs for Relays:

If you want to change pin assignments, these GPIOs are safe for relay control:

GPIO4, GPIO13, GPIO14, GPIO18, GPIO19, GPIO21, GPIO22, GPIO23, GPIO25, GPIO26, GPIO27, GPIO32, GPIO33

Avoid GPIO0, GPIO2, GPIO5, GPIO12, GPIO15 at boot (they have boot constraints).

Wiring Relays to Raspberry Pi Pico W

OceanRemote's default pin mapping for Pico W:

Relay Pico W GPIO Physical Pin # Relay Module IN Pin
Relay 1 GPIO16 Pin 21 IN1
Relay 2 GPIO17 Pin 22 IN2
Relay 3 GPIO18 Pin 24 IN3
Relay 4 GPIO19 Pin 25 IN4
Relay 5 GPIO20 Pin 26 IN5
Raspberry Pi Pico W + 5-Channel Relay Wiring:

    ┌─────────────┐                    ┌─────────────────────────┐
    │  Pico W     │                    │   5-Channel Relay       │
    │             │                    │   Module                │
    ├─────────────┤                    ├─────────────────────────┤
    │             │                    │                         │
    │   VBUS (5V) ├────────────────────┤ VCC (DC+)               │
    │   GND       ├────────────────────┤ GND (DC-)               │
    │   GPIO16    ├────────────────────┤ IN1                     │
    │   GPIO17    ├────────────────────┤ IN2                     │
    │   GPIO18    ├────────────────────┤ IN3                     │
    │   GPIO19    ├────────────────────┤ IN4                     │
    │   GPIO20    ├────────────────────┤ IN5                     │
    │             │                    │                         │
    └─────────────┘                    └─────────────────────────┘

Note: Use VBUS (5V from USB) for relay VCC. The Pico W's 3.3V pin cannot power relays!
💡 Pico W Advantage:

The Pico W has no boot pin constraints like ESP8266. All GPIOs (except GP0-1 for Serial and GP23-25 for WiFi) are safe for relay control.

Understanding Flyback Diodes

Relay coils are inductive loads. When you turn off a relay, the collapsing magnetic field generates a high-voltage spike (up to 100V or more) that can damage your microcontroller.

Why Flyback Diodes Matter:

When relay turns OFF:
    Current stops flowing → Magnetic field collapses → High voltage spike → Can destroy GPIO pin!

Solution: Flyback diode (also called snubber diode or freewheeling diode)

    ┌─────────────────────────────────────────┐
    │                                         │
    │    GPIO ──────┬─────────────────────┐   │
    │               │                     │   │
    │              ┌┴┐                    │   │
    │              │ │  Relay Coil        │   │
    │              │ │                    │   │
    │              └┬┘                    │   │
    │               │                     │   │
    │   5V ─────────┼─────────────────────┤   │
    │               │                     │   │
    │              ┌─┴─┐                  │   │
    │              │ ▼ │  Flyback Diode   │   │
    │              │   │  (1N4148 or      │   │
    │              └─┬─┘   1N4001)        │   │
    │               │                     │   │
    │              GND                    │   │
    └─────────────────────────────────────────┘

The diode gives the voltage spike a safe path to circulate back through the coil.
🔧 Good News:

Most pre-built relay modules already include flyback diodes on the PCB! Check your module - if you see a small diode near each relay, you're protected.

If you're building your own relay circuit with bare relays, always add a flyback diode (1N4148 or 1N4001) across the coil terminals.

Optocoupler Isolation (Why It's Important)

Good relay modules include an optocoupler - a component that uses light to send the control signal, providing complete electrical isolation between your microcontroller and the relay circuit.

With Optocoupler (Recommended):

    Microcontroller (3.3V)  →  LED (light)  →  Phototransistor  →  Relay Coil (5V/12V)
                                    ↑
                            No electrical connection!
                            
Benefits:
- Protects microcontroller from relay voltage spikes
- Allows controlling higher voltage relays (12V, 24V)
- Much safer for permanent installations
💡 Buy Optocoupler Modules:

When buying relay modules, look for "optocoupler isolated" or "optically isolated" in the description. These have an extra chip (usually a 4-pin DIP IC) near the input pins. They cost $1-2 more but are worth it for safety and reliability.

Power Supply Requirements

Current Draw Per Component:

Component Typical Current Peak Current
ESP8266 (active WiFi) 80mA 200mA
ESP32 (active WiFi) 150mA 300mA
Pico W (active WiFi) 100mA 200mA
Relay coil (per active relay) 70mA 100mA
DHT22 sensor 1.5mA 2.5mA
DS18B20 sensor 1.5mA 2mA

Power Supply Recommendations:

  • USB power (testing only): Computer USB port (500mA max) - OK for 1-2 relays
  • Phone charger (5V, 1A): Good for 3-4 relays + ESP board
  • Phone charger (5V, 2A): Good for 5+ relays + ESP board
  • External 5V supply (2A+): Best for permanent installations with many relays
Calculating Your Power Budget (Example):

ESP8266 D1 Mini:                   80mA
5 relay coils active: 5 × 70mA = 350mA
DHT22 sensor:                       2mA
-----------------------------------------
Total:                             432mA

Recommendation: 5V, 1A power supply (1000mA) - plenty of headroom!
⚠️ Critical Power Warning:

Do NOT power relays from the microcontroller's 3.3V pin! Relay coils need 5V (most common modules). The 3.3V pin cannot supply enough current and may damage your board's voltage regulator.

Connect relay VCC directly to the 5V pin (from USB or external supply).

Connecting High-Voltage Devices (Safety!)

⚠️ DANGER: HIGH VOLTAGE SAFETY

Working with 110V/220V AC can be DEADLY. Follow these safety rules:

  • Never work on live circuits. Unplug everything before wiring.
  • Use proper gauge wire. 14-16 AWG for 15A circuits, 18 AWG for lower current.
  • Use an enclosure. Never leave exposed high-voltage connections.
  • Add a fuse. Protect your circuit with an appropriately sized fuse.
  • Keep low voltage separate. Don't run 5V and 110V wires in the same conduit.
  • If unsure, hire an electrician. Your life isn't worth saving $50.

Typical AC Wiring Diagram:

Controlling a 110V AC Light Bulb with a Relay:

    Hot (Black)  ───────────────────────┐
                                        │
                                      ┌─┴─┐
                                      │   │  Relay Contacts
                    Live Wire ────────┤   │  (COM to NO)
                                      │   │
                                      └─┬─┘
                                        │
    Light Bulb ─────────────────────────┤
                                        │
    Neutral (White) ────────────────────┘

When relay is ON: COM connects to NO → Circuit completes → Light turns ON
When relay is OFF: COM connects to NC → Circuit breaks → Light turns OFF

Testing Your Relay

Before connecting high-voltage devices, test your relay with a simple multimeter or LED:

Test Circuit (Low Voltage, Safe):

    ┌─────────────┐     ┌─────────────┐     ┌─────────────┐
    │   5V Power  │     │   Relay     │     │   LED +     │
    │   Supply    │     │   COM ──────┼─────┤   330Ω      │
    │             │     │             │     │   Resistor  │
    │             │     │   NO  ──────┼─────┤   to GND    │
    └─────────────┘     └─────────────┘     └─────────────┘

When you toggle the relay in OceanRemote dashboard:
- Relay ON → LED lights up
- Relay OFF → LED turns off

This confirms your wiring and relay logic are correct!

Troubleshooting Relays

Problem: Relay clicks but device doesn't turn on

  • Check your load wiring: Is the device properly connected to COM and NO/NC?
  • Check relay contacts: Use a multimeter in continuity mode to test if COM connects to NO when relay is ON.
  • Check voltage: Is the device getting power? Test with a multimeter.
  • Current rating: Is your device drawing more current than the relay can handle? (Typical relays: 10A max)

Problem: Relay doesn't click at all

  • Check VCC: Is relay VCC connected to 5V (NOT 3.3V)?
  • Check GND: Is relay GND connected to board GND?
  • Check IN pin: Is the GPIO pin correctly connected to relay IN?
  • Test GPIO pin: Upload a simple blink sketch to the GPIO pin to verify it's working.
  • Check logic setting: Is your relay using Positive or Negative logic? Try toggling the setting in OceanRemote dashboard.

Problem: Relay clicks but board resets or behaves strangely

  • Power supply too weak: Add a larger power supply (2A+).
  • Missing flyback diode: Add a diode across the relay coil (if using bare relay).
  • Voltage drop: Check voltage at board when relay activates - it should stay above 4.5V.
  • Add a capacitor: Place a 100-470μF capacitor across 5V and GND near the relay module.

Problem: Relay chatters (rapid clicking)

  • Power supply inadequate: The relay coil is drawing too much current, causing voltage to dip, causing the relay to release, repeating.
  • Fix: Use a separate 5V power supply for the relay module (connect grounds together).
🔧 Quick Test:

To verify your relay module is working, disconnect the IN pin from your microcontroller and connect it directly to GND (for negative logic) or 5V (for positive logic). If the relay clicks, your module is fine - check your code or logic setting.

Relay Best Practices

  • Use optocoupler modules: They protect your microcontroller from voltage spikes.
  • Add a fuse: Protect your high-voltage circuit with an appropriately sized fuse.
  • Use an enclosure: Never leave relay modules exposed, especially with AC wiring.
  • Label your wires: Keep track of which relay controls what device.
  • Start small: Test with one relay first, then add more.
  • Use LED indicators: Many relay modules have built-in LEDs showing relay state - very helpful for debugging.
  • Keep low and high voltage separate: Don't run 5V control wires alongside 110V AC wires.
💡 Recommended Products:
  • 5V 5-Channel Relay Module (Optocoupler): Songle, SainSmart, HiLetgo (Amazon, ~$8-10)
  • Power Supply: 5V 2A USB charger or 5V 2A wall adapter
  • Wire: 22 AWG for signal wires, 14-16 AWG for AC power
  • Enclosure: Plastic junction box (for AC wiring) or project box

Next Steps

Now that you've connected your relays, continue with:

  • Tutorial 22: Understanding Positive vs Negative Relay Logic
  • Tutorial 23: Customizing Relay Names and Logic in Dashboard
  • Tutorial 24: Using Multiple Relays: Best Practices
  • Return to Dashboard: Test your relays!
🎯 Summary:

Relays let your microcontroller control high-power devices safely. Use a 5V optocoupler-isolated relay module (5-channel recommended). Connect relay VCC to 5V (not 3.3V), GND to GND, and IN pins to the GPIOs listed for your board. Add a fuse for safety, use an enclosure, and always test with a multimeter or LED before connecting high-voltage devices.