OC
OceanRemote
Low-code IoT platform
← Back to Tutorials
← Previous Next β†’

Tutorial 22: Understanding Positive vs Negative Logic for Relays

πŸ“– What You'll Learn in This Tutorial:
  • βœ“ 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.

πŸ“Š Two Types of 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
πŸ”Œ Which Logic is Most Common?

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)

  1. Connect relay module VCC to 5V and GND to GND
  2. Connect a GPIO pin to the relay's IN pin
  3. Upload a simple sketch that toggles the pin HIGH and LOW every second
  4. Listen for the relay clicking sound
  5. 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);
}
πŸ’‘ Interpreting the Results:
  • Click on HIGH, silent on LOW β†’ Positive Logic (Active HIGH)
  • Silent on HIGH, click on LOW β†’ Negative Logic (Active LOW)

Method 2: Multimeter Test

  1. Set your multimeter to continuity mode (beeper)
  2. Connect the relay module as usual (VCC, GND, IN to GPIO)
  3. Set GPIO to HIGH, check if COM and NO terminals are connected
  4. Set GPIO to LOW, check again
  5. 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
⚠️ Common Mistake:

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

  1. Go to your OceanRemote dashboard
  2. Find your device card
  3. Click "DETAILS" button
  4. In the device detail page, you can edit relay names and logic
  5. Changes take effect immediately (no need to re-flash)
πŸ”§ Logic is Server-Side!

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
πŸ’‘ Pro Tip:

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)
⚠️ Safety Warning:

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
🎯 Quick Reference:
  • 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're Ready!

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!