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.
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.
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.