Sunday, September 9, 2012


Using Motion Detectors With an Arduino

Halloween is coming up faster than you think: time to dust off the gravestones and the zombie lawn decorations! This year you may want to add a little something extra to give the trick-or-treaters a bit of a thrill. Using motion detectors and an Arduino you can trigger lights, fans, or even an pneumatically controlled cleaver-wielding clown to help add your house to the “if you dare” list.
There are different types of motion detectors. Some work by sensing sound, some send out light and sense when the beam is broken or reflected back. Another kind is known as the Passive Infared Motion Detector or PIR. These sense the infared radiation emitted by all objects above absolute zero. When something passes in front of a PIR sensor it detects the change in background temperature and then triggers an output voltage. This voltage can be read by an Arduino digital input and then the Arduino can act upon it.
The PIR Motion Sensor on the left is made by Parallax and sold by Radio Shack. The one on the right comes from Sparkfun. The difference between the two is important to note: The Sparkfun PIR has an open collector output — this means that the sensor requires a pullup resistor so that the output is always high, and when the sensor triggers it will pull the output low. The Parallax model is the opposite — the output is low until it is triggered, and then the output goes high. A simple way to think of the sensor is a switch; the diagram below explains the difference between the two approaches. It doesn’t matter which approach is used as long as you know which one you are using so you can write the code accordingly.
The circuit for the Parallax sensor looks like this:
Connections are as follows
  • PIR Pin “+” connects to Arduino +5
  • PIR Pin “-” connects to Arduino gnd
  • PIR Pin “out” connects to Arduino Digital 2
Here is the Arduino code for the Parallax sensor

int motion_1 = 2;
int light_1 = 13;
void setup(){
  pinMode (motion_1,INPUT);
  pinMode (light_1, OUTPUT);
}
void loop (){
  digitalWrite (light_1,LOW);
  delay(1000); //this delay is to let the sensor settle down before taking a reading
  int sensor_1 = digitalRead(motion_1);\
  if (sensor_1 == HIGH){
    digitalWrite(light_1,HIGH);
    delay(500);
    digitalWrite(light_1,LOW);
    delay(500);
  }
}

Here is the circuit for the Sparkfun Active Low style sensor.
  • PIR Pin “+” connects to Arduino +5
  • one side of the 10K Resistor Connects to Arduino +5
  • PIR Pin “-” connects to Arduino gnd
  • Other side of 10K Resistor connects to PIR Pin “Alarm”
  • PIR Pin “Alarm” connects to Arduino Digital 2
The code for the active low Sparkfun style motion detector looks like this.

int motion_1 = 2;
int light_1 = 13;
void setup(){
  pinMode (motion_1,INPUT);
  pinMode (light_1, OUTPUT);
}
void loop (){
  digitalWrite (light_1,LOW);
  delay(1000); //this delay is to let the sensor settle down before taking a reading
  int sensor_1 = digitalRead(motion_1);
  if (sensor_1 == LOW){
    digitalWrite(light_1,HIGH);
    delay(500);
    digitalWrite(light_1,LOW);
    delay(500);
  }
}
Regardless of the type you are using the code does the same thing: Check to see if the sensor is triggered; if it is, flash the on-board LED in front of digital pin 13. This is a good way to not only test the sensor but to see if it is going to be too sensitive and trigger to early. If it’s too sensitive you can use a cardboard tube to narrow the focus of the sensor. Just place the tube over the sensor to block any heat signatures approaching from the side.
Of course you can use multiple sensors and outputs to trigger different effects depending on your needs. The Arduino is capable of driving small loads like an LED without extra components. Next week we will look at driving larger loads like buzzers, lights and sound modules.
If you’re new to the Arduino, you should read Brian McLaughlin’s introductory articles about the Arduino. The Sparkfun PIR Sensor can be found on the Sparkfun Website as well as the the datasheet. The Parallax sensor can be found at Radioshack, and here is the datasheet.
Finally you should check out the Geekdad Community Forums. I will be setting this up as a project and you can post questions and comments there. Have fun playing with this and see you next week when we look at controlling bigger things with the Arduino.

No comments: