PIR Motion Sensor + LCD Screen + Arduino Uno
For one reason or another, I've recently had the urge to follow in my father's footsteps and start tinkering with electronics. He's basically a wizard. He has fixed a lot of appliances that others tossed out the door without the slightest bit of investigation. I think he did this with the monitor I had hooked up to my very first computer. He just popped open the case, found the problem, soldered some solution in there, and that monitor worked for probably a decade afterwards. Amazing stuff.
Anyway, I have an itch to create a laser trip wire (don't ask). I took a basic electronics class my freshman year in college, so I am already familiar with resistors, capacitors, ICs, breadboards, soldering, etc. It doesn't seem like a very daunting task to create that trip wire, but I'm starting fresh with electronics (it's been a good 8 or 9 years since I last soldered or anything like that).
One of my co-workers mentioned the Arduino as something to get me started on the trip wire, so I started doing some research here and there. The more I read, the more excited I got. I wanted soo badly to buy all of the junk that I'd need to start tinkering with an Arduino, but I didn't think my wife would appreciate that--especially around Christmas time.
Several of my awesome relatives sent me very generous gift cards for Christmas this year, which finally gave me the opportunity to buy a load of stuff for the Arduino without feeling bad. So I ordered loads of stuff. Most of it is here, some of it is still on its way. My Arduino Uno arrived around noon today, and I haven't been able to stop tinkering! It's really a lot of fun, and stupid easy even if you're not a programmer!
I started with the basic "oooh, blinking LEDs!" sort of projects (or "sketches" in Arduino parlance). Then I moved on to tweak those to work with multiple LEDs. There were a few different scenarios I ran though because two of my LEDs weren't lighting up very well. I guess I either hooked them up wrong or I didn't seat them properly... whatever.
The next project was when I hooked up a PIR (passive infrared) motion sensor ($9.99 at RadioShack) and installed a demo sketch that I found on the Arduino wiki. I wasn't quite sure how to wire everything for the PIR sensor, so I took a look at this Make Magazine video to see one way of setting up the circuit. That one simply lights up an LED and makes some noise. I just made mine light up and LED since I don't have a buzzer (yet).
Next I moved on to the LCD. The package came with a 20x4 green-on-black backlit LCD display, a 2.2k Ohm resistor, and a set of pins to connect the LCD to my breadboard or whatever. I actually soldered the pins onto the LCD display board (wow, was that even more difficult than I remember!) so I would have less of a hassle getting all of the contacts working. Getting the LCD to work was pretty easy after that.
Then I took those two projects a bit further. I'm certain others have done this years ago, but I didn't use a sketch that was completely written for me this time so I felt special enough to share :) I added the PIR circuit from before less the LED, merged pieces from both demo programs, and came up with a motion sensor that would flash "INTRUDER ALERT!!" on the LCD screen a few times when triggered. Based on my testing, the sensor works extremely well (once calibrated!!), and it will detect motion well across the open area of my apartment (maybe a bit further than 20 feet)!
I'm quite happy with my work, especially for not having "dealt with" electronics for such a long time. When I tried to share my success with some of my friends, they wanted a video to prove I'm awesome (I guess?). So here it is. Trust me, I know the video is horrible--I speak too quietly (baby sleeping in next room), I stumble over my words (that's just me), and I neglected to even offer you some music to rock out to as I blabber about this stuff.
Here's the program I wrote/modified:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
/* * ////////////////////////////////////////////////// * //making sense of the Parallax PIR sensor's output * ////////////////////////////////////////////////// * * Switches a LED according to the state of the sensors output pin. Determines * the beginning and end of continuous motion sequences. * * @author: Kristian Gohlke / krigoo (_) gmail (_) com / http://krx.at * @author: Josh VanderLinden / codekoala (.) gmail (@) com / http://www.codekoala.com * @date: 3 Jan 2011 * * kr1 (cleft) 2006 * Released under a creative commons "Attribution-NonCommercial-ShareAlike 2.0" license * http://creativecommons.org/licenses/by-nc-sa/2.0/de/ * * * The Parallax PIR Sensor is an easy to use digital infrared motion sensor module. * (http://www.parallax.com/detail.asp?product_id=555-28027) * * The sensor's output pin goes to HIGH if motion is present. However, even if * motion is present it goes to LOW from time to time, which might give the * impression no motion is present. This program deals with this issue by * ignoring LOW-phases shorter than a given time, assuming continuous motion is * present during these phases. * */ #include <LiquidCrystal.h> int calibrationTime = 10; // seconds to calibrate PIR long unsigned int pause = 5000; // timeout before we "all" motion has ceased long unsigned int lowIn; // the time when the sensor outputs a low impulse boolean lockLow = true; boolean takeLowTime; int flashCnt = 4; // number of times the LCD will flash when there's motion int flashDelay = 500; // number of ms to wait while flashing LCD int pirPin = 7; // the digital pin connected to the PIR sensor's output int lcdPin = 13; // pin connected to LCD LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2); void setup() { // Calibrates the PIR Serial.begin(9600); pinMode(pirPin, INPUT); pinMode(lcdPin, OUTPUT); digitalWrite(pirPin, LOW); clearLcd(); // give the sensor some time to calibrate lcd.setCursor(0,0); lcd.print("Calibrating..."); for(int i = 0; i < calibrationTime; i++){ lcd.print("."); delay(1000); } lcd.print("done"); delay(50); } void clearLcd() { // Clears the LCD, turns off the backlight lcd.begin(20, 4); lcd.clear(); digitalWrite(lcdPin, LOW); } void alertLcd() { // Turns on the LCD backlight and notifies user of motion lcd.setCursor(2, 1); digitalWrite(lcdPin, HIGH); lcd.print("INTRUDER ALERT!!"); lcd.setCursor(2, 2); lcd.print("================"); } void loop() { // Main execution loop if(digitalRead(pirPin) == HIGH) { // flash an alert a few times for (int c = 0; c < flashCnt; c++) { alertLcd(); delay(flashDelay); lcd.clear(); delay(flashDelay); } if(lockLow) { // makes sure we wait for a transition to LOW before any further // output is made: lockLow = false; delay(50); } takeLowTime = true; } if (digitalRead(pirPin) == LOW) { clearLcd(); if (takeLowTime) { // save the time of the transition from high to LOW // make sure this is only done at the start of a LOW phase lowIn = millis(); takeLowTime = false; } // if the sensor is low for more than the given pause, // we assume that no more motion is going to happen if (!lockLow && millis() - lowIn > pause) { // makes sure this block of code is only executed again after // a new motion sequence has been detected lockLow = true; delay(50); } } } |