Hope you tried yesterday's LCD tutorial. Now, let's shift our attention from displays to sensors! Yup, I am talking about the PIR motion sensor!
Here's the scenario: You have some really sensible and emotional stuff in digitized form on your laptop (:O, I hope you try to understand what I wanna say! :P) which could get you in trouble if someone sees you with them! Now if you close the door and watch your stuff, someone's surely gonna know that something's fishy. What if you had some motion detector stuff, that could alarm you when someone's at the corridor? That would give you enough time to save yourself!!!
Yup, were not talking about high tech "James Bond" stuff here! I am talking about a simple $2 motion detector, which is accurate enough to save your a**.
Whatever be the reason, the PIR sensor can be used in a variety of projects starting from home automation to presence detector. It's your call.
What this project does?
Detects motion and turns on an LED as long as the motion is detected. Also, prints "Motion Detected" on the serial monitor.
Let's Learn The Basics!
According to Wikipedia,
Read more here: PIR Sensor
Now, the HC-SR501 PIR SENSOR, is a very famous sensor, available across all major E-Shopping sites.
My PIR Sensor looks like this:
My Favorite Sensor! |
Shown below, is an under view of the sensor:
Copyrights: Adafruit |
GND >> GROUND(0V)
OUT >> HIGH/LOW DATA OUT PIN
VCC >> 5V
Materials Required:
1. A HC-SR501 PIR Motion Sensor
2. Arduino
3. Jumpers
4. One LED
5. A 220OHM (or equivalent) resistor
6. Breadboard
7. and of course, a PC with Arduino IDE installed!
Procedure:
Software part:
1. FIRST UPLOAD THE SKETCH WITHOUT CONNECTING ANY COMPONENTS ACROSS THE ARDUINO PINS
2. Don't power on the Arduino, yet.
Hardware part:
1. Make the connections as shown:
The Setup |
Sketch:
/* * ON PUBLIC DOMAIN * Author : Nitish Dash * Name : Detect motion with HC-SR501 and Arduino * Created : 12/31/2014 * Webpage : http://goo.gl/qNoi9f * Author Email : nitishdash95@gmail.com * Author Website : http://www.nitishdash.com/ **** DONOT COPY AND PLAGIARATE WITHOUT THE AUTHOR'S PERMISSIONS **** */ int ct = 10; //calibration time int pirPin = 9; //PIR sensor's output int ledPin = 10; void setup(){ Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(pirPin, LOW); Serial.println("Please wait, now calibrating the sensor...."); for(int i = 0; i <= ct; i++){ Serial.print(((i*100)/ct)); Serial.print("% "); Serial.println("DONE....."); delay(1000); } Serial.println("Calibration Succesfully Done."); Serial.println("** SENSOR ACTIVE **"); delay(50); } void loop(){ if(digitalRead(pirPin) == HIGH) { digitalWrite(ledPin, HIGH); Serial.println("------------------------"); Serial.println("** MOTION DETECTED **"); Serial.println("------------------------"); Serial.println(""); delay(1000); } else { digitalWrite(ledPin, LOW); } }
How this works (Algorithm):
You might be knowing that every body emits radiations. Hot bodies emit infrared too. Now, our PIR sensor basically works on the principle of thermal imaging. Every body leaves a heat signature. The PIR sensor, continuously scans the environment for any thermal change and then compares the values on its processor. If there is a change in the thermal state of its environment, it sets the OUT pin to HIGH. And if there is no change in the thermal state, it gives a LOW value to its OUT Pin.
Now, we have simply used this theory in out code. We have used an if-else condition to set the LED on or off according to the OUT PIN's voltage. Simple. :)
Now, we have simply used this theory in out code. We have used an if-else condition to set the LED on or off according to the OUT PIN's voltage. Simple. :)
Something for you!
Try these ideas to enhance your knowledge and test yourself:
1. Try replacing the LED with a buzzer / piezo.
2. Try making a code that would send an SMS to your phone, whenever someone's at your home
3. Try making two circuits, one with the PIR and another with a buzzer and let them communicate via Bluetooth or FM(433Mhz module)
So, guys, there you go. I tried to explain the basics of a PIR sensor. In future projects, we'll go wireless and let the PIR transmit presence data even when you are travelling the world. We'll let arduino message us, whenever the sensor detects motion. We'll also make an android app, that would communicate with the Arduino via HC-05 Bluetooth module and send us data.
Bye! Thanks for reading the tutorial. Leave comments for sure!
Bye! Thanks for reading the tutorial. Leave comments for sure!
Thanks a lot bro..... This is working nicely. I've recently started my venture in these electronics stuff.... glad I found your blog.
ReplyDeleteBy the way, will this also work with Leonardo?
-John.
Thank you John for your feedback. Yes, I congratulate you for your journey into DIY.
DeleteYes, the code will also work with Arduino Leonardo. But, remember that Leonardo is somewhat different from UNO.
Unlike the Arduino Uno, the Leonardo and Micro won't restart your sketch when you open a serial port on the computer. That means you won't see serial data that's already been sent to the computer by the board, including, for example, most data sent in the setup() function.
This change means that if you're using any Serial print(), println() or write() statments in your setup, they won't show up when you open the serial monitor. To work around this, you can check to see if the serial port is open after calling Serial.begin() like so:
Serial.begin(9600);
// while the serial stream is not open, do nothing:
while (!Serial) ;