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
}