- β What is Positive Logic (active HIGH)
- β What is Negative Logic (active LOW)
- β Why relay modules use different logic types
- β How to identify your relay module's logic
- β Configuring OceanRemote for correct relay logic
- β Troubleshooting relay logic issues
Introduction to Relay Logic
When controlling relays with a microcontroller, the relationship between the GPIO pin state (HIGH or LOW) and the relay's ON/OFF state depends on the relay module's design. This is called relay logic.
| Logic Type | GPIO State | Relay State |
|---|---|---|
| Positive Logic (Active HIGH) | HIGH (3.3V/5V) | ON (relay clicks, circuit closed) |
| LOW (0V) | OFF (relay silent, circuit open) | |
| Negative Logic (Active LOW) | LOW (0V) | ON (relay clicks, circuit closed) |
| HIGH (3.3V/5V) | OFF (relay silent, circuit open) |
Why Do Relay Modules Have Different Logic?
The logic type depends on the relay module's internal circuit:
- Optocoupler Design - Some modules use an optocoupler that triggers on HIGH, others on LOW
- Transistor Type - NPN transistors typically use Positive Logic, PNP transistors use Negative Logic
- Manufacturer Choice - Different manufacturers use different designs
- Historical Reasons - Some modules were designed for specific microcontrollers
Most cheap 5V relay modules from China (Amazon, AliExpress, eBay) use Negative Logic (Active LOW). The relay clicks when you set the GPIO pin to LOW (0V).
Advanced relay modules often use Positive Logic (Active HIGH) for compatibility with PLCs.
How to Identify Your Relay Module's Logic
Method 1: The "Click" Test (Easiest)
- Connect relay module VCC to 5V and GND to GND
- Connect a GPIO pin to the relay's IN pin
- Upload a simple sketch that toggles the pin HIGH and LOW every second
- Listen for the relay clicking sound
- Note which state (HIGH or LOW) makes the relay click
// Relay Logic Test Sketch
// Upload this to identify your relay's logic
#define RELAY_PIN D1 // Change to your GPIO
void setup() {
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(115200);
Serial.println("Relay Logic Test");
Serial.println("Listen for clicks every second");
}
void loop() {
Serial.println("HIGH (3.3V/5V) - Relay should be OFF if Positive Logic, ON if Negative");
digitalWrite(RELAY_PIN, HIGH);
delay(1000);
Serial.println("LOW (0V) - Relay should be ON if Positive Logic, OFF if Negative");
digitalWrite(RELAY_PIN, LOW);
delay(1000);
}
- Click on HIGH, silent on LOW β Positive Logic (Active HIGH)
- Silent on HIGH, click on LOW β Negative Logic (Active LOW)
Method 2: Multimeter Test
- Set your multimeter to continuity mode (beeper)
- Connect the relay module as usual (VCC, GND, IN to GPIO)
- Set GPIO to HIGH, check if COM and NO terminals are connected
- Set GPIO to LOW, check again
- The state that closes the circuit is the relay's ON state
Method 3: Check the Relay Module PCB
- Look for an optocoupler (typically a 4-pin IC like PC817)
- If the optocoupler LED anode connects to VCC β Negative Logic (active LOW)
- If the optocoupler LED anode connects to IN pin β Positive Logic (active HIGH)
- Note: This requires reading the schematic or tracing PCB traces
Many users assume all relay modules work the same way. This is incorrect! Always test your specific module to avoid confusion.
OceanRemote Relay Logic Configuration
OceanRemote allows you to set the logic for each relay individually. Here's how:
Step 1: During Device Creation
When generating firmware from the "Add New Device" page, you can select the logic for each relay:
| Option | Meaning | When to Use |
|---|---|---|
| Positive Logic (πΊ) | HIGH = ON, LOW = OFF | Advanced relay modules, some optocoupler designs |
| Negative Logic (π») | LOW = ON, HIGH = OFF | Most cheap 5V relay modules (Amazon, AliExpress) |
Step 2: Changing Logic After Device Creation
- Go to your OceanRemote dashboard
- Find your device card
- Click "DETAILS" button
- In the device detail page, you can edit relay names and logic
- Changes take effect immediately (no need to re-flash)
Relay logic is stored on the server, not in the firmware. You can change it anytime without re-flashing your device. The server sends the correct state based on your logic setting.
Understanding the Dashboard Display
OceanRemote shows the logic type for each relay in the dashboard:
- πΊ (Triangle up) - Positive Logic (HIGH = ON)
- π» (Triangle down) - Negative Logic (LOW = ON)
Example Dashboard Display: βββββββββββββββββββββββββββββββββββββββ β PIN MAPPING β β βββββββ βββββββ βββββββ βββββββ β β β R1 β β R2 β β R3 β β R4 β β β β πΊ β β π» β β πΊ β β π» β β β βLightβ β Fan β βPump β βHeat β β β βββββββ βββββββ βββββββ βββββββ β β β β R1: Positive Logic (πΊ) β β R2: Negative Logic (π») β βββββββββββββββββββββββββββββββββββββββ
Practical Examples
Example 1: You have a 5V 4-channel relay module from Amazon
- Likely Logic: Negative Logic (Active LOW)
- OceanRemote Setting: Negative Logic (π»)
- Result: Clicking "ON" in dashboard sends LOW to GPIO β relay clicks ON
Example 2: You have an industrial PLC relay module
- Likely Logic: Positive Logic (Active HIGH)
- OceanRemote Setting: Positive Logic (πΊ)
- Result: Clicking "ON" in dashboard sends HIGH to GPIO β relay clicks ON
Example 3: Mixed logic (different relays on same module)
- Some relay modules have different logic for different channels
- OceanRemote allows per-relay logic settings
- Test each relay individually to determine correct logic
If your relay clicks when you expect OFF and stays silent when you expect ON, you have the logic reversed. Simply change the logic setting in the device details page!
Technical Deep Dive
Why Negative Logic is Common in Cheap Modules
Many inexpensive relay modules use an optocoupler (PC817) and an NPN transistor. The optocoupler's LED is connected between VCC and the IN pin. When IN is LOW (0V), current flows through the LED, activating the optocoupler and turning on the relay. When IN is HIGH (3.3V/5V), no current flows (or very little), and the relay stays OFF.
Negative Logic Circuit (Simplified):
VCC (5V)
β
βΌ
βββββ
βLEDβ β Optocoupler LED
βββββ
β
ββββββββΊ IN pin from microcontroller
β
GND
When IN is LOW: Current flows through LED β Relay ON
When IN is HIGH: No current flows β Relay OFF
Positive Logic Modules
These modules typically have an additional transistor that inverts the signal, or they use a different optocoupler configuration where the LED is driven directly by the IN pin.
Positive Logic Circuit (Simplified):
IN pin from microcontroller
β
βΌ
βββββ
βLEDβ β Optocoupler LED
βββββ
β
GND
When IN is HIGH: Current flows through LED β Relay ON
When IN is LOW: No current flows β Relay OFF
Troubleshooting Relay Logic Issues
Problem: Relay clicks when dashboard shows OFF
- Cause: Logic is reversed
- Fix: Change logic setting (Positive β Negative)
Problem: Relay never clicks (always OFF)
- Check VCC and GND connections (relay needs 5V)
- Test with a multimeter to see if GPIO pin is changing state
- Try the opposite logic setting (maybe the relay needs the opposite state to click)
- Relay module may be damaged
Problem: Relay clicks but device doesn't turn on/off
- Check the high-voltage wiring (COM, NO, NC terminals)
- Verify you're using the correct terminals (NO = Normally Open, NC = Normally Closed)
- Check if the connected device is powered
Problem: Relay clicks erratically or buzzes
- Insufficient power supply (relay needs stable 5V)
- GPIO pin not providing enough current (use a transistor driver)
- Electrical interference (use shielded cables or add a capacitor)
Relays can switch high voltage (110V/230V AC). Always disconnect power before wiring. If you're not comfortable with high voltage, use low-voltage DC devices only.
Summary Table
| Relay Type | Logic | GPIO HIGH | GPIO LOW | OceanRemote Setting |
|---|---|---|---|---|
| Cheap 5V module (AliExpress) | Negative | OFF | ON | π» Negative |
| Advanced module | Positive | ON | OFF | πΊ Positive |
| SSR (Solid State Relay) | Positive (usually) | ON | OFF | πΊ Positive |
| Relay Shield for Arduino | Positive (usually) | ON | OFF | πΊ Positive |
- Most cheap modules β Negative Logic (π»)
- If relay clicks when you expect OFF β Reverse logic
- If relay never clicks β Check power and wiring
- When in doubt β Test with the sketch provided above
Next Steps
Now that you understand relay logic, continue with:
- Tutorial 21: Connecting Relays to Your Board (wiring guide)
- Tutorial 23: Customizing Relay Names and Logic
- Tutorial 24: Using Multiple Relays: Best Practices
- Return to Tutorial 02: Generate firmware with correct logic settings!
You now understand the difference between Positive and Negative relay logic. Your OceanRemote dashboard will show the correct logic symbols, and you can change settings anytime without re-flashing!