How to send high to relay in setup loop

Hello all,

I'm a total beginner to the Arduino, and to programming. I am currently trying to use a PIR sensor to set off a relay, with an Arduino nano as the controller. The code I am using is listed below. I have the PIR sensor into A2, and the output pin I am using is D4.

I would like the Arduino to send a HIGH to the relay on the initial setup, but I can't figure this out. Ideally, I would press the reset button on the nano and have the relay go off.

digitalWrite(relay,HIGH) seems like the most straightforward way to do this, but it doesn't work. I am not getting any compiling errors. Can anyone tell me what I am doing wrong, or what other ways of doing this might work?

thank you so much!


#define pir A2              //control pin 
#define pm_pin A5           //potentiometer pin, currently unused
const int relay = 4;        //relay pin
     


void setup() {
  pinMode(relay, OUTPUT);     //set pin mode
  Serial.begin(9600);         //initialize serial monitor
  digitalWrite(relay,HIGH);   //set off relay on void setup NOT WORKING
  
}
void loop() {

  int pirValue = analogRead(pir);  //read PIR sensor 
  
  if (pirValue > 100) {            //if PIR reads greater than 100
    digitalWrite(relay,HIGH);      //set off relay
    Serial.println(pirValue);      //print pirValue to facilitate calibration
  } else {
    digitalWrite(relay,LOW);       //do not set off relay
  }
  delay(500);                      //delay .5 seconds between loops for stability 

}

It probably is going high based on your code, but it’s immediately going low when it does Loop in your else.

Don’t put it low until your reset button is pressed. For how to code a button, see the Arduino example for for digital, button. Test that out with a button and led then integrate it into your project. Learn the basics from an example, then try to apply for your use.

I'm sure it does work. What do you think happens a few microseconds later when the code hits the if/else in loop() ?

Not without something describing what the "relay" is and how you have it wired and powered.

Also, are you using a relay from an example? The Arduino can’t directly drive most relays.

the rest of the code works as intended; the relay is the this one listed on amazon, and I can hear it going off during the void loop

Simply test it with a delay(2000); at the end of the setup.... (temporarily ofcourse..)

having read your comments, I added a 250ms delay at the end of the setup, which is enough time for the relay to get the high signal before initializing the main loop. I hadn't realized that the main loop would override the command send in the setup, and that is absolutely why is wasn't working.

is there a reason not to leave this in permanently? does it lead to unstable operation?

No, you may skip this line... a relay is soooooo slow....

Going off, as in fireworks, or going off, as in a room light?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.