Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560 Security System with PIR Motion Sensor and Keypad

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Security System with PIR Motion Sensor and Keypad

Circuit Documentation

Summary

This circuit consists of an Arduino Mega 2560 microcontroller, a PIR motion sensor, a red LED, a green LED, and a 4x4 keypad. The circuit is designed to detect motion using the PIR sensor and control the LEDs based on the input from the keypad. The green LED lights up when motion is detected, and the red LED lights up when the correct passcode is entered via the keypad.

Component List

  1. Arduino Mega 2560

    • Description: A microcontroller board based on the ATmega2560.
    • Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, AREF, SDA, SCL
  2. PIR Motion Sensor (Wokwi Compatible)

    • Description: A sensor that detects motion by measuring changes in infrared radiation.
    • Pins: VCC, OUT, GND
  3. LED: Two Pin (red)

    • Description: A red LED with two pins: anode and cathode.
    • Pins: cathode, anode
  4. LED: Two Pin (green)

    • Description: A green LED with two pins: anode and cathode.
    • Pins: cathode, anode
  5. 4x4 Keypad (Simulator)

    • Description: A 4x4 matrix keypad used for input.
    • Pins: R1, C4, R2, C2, R3, R4, C1, C3

Wiring Details

Arduino Mega 2560

PIR Motion Sensor (Wokwi Compatible)

LED: Two Pin (red)

LED: Two Pin (green)

4x4 Keypad (Simulator)

Code Documentation

#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {21, 13, 12, 11};
byte colPins[COLS] = {10, 9, 8, 7};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

String inputString = "";
const String passcode = "0000";

void setup() {
  Serial.begin(9600);

  // Set pin modes using direct port manipulation
  DDRD = 0xF8;

  // Initialize LED states
  PORTD &= ~(1 << PIND3); // Green LED anode LOW
  PORTD &= ~(1 << PIND6); // Green LED cathode LOW
  PORTD &= ~(1 << PIND5); // Red LED anode LOW
  PORTD &= ~(1 << PIND4); // Red LED cathode LOW
}

void loop() {
  // Read PIR sensor using direct port manipulation
  if (PIND & (1 << PIND2)) {
    PORTD |= (1 << PIND3); // Green LED anode HIGH
    PORTD &= ~(1 << PIND6); // Green LED cathode LOW
    delay(300000); // delay for 3 min
    PORTD &= ~(1 << PIND3); // Green LED anode Low
    PORTD &= ~(1 << PIND6); // Green LED cathode LOW
  } else {
    PORTD &= ~(1 << PIND3); // Green LED anode LOW
    PORTD &= ~(1 << PIND6); // Green LED cathode LOW
  }

  // Read keypad input
  char key = keypad.getKey();
  if (key) {
    delay(50); 
    inputString += key;
    if (inputString.length() == 4) {
      if (inputString == passcode) {
        PORTD |= (1 << PIND5); // Red LED anode HIGH
        PORTD &= ~(1 << PIND4); // Red LED cathode LOW
      } else {
        PORTD &= ~(1 << PIND5); // Red LED anode LOW
        PORTD &= ~(1 << PIND4); // Red LED cathode LOW
      }
      inputString = ""; 
    }
  }
}

This code initializes the keypad and sets up the pin modes for the LEDs and the PIR sensor. In the loop function, it checks for motion detected by the PIR sensor and lights up the green LED accordingly. It also reads input from the keypad and lights up the red LED if the correct passcode is entered.