Hi All,
Would appreciate your assistance with troubleshooting my attempt to use a PIR sensor to turn on/off the onboard Arduino Uno LED and turn on/off a relay (which controls a 120V light). The sensor works, the LED works, but the relay will only turn on and remain on ...
/****************************************************
Use a PIR sensor to turn on/off the onboard LED and turn on/off a relay (which controls a 120V light)
Materials Schedule
Arduino Uno - 1 each
https://store.arduino.cc/usa/arduino-uno-rev3
DF Robot Gravity I/O Shield - 1 each
OSEPP Passive Infrared Sensor (PIR) Sensor HC-SR501 - 1 each
https://www.osepp.com/electronic-modules/sensor-modules/73-passive-infrared-sensor-pir-module
A-B Cable - 1 each
Wire Bundles - 3 female/female wires
Wire Bundle - 2 male/female wires
DLI IoT Relay - 1 each
Power Supply for Arduino 9V/1000mA - 1 each
120V Light - 1 each
Assembly
1.Connect A-B cable to arduino and laptop and upload sketch
2.Disconnect A-B cable
3.Connect aurduino and shield
4.Connect PIR sensor to arduino digital pin 3 with (3) female/female wires
(D) Digital Signal White
(+) Voltage VCC Red
(-) Ground GND Black
5.Connect IoT relay connector (+) (-) to arduino digital pin 13 with (2) male/female wires
(+) Voltage VCC Red
(-) Ground GND Black
*IoT Relay Frequently Asked Questions - FAQs
*About 0.2mA, the input is constant current
*12-120VAC or 3.3-48VDC will trigger the relay
*DC trigger input is polarity sensitive
6.Plug 9V power supply for arduino into relay outlet labeled "always on"
7.Plug 120V light into IoT relay outlet labled "normally off"
8.Plug IoT relay into mains outlet
Engineering
Originally created by rui santos
Modified by surfnm during 2019-01
****************************************************/
// Relay pin is controlled with digital pin 13. The active wire is connected to Normally Closed and common
// Volatile command https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/volatile/
int relay = 13;
volatile byte relayState = LOW;
// PIR Motion Sensor is connected to digital pin 3.
int PIRInterrupt = 3;
// Timer Variables
long lastDebounceTime = 0;
long debounceDelay = 10000;
// setup code here, runs once:
void setup() {
// Pin for relay module set as output
pinMode(relay, OUTPUT);
digitalWrite(relay, HIGH);
// PIR motion sensor set as an input
pinMode(PIRInterrupt, INPUT);
// Triggers detectMotion function on rising mode to turn the relay on, if the condition is met
attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);
// Onboard LED (Arduino Uno digital pin 13) setup
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
// Serial communication for debugging purposes
Serial.begin(9600);
}
// main code here, runs/loops repeatedly
void loop() {
// If 10 seconds have passed, the relay is turned off
if((millis() - lastDebounceTime) > debounceDelay && relayState == HIGH){
digitalWrite(relay, HIGH);
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
relayState = LOW;
Serial.println("Motion not detected: LED & Relay turned off");
}
delay(50);
}
// breakout of function used in the void loop
void detectMotion() {
Serial.println("Motion status");
if(relayState == LOW){
digitalWrite(relay, LOW);
}
relayState = HIGH;
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on by making the voltage HIGH
Serial.println("Motion detected: LED & Relay turned on");
lastDebounceTime = millis();
}