Project tutorial
Motion Detection Lamp

Motion Detection Lamp © GPL3+

A motion detection lamp. It switches on and off when there's motion.

  • 4,799 views
  • 0 comments
  • 9 respects

Components and supplies

About this project

Overview

This motion detection lamp basically switches on and off when there is motion. The PIR sensor acts as a switch. Initially the idea was to make a mood lamp activated by light sensor. But with a few circumstances, the idea gradually changed. First and foremost, I assembled the wiring.

Step 1

Next, I wrote up the coding. The coding was not as easy as I thought, since I am still new to this. I had a few difficulties in writing it to make the LEDs turn on and off only when there is motion. The normal coding for motion allows it to only detect motion and automatically the LEDs will turn off when it does.

Step 2

After much trial and error, the write up is done, so I uploaded the code onto the Arduino Uno. Hence the assembly of the lamp begins. Even though the coding is done, there is a slight imperfection such that after the motion is detected to turn on the lamp, I need to wait for approximately 10 seconds after all motion has stopped to do another motion to turn off the lamp.

Step 3

A touch sensor was supposed to be a part of the lamp. But I had difficulty combining together both the codes for the PIR sensor and the touch sensor. They work separately, but when they are merged together it doesn't work as I planned. The touch sensor was meant to adjust the brightness of the lamp.

Code

MotionDetectionLamp codeArduino
This code is to detect motion for the Led's to on and off. The Pir Motion sensor acts as a switch.
 
int blue = 12;                // choose the pin for the LED
int red = 10;
int green = 11;
int green2 =5;
int red2 = 4;
int inputPin = 7;               // choose the input pin (for PIR sensor)
int pirState = LOW;             // we start, assuming no motion detected
int val = 0;                    // variable for reading the pin status
int x = HIGH;
int pirReading;
//int pulseSpeed = 100 ; 

void setup() {
  pinMode(blue, OUTPUT);      // declare LED as output
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(green2, OUTPUT);
  pinMode(red2, OUTPUT);
  pinMode(inputPin, INPUT);     // declare sensor as input
 
  Serial.begin(9600);
}
 
void loop(){

  val = digitalRead(inputPin); 
                     // read input value
                     
        if (val == HIGH) {  
          if (x == HIGH) {                    // check if the input is HIGH
          digitalWrite(blue, HIGH); 
          digitalWrite(red, HIGH);
          digitalWrite(green, HIGH);
          digitalWrite(green2, HIGH);
          digitalWrite (red2, HIGH);
          Serial.println("Motion 2");
          }
          else{
           digitalWrite(blue, LOW);
           digitalWrite(red, LOW);
           digitalWrite(green, LOW);
           digitalWrite(green2, LOW);
           digitalWrite(red2, LOW);
           Serial.println("Motion 1");  
          }
          if (pirState == LOW) {
           // we have just turned on
            Serial.println("Motion detected!");
            // We only want to print on the output change, not state
            pirState = HIGH;
            } 
          
  
        }   
    
    else {
    
    if (pirState == HIGH){
      if (x==HIGH){
        x=LOW;
      }
      else {
        x=HIGH;
      }
      // we have just turned of
      Serial.println("Motion ended!");
      // We only want to print on the output change, not state
      pirState = LOW;
      
    }
    //delay(pulseSpeed);
  }
}

Schematics

Basic connection with pir
Motion qegdcmfm0u

Comments

Similar projects you might like

Motion Activated Night Light

Project tutorial by Haziq Afandi

  • 3,114 views
  • 0 comments
  • 4 respects

Easy Motion and Gesture Detection by PIR Sensor & Arduino

Project tutorial by ElectroPeak

  • 26,522 views
  • 9 comments
  • 214 respects

Coolest Motion Detection Sensor ever

by remnis

  • 8,113 views
  • 2 comments
  • 7 respects

Complete Digital Clock Including Alarm and Motion Sensor

Project tutorial by LAGSILVA

  • 12,677 views
  • 11 comments
  • 50 respects

Motion activated SMS Shield- Tutorial

Project tutorial by Burger

  • 3,602 views
  • 2 comments
  • 5 respects
Add projectSign up / Login