Security alarms, time lapse cameras, and those house-lights that turn on when you walk by all have something in common… Motion detection. Motion detection can be a very handy thing in installation art, interactive walls, and other times you need a cheep way to know when people are around.

PIR or Passive Infrared is a common method of motion detection that measure changes in heat to signal the change. The basic model is that they take IR (heat) images on 2 sensors at different times, when they differ, they know something has changed. This particular sensor from SparkFun beings the signal pin low when it senses a change.

Hooking it up

Hooking one up to your Arduino is pretty simple, but you need to make sure not to fall for the manufacturer’s trap. The red wire is V+ as you would think, the brown wire is ground, NOT the black wire. The black wire is the signal wire. To make matters even worse, the manufacturer uses a B/W image to show you what wire is what.

Aside from power, the signal pin connects to any digital pin on your Arduino but also needs a 10k pullup resistor between the signal and 5V.

The reason for the resistor is that the signal pin is something called an open-collector, meaning it is as if it was not connected to anything at all when nothing is signaled. When motion is sensed, it connects the pin to ground. So we use the resistor so when we read the value from that pin when no motion is detected it looks HIGH. Without the resistor the pin would be floating around and the read value would randomly float between HIGH and LOW, and we don’t want that.

Code

The code for this is pretty simple. When it starts up, it needs 2 seconds to take an image to compare to. When we see the signal pin go low, we print some text to the serial terminal (you can replace that with any code you want) and wait 2 seconds again before checking.

You could make the code a little more sophisticated to do away with the 2sec delay after motion is sensed, but I found that the signal pin triggered on and off for a few seconds after it first sensed motion, so that delay is just to take care of that.

int pirPin =2;//digital 2

void setup(){

Serial.begin(9600);

pinMode(pirPin,INPUT);

}

void loop(){

int pirVal =digitalRead(pirPin);

if(pirVal ==LOW){//was motion detected

Serial.println("Motion Detected");

delay(2000);

}

}

Unless otherwise stated, this code is released under the MIT License – Please use, change and share it.

So… after solved the last hangups, so more things changed by this side.

My new raspberry pi had arrived, and it was time do turn the one that i’ve been trying to Vasco.

So, took the sdcard from one, and plugged in another. Only have to change the internet stuff at router. At this time i’ve taked the usb cable from my samsung phone and replaced with a cheap one bought in ebay.

So, after that hangs came back….

Our software being developed had changed too.

After reading some stuff, I’ve decided to replace the usb cable with the previous. This hangs are different than the other in the past. Last one there was messages in the stdout about kernel panics… in this don’t.

In the beginning the overload was a thing that i blamed, but after being working in the pi and it blocked without anything special running… made me think. So replacing the cable with the samsung one helped a lot.

Still, it may solve some things, but there is a unsure rumour about a pl2303 interface being causing problems.

I’ve being using one in our projects to parse the XML supplied by a Current Cost Envi. So despite the huge load caused by python, the arduino usb interface being bug fixed to interface better with the home X10 interface, some lockups in the sqlite database used by python and web interface, and this new fact of the pl2303 i’m not quite sure of what’s the real problem.

Since the device don’t have anything on the screen when it crashes it’s complicated. My best bet is now the pl2303, which seen in a forum that can be solved with deleting the module and inserting it again with some tweaks.