Thursday, January 13, 2011

Motion Sensor on Arduino

I already showed you the example of connecting a radar sensor to the Arduino. While radar sensors are very good at detecting frontal motion, they have their weaknesses when it comes to detecting motion in a wider angle. They are triggered by any object which reflects their radar waves.
For bypassing motion you normally use a passive infrared motion sensor (PIR). They aren't that great when it comes to detecting frontal motion, but they are sensitive to motion in a wider angle. They are called passive infrared motion sensors, because they detect infrared radiation which is emitted by every living person. The hotter your body temperature, the more you emit.

I used the ePIR sensor board from Zilog. You can configure the sensors sensitivity, you can set it to sleep to preserve energy and you can couple it with a light sensor so it won't be triggered at day time. For details on how to configure it, have a look into the datasheet. I used a simple default connection and didn't want to configure anything.

Here is a default setup:

The source code is also pretty simple:
int sleepModePin = 4;
int motionDetectPin = 2;
int alarmLED = 12;
int val;

void setup() {
  //the sleep pin has to be active high to enable detection
  digitalWrite(sleepModePin, HIGH);

void loop() {
  val = digitalRead(motionDetectPin);
  //the ePIR sensor is active low so if motion is detected the output is low
  if(val == LOW) {
    digitalWrite(alarmLED, HIGH);
  } else {
    digitalWrite(alarmLED, LOW);
Motion sensor in action:


Microphone on Arduino

Today I connected a microphone mini board from sparkfun to the Arduino. After soldering on some connector pins you have three connections. Vcc, which works from 2.7V up to 5.5V, GND and the Signal pin. I used 5V for Vcc. I used an additional 4.7kΩ resistor to pull up the ADC value read from the analog port of the Arduino. You might not need it though. As an indicator for signal strength / loudness, I used three LEDs which were triggered if a certain threshold is passed. So it is kind of an equalizer.

Here is the basic setup:

The code is pretty easy. The only thing you might change are the threshold values, because your background noise might differ.
int audioPort = 0;
int val;
int greenLed = 7;
int yellowLed = 4;
int redLed = 2;
int lowThreshold = 780;
int mediumThreshold = 890;
int highThreshold = 1000;

void setup() {
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT); 

void loop() {
  val = analogRead(audioPort);
  if(val > highThreshold) {
    digitalWrite(redLed, HIGH);
    digitalWrite(redLed, LOW);
  } else if(val > mediumThreshold) {
    digitalWrite(yellowLed, HIGH);
    digitalWrite(yellowLed, LOW);
  } else if(val > lowThreshold) {
    digitalWrite(greenLed, HIGH);
    digitalWrite(greenLed, LOW);
I wanted to use one of my favorite songs to demonstrate this experiment, but since I don't want to get into any trouble with the RIAA or the GEMA, I used a recording of me playing my dobro.