As I wrote last week, Ali and I decided to leave the software coding zone and get a better understanding of how the hardware is working on some very small sketches. We are complete noobs, so I am trying to document my experiences for anyone who might be interested.

In typical Loughlin style, we just bought or collected random parts and are hobbling them together to try to do something.

The project we completed is a basic PIR Motion detector that alerts us of any movement in a room. The code allows a 15 second GTFO for arming the device.

For this first project you will need:

  • Arduino Uno Board
  • A small solderless breadboard
  • Jumper, Resisters, LED (3 colors)
  • PIR Sensor

I tried to buy as many items in combo to speed up things: Arduino Uno, Breadboard, Jumpers, a nice holder for the boards, and a usb cable, next some LED and Resistors, and finally the PIR Sensor.

If you prefer to head to Radioshack, they carry all of these in stock.

This project is not complete but complete enough for a basic tutorial. I hope to integrate a networking shield and have the intruders busted in real-time. But for now:

How it works: Green light lets you know all is well, Red light lets you know movement has been detected. The device goes back and forth into movement / non movement state as people are walking around. But in my real life situation, I want to know if the alarm has been tripped at least 1 time. So in comes the yellow light. If it is on, shit could be crazy as someone has waltzed in front of your sensor. The only way to clear it is to hit the ‘reset’ button on the Arduino at this point. Pretty serious stuff. I imagine this will be a standard in home installation within the next two months. Everyone is afraid of the yellow light.

I will try to get a wiring diagram together but honestly I am still figuring that part out.

Let’s look at the code which I am familiar with:


// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int onLedPin =  7;      // These could be any pin number - I picked them due to spacing
const int offLedPin =  4;      //
const int alertLedPin =  10;      //This is the dreaded yellow pin that signals past tragedy
const unsigned long startTime=millis(); //When the app starts - we want to give it some time
                                        //to let us escape the trap
// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
void setup() {
  // initialize the LED pin as an output:

  pinMode(onLedPin, OUTPUT);   
  pinMode(offLedPin, OUTPUT); 
  pinMode(alertLedPin, OUTPUT); 
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);  
  digitalWrite(offLedPin, HIGH);
  digitalWrite(onLedPin, LOW);    
  digitalWrite(alertLedPin, LOW);   
}
void loop(){
  // This code loops Foreeevvvver
  // read the state of the pushbutton value:
  delay(100);
  buttonState = digitalRead(buttonPin); //We are reading the state of the PIR sensor
  unsigned long currentMillis = millis(); //Getting the current time
  // checking if there is motion detected (HIGH means motion)
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(onLedPin, HIGH);  
    digitalWrite(offLedPin, LOW);  
    if((currentMillis-startTime) > 15000)
    {
   //This gives us a 15 second head start.  Depending on your application, you might need more time
//Turns on alert light (which never shuts off)   
     digitalWrite(alertLedPin, HIGH); 
    }
  } 
  else {
    // turn LED off:
    digitalWrite(offLedPin, HIGH);  
    digitalWrite(onLedPin, LOW); 
  }
  
}

That is pretty much it. You should be able to copy and paste that into the Arduino IDE and have it run.

PIR Sensor Example

Alison is helping me with a better photo and diagram tomorrow.


Leave a Reply