First Circuit – Motion Alarm

For my first excursion into things Arduino I had purchased the “Make: Ultimate Microcontroller Pack” kit from RadioShack. It contains an assortment of components, i.e. LEDs, capacitor, resistors, various sensors, LCD display …etc, as well as the Arduino Uno Rev 3 and the MakerShield prototyping “shield”.

I had also purchased a passive infrared sensor and decided to make a simple motion detection alarm.

Motion Alarm

Simple Motion Alarm

It is very basic – even trivial – but it helped ease me back into things.

I have to say that I’m very impressed with the whole Arduino platform. It’s the main reason I’m so keen to get back into electronics. I love its simplicity and, in particular, the ability to build out using the stackable “shield” boards. So cool.

As part of my journaling effort I want to document everything I do as best I can. Part of that is creating circuit schematics. I searched around to see if I could find a simple schematic editor… but, alas, for now, I’ve been unsuccessful. Like a failed Goldilocks experience I could not find one that was “just right”.

In the end I settled on CadSoft’s EAGLE software. I guess if I decide, later, to fabricate a board then the learning curve will be worth it. The schematic below was created with that software:

Circuit Schematic

And,  finally, here is the code for the microcontroller – or rather, in Arduino terminology, a “sketch” :

/*
  Motion Alarm
  Version 1.
  12/3/13
 */

int led = 7;            // pin LED is attached to
int buzzer = 2;         // pin buzzer is attached to
int motion = 8;         // pin motion detector is attached to
int buzFrames = 5;      // number of frames buzzer is to sound for
int frameTime = 100;    // frame duration in milliseconds
int frame = 0;          // current buzzer frame
int lastMotion = -1;    // state of the sensor on last frame

// the setup routine runs once when you press reset:
void setup()
{
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(motion, INPUT);
  Serial.begin(9600);
  Serial.println("Running...");
}

// the loop routine runs over and over again forever:
void loop()
{

  // sample motion
  int detectMotion = digitalRead(motion) == HIGH;

  if( detectMotion != lastMotion)
  {
    // we have a change
    digitalWrite(led, detectMotion ? HIGH : LOW );

    if( detectMotion )
    {
      Serial.print("motion detected");

      // start the buzzer if it is not already sounding
      if( frame == 0 )
      {
        Serial.print(" - starting buzzer");
        digitalWrite(buzzer, HIGH);
      }
      frame = buzFrames;  // number of frames till buzzer is stopped
    }
    else
    {
      // there has not been any new motion for a time
      Serial.println(" - motion reset");
    }

    // remember this state for the next frame
    lastMotion = detectMotion;
  }

  if( frame > 0 && --frame == 0)
  {
    // stop the buzzer
    Serial.print(" - stopping buzzer");
    if( !detectMotion )
    {
      // move to a new line
      Serial.println();
    }
    digitalWrite(buzzer, LOW);
  }

  delay(frameTime);               // wait for one frame
}

The sketch simply reads the sensor output ever 100 milliseconds. If the sensor goes high (i.e. motion has been detected) it starts the buzzer sounding and turns on the LED. After a short time (500 ms) it stops the buzzer. When the sensor’s output returns low the controller turns off the LED.

(The coding is a little more verbose than it needs to be with all the serial output.)

Advertisements

Comments welcome

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: