Arduino Motion Detection Simple Project

Motion Detection Simple Project (1)

Motion Detection Simple Project (1)










This is a very basic tutorial for Arduino beginners , we tried to keep it simple and to the point, how to use PIR motion detection sensors and how to control LED based on the sensor outpu.

and so simply put,The main idea of this project is to light a number of LEDs after sensing a motion with the PIR Sensor, LEDs are going to be turned on with a specific color which here we chose to be purple.. otherwise the LEDs are going to be off.

Step 1: Step 1:What Will You Need


For this project we will need :

  • LED
  • PIR sensor
  • Breadboard
  • Arduino
  • Jumpers

Step 2: How to Connect ThemConnect Them (1)













  1. Connect PIR Sensor to Arduino :
    1. – Yellow wire to PIN 2 in the Arduino
    2. – Black to GND
    3. – Red to 5V
  2. All LEDs to PIN 13 by using the breadboard as shown in the figure below

just follow the circuit diagram at the top, you can’t go wrong

Step 3: Copy the Code

Just copy the code to Arduino IDE , and upolad it to your Arduino , all commands are commented , so you can read it line by line to understand the whole thing


int ledPin = 13; // choose the pin for the LED

int inputPin = 2; // 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

//variables needed to store values

long pulse, inches, cm;

void setup() {

pinMode(ledPin, OUTPUT); // declare LED as output

pinMode(inputPin, INPUT); // declare sensor as input



void loop(){

val = digitalRead(inputPin); // read input value

if (val == HIGH) { // check if the input is HIGH

setColor(250,0,255,ledPin); // choose the color value

digitalWrite(ledPin, HIGH); // turn LED ON

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;

pulse = pulseIn(inputPin, HIGH);

//147uS per inch

inches = pulse/147;

//change inches to centimetres

cm = inches * 2.54;

Serial.print(inches); // print for the inches value

Serial.print(“in, “);


Serial.print(“cm”); // print for the centinetres value




} else {

digitalWrite(ledPin, LOW); // turn LED OFF

if (pirState == HIGH){

// we have just turned of

Serial.println(“Motion ended!”);

// We only want to print on the output change, not state

pirState = LOW;




void setColor(int red, int green, int blue,int led) // color function to set the color you want for the LED



red = 255 – red; // the red channel value

green = 255 – green; // the green channel value

blue = 255 – blue; // the blue channel value


// dedicate every channel value to get one color

analogWrite(led, red);

analogWrite(led, green);

analogWrite(led, blue);


Step 4: Conclusion

Conclusion 2 (1)











Read more: Arduino Motion Detection Simple Project

Leave a Comment

Your email address will not be published. Required fields are marked *