Detects any motion nearby and sounds an alarm by sound and LED if motion are detected.

What will I learn?

You will learn how to read digital values from a connected sensor and perform some action based on the value read from the sensor. You also learn about how to use a buzzer and turn on an LED.

Prerequisites

A Computer with a configured Arduino IDE tested and working. If you don’t know how to get the basic set up working, check out my guide for Getting started (1/5)

Important notes

  • This project contains components that cannot handle 5V, so take care to use resistors (1 KOhm) with the LEDs.
  • Respect minus and plus, otherwise you may damage the components.
  • Start out by figuring out what components can handle 5V and what components can’t. If you connect a component that cant handle 5V you may destroy the component.
  • It can be hard to read the pins of the PIR sensor as they are hidden behind the plastic bulb. Gently remove the cover to see the pin descriptions and do NOT touch the sensor.

What you need

  • Breadboard
  • Dupont wires & jumper wires
  • CP2102
  • Arduino Pro Mini
  • PIR sensor
  • 1 KOhm resistor
  • 1 Color LED
  • 5V Buzzer

Connecting instructions

  • Arduino VCC -> PIR VCC
  • Arduino GND -> PIR GND
  • Arduino D6 -> PIR Signal
  • Arduino D9 -> Positive pin of Red LED
  • LED negative pin -> 1KOhm resistor and then to GND
  • Arduino D10 -> 5V Buzzer positive
  • Buzzer Neg. -> GND

 

Fritzing

pir-sensor_bb

Code

/**
* Alarm w. PIR sensor - Registers the distance between the distance sensor
* and an object and sounds the alarm if it detects motion.
*
* Connections:
* - Arduino VCC -> PIR VCC
* - Arduino GND -> PIR GND
* - Arduino D6 -> PIR Signal
* - Arduino D9 -> Positive pin of Red LED
* - LED negative pin -> 1KOhm resistor and then to GND
* - Arduino D10 -> 5V Buzzer positive
* - Buzzer Neg. -> GND
*
* Author: Jakob Maaløe
* https://wackydeveloper.wordpress.com
*/
#define pirPin 6
#define led 9
#define buzzer 10

int sound = 10;

int value = 0;

void setup() {
Serial.begin(115200); // Sets the baud rate for logging

pinMode(pirPin, INPUT);
pinMode(led, OUTPUT);
pinMode(buzzer, OUTPUT);

}

void loop() {
// Read the PIR
value = digitalRead(pirPin);
Serial.print("Value: ");
Serial.println(value);

if (value == HIGH) {
Serial.println("James bond is in the room!");
digitalWrite(led, HIGH);
tone(buzzer, sound);
} else {
digitalWrite(led, LOW);
noTone(buzzer);
}

// Note that the PIR sensor wont read properly until it has cooled down after an alarm. This could take several seconds.
delay(500);
}

Next steps

Try turning the small Potentiometers on the PIR sensor to see if they do anything, add a larger buzzer or transform it in to a wireless alarm system.

Then try the next project, the Laser trip-wire alarm.

Advertisements