PIR sensor wireless Alarm

PIR sensor Wireless Alarm with 433 Mhz RF Transmitter and Receiver Module
//https://www.instructables.com/ARDUINO-WIRELESS-HOME-SECURITY-SYSTEM/















 Arduin Sketch for Transmitter

// download and install virtualwire library from Github-https://github.com/song940/VirtualWire
// Include VirtualWire library
#include <VirtualWire.h>

int led_pin = 13;// //connect LED to Pin 13 of Arduino
int transmit_pin = 12;//connect RF433 data to Pin 12 of Arduino
int pir_pin = 2;//connect PIR sensor signal pin to Pin 2 of Arduino
int btn_pin = 3;//connect Button to Pin 3 of Arduino
int pir_val = 0; // variable for store the status of PIR sensor
int btn_val = 0;// variable for store the status of push button
int pir_state = LOW;//set LOW the initial value
int btn_state = LOW;//set LOW the initial value
void setup()
{
   Serial.begin(9600);// set baud rate 9600 for serial communication
   vw_set_tx_pin(transmit_pin);
   vw_setup(4000); // Transmission rate
   pinMode(led_pin, OUTPUT);//Initilized pin 13 as an OUTPUT
   pinMode(pir_pin,INPUT);//Initilized pin 2 as an INPUT
   pinMode(btn_pin,INPUT);//Initilized pin 3 as an INPUT
}
 
void loop()
{
  char msg[1] = {'0'};
  // Get sensor value
  pir_val = digitalRead(pir_pin);// variable pir_val will store the value of pir_pin(2)
  btn_val = digitalRead(btn_pin);// variable btn_val will store the value of btn_pin(3)
  // Change message if motion is detected
  if (pir_val || btn_val == 1)
  {
      msg[0] = '1';
      digitalWrite(led_pin, HIGH); // Flash a light to show transmitting
      vw_send((uint8_t *)msg, 1);
      vw_wait_tx(); // Wait until the whole message is gone
      if (pir_state == LOW) 
      {
      Serial.println("Motion detected!");
      pir_state = HIGH;
      }
   }
 else
 {
   msg[0] = '0';
   digitalWrite(led_pin, LOW);
   vw_send((uint8_t *)msg, 1);
   vw_wait_tx(); // Wait until the whole message is gone
   if (pir_state == HIGH)
   {
      Serial.println("Motion ended!");
      pir_state = LOW;
   }
  }
}


Arduino Sketch for Receiver 

// Include VirtualWire library
#include <VirtualWire.h>
 
// Pins definition
const int led_pin = 13;//connect LED to Pin 13 of Arduino
const int receive_pin = 12;//connect 433Mhz RF Receiver module Data pin  to Pin 12 of Arduino
int Buzzer_pin = 10; //connect Buzzer to Pin 10 of Arduino
 
void setup()
{
   Serial.begin(9600); // Debugging only
   // Initialise the IO and ISR
   vw_set_rx_pin(receive_pin);
   vw_setup(4000); // Transmission rate
   // Start the receiver PLL
   vw_rx_start();
   // Set LED pin and Buzzer
   pinMode(led_pin, OUTPUT);// set led_pin as an OUTPUT
   pinMode(Buzzer_pin, OUTPUT);// set Buzzer_pin as an OUTPUT
}
 
void loop()
{
   uint8_t buf[VW_MAX_MESSAGE_LEN];
   uint8_t buflen = VW_MAX_MESSAGE_LEN;
 
   // Check if a message was received
    if (vw_get_message(buf, &buflen)) 
    {
      if(buf[0]=='1')
      {
      Serial.println("Motion detected!");
      digitalWrite(led_pin,1);
      playTone(300, 160);// calling the play_Tone function with frequency-300 for 160 ms
      delay(150);
      }  
     if(buf[0]=='0')
     {
     Serial.println("Motion ended!");
     digitalWrite(led_pin,0);
     playTone(0, 0);// No play_Tone
     delay(300); 
     }
   }
}

// duration in mSecs, frequency in hertz
void playTone(long duration, int freq) // user defined function(playTOne) for Play Buzzer 
{
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) 
    {
    digitalWrite(Buzzer_pin,HIGH);
    delayMicroseconds(period / 2);
    digitalWrite(Buzzer_pin, LOW);
    delayMicroseconds(period / 2);
    elapsed_time += (period);
   }
}

Comments

Popular posts from this blog

4 Stage Timer With DS3231RTC Module.

Interfacing KY038 sound sensor with Raspberry pi Pico

Interfacing MQ2 Gas & Smoke Sensor With Raspberry Pi Pico