Arduino PIR switch - with Relay

Help me by sharing this post


←PREVIOUS TUTORIAL       NEXT TUTORIAL→

In this short tutorial we will see how to mount the circuit, read the signal from the PIR sensor and then control the delay when the Relay will be switched on and by taht the light bulb will be turned on as well. See scheamtic and example code below.





PART 1 - Schematic

Below we have the schematic for this project. The PIR sensor will give a high signal for 3 seconds when it detects movement. Then we read that signal with the Arduino. At the same time we read the potentiometer value and map the delay between 5 seconds and 60 seconds. You could change that in the code if you want. The Arduino will control a NPN transistor that will activate te relay and by that turn on the light bulb.


We need:
1 x Arduino NANO: LINK eBay
1 x PIR module: LINK eBay
1 x 220VAC to 12VDC regulator: LINK eBay
1 x Relay: LINK eBay
1 x S8050 NPN transistor: LINK eBay
1 x 10k potentiometer: LINK eBay

PIR switch 220V Arduino relay


PART 2 - Code

Copy or downlaod the code from below. Make the connections as in the schematic above and then upload the code. The Arduino is supplied with 12V from the 220VAC to 12VDC regulator. Change this line "delay_time = map(analogRead(delay_in),0,1024,5000,60000); " if you want a different delay range than 5 to 60 seconds.


Download the example code:


int sensor_in = A0;
int delay_in = A1;
int relay_out = 3;

bool relay_activated = false;
unsigned long delay_time = 5000;

void setup() {
  pinMode(sensor_in,INPUT);
  pinMode(delay_in,INPUT);
  pinMode(relay_out,OUTPUT);
  digitalWrite(relay_out,relay_activated);  
}

void loop() {
  delay_time = map(analogRead(delay_in),0,1024,5000,60000);
  
  if(analogRead(sensor_in) > 500 && !relay_activated)
  {
    relay_activated = true;
    digitalWrite(relay_out,relay_activated);
    delay(delay_time);
  }

  if(analogRead(sensor_in) < 500 && relay_activated)
  {
    relay_activated = false;
    digitalWrite(relay_out,relay_activated);
    delay(10);
  }

  
}






PART 3 - On the breadboard

Below we can see the regulator that supplies 12V to the relay and the Arduino Vin pin. The PIR module is connected to the Arduino and the Arduino to a NPN transistor that activates the relay. The Relay is NO (noramlly open) so when we activate the transistor it will be closed so the light bulb is turned on.


PIR switch with delay Arduino relay








←PREVIOUS TUTORIAL       NEXT TUTORIAL→

Help me by sharing this post








yt_link
insta_link
fb_link
twitter_link

Arduino PIR switch
page 1/1



ADVERTISERS
PCBWAY PCB service





Gearbest Alfawise U30 2.8 inch Touch Screen DIY Desktop 3D Printer - U30 EU Plug Black
Alfawise U30 2.8 inch Touch Screen DIY Desktop 3D Printer - U30 EU Plug Black only $$162.89

AFFILATE

Gearbest Creality3D CR - 10S 3D Printer - EU Plug Upgrade Version Coffee and Black
Creality3D CR - 10S 3D Printer - EU Plug Upgrade Version Coffee and Black only $$352.94

Gearbest Creality 3D Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220 x 220 x 250mm Printing Size - EU Plug Black
Creality 3D Ender-3 V-slot Prusa I3 DIY 3D Printer Kit 220 x 220 x 250mm Printing Size - EU Plug Black only $$153.84

Gearbest Alfawise U20 Large Scale 2.8 inch Touch Screen DIY 3D Printer - EU - U20 EU Plug Black
Alfawise U20 Large Scale 2.8 inch Touch Screen DIY 3D Printer - EU - U20 EU Plug Black only $$253.39