Relay triggers onwantted on startup

I've made a relay board to switch a relay using this simple diagram I found on circuitdigest.com
On startup it briefly triggers on off, after that it works fine. How can I prevent the relay from doing that?

Parts I used

Relay 5v SMIH-05VDC-SL-C 8Pin
BC547 transistor
1k resistor
1n4007 diode

Diagram

Try a 10K resistor from A0 to Gnd so the transistor is fully off when power is first applied to the Uno.

I tried it like this but still triggers on reboot

Controlling-Relay-using-Arduino-circuit-diagram2.png

Controlling-Relay-using-Arduino-circuit-diagram2.png

Few more details witch I don't know if they are imported

I'm using a Mega 2560 R3
whit a Sensor Shield for Arduino Mega 2560 from RobotDyn aliexpress
LCD module TFT 2.4 inch
the relay is connected to pin 29
and only using usb power.

And I had this same Issue before whit a relay module I bought from aliexpress
I mange to get around the relay switching on startup or reboot by adding INPUT_PULLUP in my setup before setting the pins to outputs.

biggizmo:
And I had this same Issue before whit a relay module I bought from aliexpress
I mange to get around the relay switching on startup or reboot by adding INPUT_PULLUP in my setup before setting the pins to outputs.

Then use the same circuit/code.

Use a BC557 (PNP), with emitter to 5volt, collector to relay coil, 1k between pin/base.
Other side of the relay coil to ground, diode ring/cathode towards collector.
Could be better to change that 1k resistor to 470 ohm, for better saturation of the transistor. Assuming you have a common ~75mA blue sugarcube relay.
Leo..

biggizmo:
And I had this same Issue before whit a relay module I bought from aliexpress
I mange to get around the relay switching on startup or reboot by adding INPUT_PULLUP in my setup before setting the pins to outputs.

Wawa:
Then use the same circuit/code.

Err, no!

Using that same low-level trigger code will cause the relay to initially trigger during startup with a high-level trigger circuit.
The 10k resistor is simply unnecessary with a transistor, only necessary with a FET. :roll_eyes: (Note there is no such resistor in the circuit below.)

If the relay operates briefly on startup with a high-level trigger transistor switch, it is because you programmed it to do so!

Here is the low-level trigger relay circuit. You need to digitalWrite the pin HIGH (which is the same as setting INPUT_PULLUP but does not actually set the pin as an INPUT, so is simpler) before setting it as OUTPUT. That is all.

But for a high-level trigger relay, you must not do that. :grinning:

Paul__B:
Err, no!

Should have explained it better.
Those relay boards use a high-side reference with a ~2mA sink current (active LOW).
Can do the same, but without the opto isolation, with a PNP transistor as I described.
Imagine replacing the opto LED with BE of the PNP transistor.

There could of course be an issue with OP's code, which was not posted.
The NPN circuit should work.
Leo..

Use a BC557 (PNP), with emitter to 5volt, collector to relay coil, 1k between pin/base.
Other side of the relay coil to ground, diode ring/cathode towards collector.
Could be better to change that 1k resistor to 470 ohm, for better saturation of the transistor.

have tried your suggestion, also have try switching the emitter to ground and collector to 5v as that is the dereliction of the arrow. but the relay does not trigger at all then in both of those cases ???

Assuming you have a common ~75mA blue sugarcube relay.

well it is a blue relay, after some research I found some ware some more technical info naradaelectronics.rw

think it is more around 106mA

There could of course be an issue with OP’s code, which was not posted.

That could be but that not explains why a relay board from aliexpres works fine but my own build one not. I can try see if I can post my code but it is rather large. But I can post parts from it

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
few more library's and lot more things defend but all on-relevant 

#define PumpRelay   29
int  PumpState ;

void setup(){

 pinMode(PumpRelay, INPUT_PULLUP);
 pinMode(PumpRelay, OUTPUT);


}


void loop(){
 bool buttonPressed = Touch_getXY();
 Pump_btn.press(buttonPressed && Pump_btn.contains(pixel_x, pixel_y));

 if (Pump_btn.justReleased())
    Pump_btn.drawButton();
  if (Pump_btn.justPressed()) {
    Pump_btn.drawButton(true);
    PumpState++;
    Pump_relay ();
  }

}



void Pump_relay () { // Pump relay
  if ( PumpState == 1) {
    digitalWrite(PumpRelay, LOW);// Pump on
    Serial.print("\n\r");
    Serial.print("Water Pump is ON ");
    tft.fillRect(135, 60, 22, 22, GREEN);
  } else if  ( PumpState >= 2 or PumpState == 0 ) {
    digitalWrite(PumpRelay, HIGH);// Pump is OFF
    Serial.print("\n\r");
    Serial.print("Water Pump is OFF");
    tft.fillRect(135, 60, 22, 22, RED);
    PumpState = 0;
  }

}

