Arduino relay (whitch is load) stays in NO after power off arduino

Hello All,

Im try to switch the 230V power of my laptop charger. I made a windows program with sends data to the arduino. The arduino can read this and will switch the relay ON and OFF. Sometimes it works correctly but sometimes the relay gives an error.

Sometimes when the battery is charging (wires are connected to NO and COM) and I will switch it OFF i didn't hear a 'loud' click but only a dull sound. the relay didnt switch good and the battery is still charging.

other case:
sometimes when i pull of the USB (witch charges the arduino) the relay also stays in NC (also when the battery is charging).

code:

#include <SoftwareSerial.h>
using namespace std;

#define BAUD 9600
#define relay 12

bool liveCheck  = false;
bool relayState = LOW;
int batterijpercentage_int;

void LoadBattery(){
  if(digitalRead(relay)==LOW){}
  else{
    digitalWrite(relay,LOW);
    delay(3000);
  }
}

void notLoadBattery(){
  if(digitalRead(relay)==HIGH){}
  else{
    digitalWrite(relay,HIGH);
    delay(3000);
  }
}

void setup() 
{
  Serial.begin(BAUD);
  pinMode(relay, OUTPUT);
  delay(3000);
  digitalWrite(relay,relayState);
  delay(3000);
}

void loop() 
{
//here is some code for a handshake and the serial.read function. 
//windows app will send a 0,1  or 2.  

    if(batterijpercentage_int==0){
        notLoadBattery();
      }
    if(batterijpercentage_int==1){
        LoadBattery();
      }
    if(batterijpercentage_int==2){
        //nothing to do
      }
  }
}

i try to use a pullup to +5V for PIN12 but it didnt change anything.
arduino is powered by USB from laptop
relay is powered by +5V and GND of the arduino, IN is connected to pin 12.
COM is connected to phase
NO is connected to phase of laptop charger.

thanks for help!!
Evert

The digital read on an output pin immediately changes it from an output to an input, from that point on in your program.

Have you tried using another relay?

I think it's not

No, you can safely read the state of an output.

//#include <SoftwareSerial.h>
#define relay 12

byte batterijpercentage_int;

void setup()
{
  Serial.begin(115200);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
}

void loop() {
  static byte state = 0;
  static byte input = 0;
  if (Serial.available() > 0) {
    input = Serial.read();
  }
  if (input > 47 && input < 51) {
    batterijpercentage_int = input - 48;
    input = 0;
    Serial.println(batterijpercentage_int);
  }
  if (batterijpercentage_int == 0 && state != 0) {
    state = 0;
    notLoadBattery();
  }
  if (batterijpercentage_int == 1 && state != 1) {
    state = 1;
    LoadBattery();
  }
  if (batterijpercentage_int == 2 && state != 2) {
    state = 2;
    Serial.println("do nothing");//nothing to do
  }
}

void LoadBattery() {
  digitalWrite(relay, LOW);
  Serial.println("C-C-Charge!");
  delay(3000);
}

void notLoadBattery() {
  digitalWrite(relay, HIGH);
  Serial.println("stop");
  delay(3000);
}


Yes, this is a second one, witch i buy new. But sometimes it works perfect, but sometimes it gives this error...

Image of your project, schematic diagram, project power. post.

I didn't upload schematics because I dont think that's the problem. As said before sometimes it works perfect.

Also, when i had an error with the relay, i hear something like a click of the relay. But the relay didn't realy switch.

By the way, the error is always when i want to switch off.

Could it be that the load, my batterij charger, is too inductive? Because, sometimes if i unpower the Arduino the battery is still charging but the relay is connected to NO.

I didn't realy think the code is the problem. If i didnt connect a load to the relay, i can switch perfectly. (I mean, i hear a 'loud' click while switching off and on.

Really?

How could it do that? :roll_eyes:

Not the code and not the hardware. Power Supply.

If you knew what the problem was you wouldn't have to ask.

If something works intermittently it is very rarely the code and most likely the hardware. So for real help pleas post what you have been asked for. Also don't post a Fritzing physical layout diagram they are useless for seeing what you have. There only use is if you want some one else who doesn't understand things to be able to make the same thing as you have.

"the relay ", WHICH relay?
Those cheap relays like Songles are known for welded contacts when switching inductive AC loads. Next time it fails to switch off, tap it with a small screw driver handle, see if it turns off.

Hi, @evertvandermeij
Can you please post a schematic of your project?
An image of a hand drawn circuit is fine, please include all power supplies, component names and pin labels.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Lack of relay contact arc suppression can cause this issue. I recommend adding an MOV across the contacts (NO and COM).

Its powered by the laptop USB.

Thanks, i think this can be a good option!

Hi,
Can you please post a link to data/specs of the relay module you are using along with your schematic?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I try this option and this works. it does what i expected!
Thanks dlloyd!!!

1 Like