This post is about a simple and interesting combination of software and hardware.
Last week, I picked up a couple of PIR sensors from Ebay.
PIR sensors are (usually) easy to interface with an arduino board.
Red: 5V, Black: Ground, Yellow: Analog In. Could it be easier than this?
After a bit of experimentation, I found that the board sends a pulse as soon as it detects some sort of movement. Also, it sends a pulse at the very beginning, when it sort of “boots”.
My ultimate aim was to start Skype as soon as movement was detected.
I loaded the standard firmata sketch on my arduino, see: http://arduino.cc/en/Reference/Firmata. Basically, the arduino simply sends all the analog port readings to Processing.
The following Processing code is simply a proof of concept. The sketch starts, looks for an arduino at the second serial port of my PC, waits for 5 secs for the PIR to send its first pulse and then, if movement is detected calls a number, using Skype.
At any moment, in the middle of the screen, you see the value of the PIR sensor.
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 | import cc.arduino.*; import java.io.*; import processing.serial.*; int analogPin = 5; int myV; // PIR sensor value PFont font; Arduino arduino; ////////////////////////////////////////////////////////////////////////////// void setup(){ size(400, 300); font = createFont("ArialUnicodeMS-12.vlw",22, false); //my board is the 2nd serial device, hence Arduino.list()[1] println(arduino.list()); arduino = new Arduino(this, Arduino.list()[1], 57600); } ////////////////////////////////////////////////////////////////////////////// void draw(){ background(0); fill(255); // the PIR sensor is sending out HIGH signal in the first 5 secs // so we will ignore all the early signals if(millis() < 5000){ } else{ // myV is the scaled value of the PIR sensor myV = arduino.analogRead(analogPin); textFont(font, 22); textAlign(CENTER,CENTER); text("PIR sensor value: " + myV,width/2,height/2); text("Active for " + round(millis()/1000) + " seconds",width/2,height*3/4); callme(); } } ////////////////////////////////////////////////////////////////////////////// void callme(){ // you should be logged in on Skype and have some credit // if your are dialling an external line String mySkype = "C:\\Program Files\\Skype\\Phone\\skype.exe /nosplash /callto:+44798000000"; if(myV>100){ print(" MyV: " + myV + " "); try { String line; Process p = Runtime.getRuntime().exec(mySkype); p.waitFor(); System.out.println("EXIT: " + p.exitValue()); } catch (Exception err) { err.printStackTrace(); } delay(3000); } } ////////////////////////////////////////////////////////////////////////////// |
Here is a very short video of this test in action:
PIR Sensor – Test from Arkadian.Eu on Vimeo.