Sounds like you might have the load connected to the NC relay terminal and turning the relay OFF to turn the load ON, post a clear picture.

Sounds like you might have the load connected to the NC relay terminal and turning the relay OFF to turn the load ON, post a clear picture.

sure

topside

Here is the low-level trigger relay circuit. You need to digitalWrite the pin HIGH (which is the same as setting INPUT_PULLUP but does not actually set the pin as an INPUT, so is simpler) before setting it as OUTPUT. That is all.

that looks like almost the same circuit as on the aliexp relay board,
so what your saying is add a Optocoupler (PC817 ) or something ?

Try this version of your program, if it still clicks on on startup, may be something with the touch screen.

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
few more library's and lot more things defend but all on-relevant

#define PumpRelay   29

void setup(){

 //pinMode(PumpRelay, INPUT_PULLUP);
 pinMode(PumpRelay, OUTPUT);


}


void loop(){
 bool buttonPressed = Touch_getXY();
 Pump_btn.press(buttonPressed && Pump_btn.contains(pixel_x, pixel_y));

 if (Pump_btn.justReleased())
    Pump_btn.drawButton();
  if (Pump_btn.justPressed()) {
    Pump_btn.drawButton(true);
    PumpState++;
    Pump_relay ();
  }

}



void Pump_relay () { // Pump relay
  if ( PumpState == 1) {
    digitalWrite(PumpRelay, HIGH);// Pump on
    Serial.print("\n\r");
    Serial.print("Water Pump is ON ");
    tft.fillRect(135, 60, 22, 22, GREEN);
  } else if  ( PumpState >= 2 or PumpState == 0 ) {
    digitalWrite(PumpRelay, LOW);// Pump is OFF
    Serial.print("\n\r");
    Serial.print("Water Pump is OFF");
    tft.fillRect(135, 60, 22, 22, RED);
    PumpState = 0;
  }

}

Try this version of your program, if it still clicks on on startup,

so without setting the pin to INPUT_PULLUP or digitalWrite the pin HIGH, the relay stays active til it is turned of, and a reboot triggers it.

may be something with the touch screen.

aliexp board works perfect but mine triggers unwanted, don't get what that can be related to the screen.

#define PumpRelay   29  //WHY 29 ?       why not A0

void setup()
{
 digitalWrite(PumpRelay, LOW);
 pinMode(PumpRelay, OUTPUT);
}
#define PumpRelay   29  //WHY 29 ?       why not A0

void setup()
{
digitalWrite(PumpRelay, LOW);
pinMode(PumpRelay, OUTPUT);
}

My touch screen uses pin A0/A5 and bunch of other pins,
But what does it matter witch pin I use? as long as I make sure the pin can do what i need it to do.
Mega has plenty to use

setting the pin to LOW will set the relay on and keep it on, till it is set off,
a restart will trigger the relay on. same as setting it direct to output.

I think it is more the difference in electric circuit, that is causing the problem.
since the aliexp relay boards use a optocoupler and transistor,
as Paul__B shown in the diagram he posted,
that properly a better way of how I sought make my circuit,

I don't have any optocoupler laying around to make that circuit to test it,
have to look more into them to know witch one to order.

seen some relay diagrams online using 2 transistors one a PNP and the other NPN,
will give those a try

“ But what does it matter witch pin I use? ”

It matters because your schematic does not agree with your code.

We give answers based on the data you offer us.


“setting the pin to LOW will set the relay on and keep it on,”

BC547 is an NPN, it needs a HIGH to turn it on, hence the relay energizes !

A LOW turns the transistor off and releases the relay !

That’s why you want to start out with the output pin a LOW.


Look, we are trying to help you but it seems like you are being difficult.

If you want help then try the things we are suggesting !

" But what does it matter witch pin I use? "

It matters because your schematic does not agree with your code.

We give answers based on the data you offer us.

ok sought have changed the schematic to match my code

"setting the pin to LOW will set the relay on and keep it on,"

BC547 is an NPN, it needs a HIGH to turn it on, hence the relay energizes !

A LOW turns the transistor off and releases the relay !

That's why you want to start out with the output pin a LOW.

ahhh ok, I see, now I get it,
I thought I had to use a LOW for on, I overlooked and not see the change made in relay function

I made the changes and this works perfectly

Thx everyone for the help

BC547 is an NPN, it needs a HIGH to turn it on, hence the relay energizes !

A LOW turns the transistor off and releases the relay !

So if I change to a PNP then I can do a LOW trigger to turn on Relay.

It my first time I'm working whit transistors :confused:

"So if I change to a PNP then I can do a LOW trigger to turn on Relay."

Yes

The relay is always in the collector leg, there for for a PNP the emitter would be

connected to +5, the collector to the relay and the other terminal on the relay connected to GND.

The reversed biased kickback diode still stays across the relay with the cathode to the collector.