Relay module works backwards (on at HIGH or off at LOW): active-low fix

Many relay modules are active low: the relay turns on when the input pin is LOW. Change your code to match the module, and set the pin before you enable the output.

What the message means

Opto-isolated relay boards often pull the relay on when the input goes low. So digitalWrite(pin, HIGH) turns it off, and the reverse. Floating pins during boot can also click the relay.

Fix it in this order

  1. Test with a LED or a multimeter on the contacts, not the mains. Find which level turns the relay on.
  2. Invert your logic in code, for example with two constants RELAY_ON and RELAY_OFF.
  3. Write the off level to the pin before calling pinMode(pin, OUTPUT) so the relay does not click at start-up.
  4. Power the module from the supply it needs, usually 5 V, and check its separate JD-VCC jumper if present.
  5. Never touch mains wiring. Use a certified enclosure and have mains work done by someone qualified.
  6. Use a flyback diode across DC coils or motors you switch.

Why it happens

Many relay modules are active-low: the relay switches on when the input pin is pulled low and off when it is high. With the pin floating or set the wrong way at start-up, the relay behaves opposite to what you expect.

Write HIGH to the pin before you set it as an output so the relay does not click at boot, and invert your logic if needed. Check the markings and the trigger jumper on the module.

If you see this, do this

You seeDo this
Relay is on at power-upActive-low input and a floating pin.
It clicks but the load does not runCheck the contact wiring (COM and NO).

safety relay wiring guide, Arduino resets when the motor starts.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

A relay module has a small transistor stage, often with an opto-coupler, between the input pin and the relay coil. Many boards wire this stage so the input pin has to be pulled to ground to turn the relay on; the opto-coupler LED is fed from the module supply through the pin. Such a module is called active low, and it behaves backwards if you expect a high level to turn it on.

The start-up click has a separate cause. During reset and boot the microcontroller pins are inputs or float, and an active-low module can briefly see a low level. The relay then clicks once before your sketch sets the pin. Writing the off level first, before making the pin an output, shortens that click on boards where that works.

How to confirm the cause

  • Look at the module for a jumper or label such as LOW or HIGH trigger. Some boards let you choose.
  • Test with the load disconnected. Tie the input pin to ground with a wire and see if the relay clicks on. If it does, the module is active low.
  • With the power off and the load disconnected, use the meter’s continuity setting across COM and NO while you do that test.
  • Check the module’s supply pin voltage; a relay that clicks weakly may be starved.
  • Read the module’s pins for a separate JD-VCC jumper, which isolates the coil supply from the logic supply.

Worked example

Using named constants so the logic is clear. This sketch switches a low-trigger relay on for two seconds every four seconds:

const int RELAY = 5;
const int ON = LOW, OFF = HIGH;   // active-low module

void setup() {
  digitalWrite(RELAY, OFF);        // set level first (AVR boards)
  pinMode(RELAY, OUTPUT);
}

void loop() {
  digitalWrite(RELAY, ON);  delay(2000);
  digitalWrite(RELAY, OFF); delay(2000);
}

Common mistakes

  • Assuming the same behaviour on every board. On an ESP32, boot-time pin levels differ and some pins are used during boot.
  • Using an ESP32 strapping pin for the relay, which may change the boot mode.
  • Powering a 5 V relay module from 3.3 V.
  • Removing the JD-VCC jumper without providing the separate supply.
  • Wiring mains to the module on a breadboard. Mains work needs a certified enclosure and someone qualified.

If this did not fix it

  • A wrong terminal choice on the relay, such as using NC when you need NO.
  • A relay that is on but the load is not working, which is a load or wiring fault.
  • A board that resets when the relay switches, which is the supply or coil spike problem.