Relay module is Turning ON for a second while switch off the device

Hello, I am facing a weird problem that when I power off the following device, the relay module is turned ON for a second and turned off. I tried with different module but facing the same issue. Here is the conncetion diagram. BTW the program is working as expected but the only issue while switch off the device.

I am not sure what could be the issue.

@LarryD can you help me to resolve this issue?

Relay ground needs to be connected to Arduino ground

Post your code.

Is it a 12V relay module?

  • Actually no, the Arduino pulls the relay module input to GND to energize a relay.

  • In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.
    Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

  • You can attach a scaled down version that still exhibits the problem.

Right

Here is my sketch.


// Define the relay pin and input pins
const int relayPin = 7;
const int buttonOnPin = 3;
const int buttonOffPin = 4;

void setup() {
  // Set the relay pin as an OUTPUT
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW); // Ensure relay is OFF initially

  // Set the button pins as INPUT with internal pull-up resistors
  pinMode(buttonOnPin, INPUT_PULLUP);
  pinMode(buttonOffPin, INPUT_PULLUP);
}

void loop() {
  // Check if buttonOnPin is connected to GND
  if (digitalRead(buttonOnPin) == LOW) {
    digitalWrite(relayPin, HIGH); // Turn the relay ON
  }
  
  // Check if buttonOffPin is connected to GND
  if (digitalRead(buttonOffPin) == LOW) {
    digitalWrite(relayPin, LOW); // Turn the relay OFF
  }
}

Yes this is 12v relay module. If I connect both arduino GND and exyernal power supply GND then relay is directly ON though the trigger pin is not activated by the microcontroller.

  • Don’t connect the GNDs

  • 1st step

digitalWrite(relayPin, LOW); // Ensure relay is OFF initially

. . .

digitalWrite(relayPin, HIGH);


  • A LOW energizes the relay, a HIGH de-energizes the relay

My primary concern is while power off this entire device the relay is enerzised for a second and then finally switching OFF the entire device as there is no power supply.

  • Show us your actual wiring

  • Try this change to the sketch below, more than likely won’t make any difference :woozy_face:
    Note you may need to swap the relay N.C. and the N.O. contacts.

#define RELAYon    LOW
#define RELAYoff   HIGH

// Define the relay pin and input pins
const int relayPin = 7;
const int buttonOnPin = 3;
const int buttonOffPin = 4;

void setup() {

  digitalWrite(relayPin, RELAYoff); // Ensure relay is OFF initially

  // Set the relay pin as an OUTPUT
  pinMode(relayPin, OUTPUT);


  // Set the button pins as INPUT with internal pull-up resistors
  pinMode(buttonOnPin, INPUT_PULLUP);
  pinMode(buttonOffPin, INPUT_PULLUP);
}

void loop() {
  // Check if buttonOnPin is connected to GND
  if (digitalRead(buttonOnPin) == LOW) {
    digitalWrite(relayPin, RELAYon); // Turn the relay ON
  }
  
  // Check if buttonOffPin is connected to GND
  if (digitalRead(buttonOffPin) == LOW) {
    digitalWrite(relayPin, RELAYoff); // Turn the relay OFF
  }
}

  • You really must only react to the switches when they change state, not to the current switch level.

The wiring diagram image is already attached with my post. That is my actual wiring connection diagram.

I am not using NC of the realy because it is not required at all for my requirement.

I am using only COM and NO of the realy to control the external devices.

My requirement is simple. If I connect pin3 to arduino GND pin then need to be enerzised the relay and if I connect pin4 with GND the relay should deenerzised.

But the problem is not with overall functionality but while turning off the device the relay is triggering for a second. I am not sure whether the issue with wiring or sketch.

  • We want to see the actual wiring

  • The sketch below looks at when the switch changes state.

#define RELAYon          LOW
#define RELAYoff         HIGH

#define PRESSED          LOW
#define RELEASED         HIGH

// Define the relay pin and input pins
const int relayPin     = 7;

const int buttonOnPin  = 3;
const int buttonOffPin = 4;

byte lastOnPin         = RELEASED;
byte lastOffPin        = RELEASED;


//================================================^================================================
void setup()
{
  digitalWrite(relayPin, RELAYoff); // Ensure relay is OFF initially

  // Set the relay pin as an OUTPUT
  pinMode(relayPin, OUTPUT);


  // Set the button pins as INPUT with internal pull-up resistors
  pinMode(buttonOnPin, INPUT_PULLUP);
  pinMode(buttonOffPin, INPUT_PULLUP);

} //END of   setup()


//================================================^================================================
void loop()
{
  byte state;
  
  //=======================================
  state = digitalRead(buttonOnPin);

  if (lastOnPin != state)
  {
    //update to the new state
    lastOnPin = state;

    if (state == PRESSED)
    {
      digitalWrite(relayPin, RELAYon); // Turn the relay ON
    }
  }

  //=======================================
  state = digitalRead(buttonOffPin);
  
  if (lastOffPin != state)
  {
    //update to the new state
    lastOffPin = state;

    if (state == PRESSED)
    {
      digitalWrite(relayPin, RELAYoff); 
    }
  }

  //=======================================
  //poor man's debounce
  delay(50);
  
} //END of   loop()


//================================================^================================================

Actual wiring diagram or phto of my device wiring??

Actually I am not using any buttons but I am trying to connect direct wire from MC pin to MC GND pin.

  • A photo of the actual wiring.
  • That’s fine, as long as a LOW means relay ON for Arduino pin 3 and
    as long as a LOW means relay OFF for Arduino pin 4



  • Assume Micro Controller

  • Describe the exact sequence of events that cause your problem.

  • Try the sketch in post #15

I connceted 12v VCC to the relay module jd-vcc (removed jumper) and 12v GND to relay module GND.

Connected pin 2 of MC to IN of relay module.

This is the overall wiring connection overview.

Now the issue is whenever I switched off the entire powersupply then the relay module is enerzised for a while and switching off.

// Define the relay pin and input pins
const int relayPin = 7;
const int buttonOnPin = 3;
const int buttonOffPin = 4;

Your code says relayPin is 7 not 2

  • Try Sketch in #15, change the sketch’s relayPin from 7 to agree with what you are using (2 ?)