Arduino Motion Sensor Control Led Light

by eglass1 in Circuits > Sensors

56501 Views, 73 Favorites, 0 Comments

Arduino Motion Sensor Control Led Light

photo 5.JPG
In need of a little helpful tutorial on how to wire and program your new motion sensor with your arduino? Well look no further. This tutorial will give you a simple example of how to set up a motion sensor to your arduino and turn an led on or off if motion is detected.

The Parts.

photo 1.JPG
Ok first off you're going to need an Arduino microcontroller whichever one you like. You will need a motion sensor, most have three pins, a vcc pin(the positive from the arduino), a ground pin(the ground), and an out pin (the output from the sensor) and you can get them pretty cheap online or about 13$ at radio shack. You will also need an LED bulb and some wire. Note that if your LED bulb is not capable of handling five volts you will need a resistor. I went ahead and used the standard white LED, most can handle 5 V so it's no big deal which made it easier to wire with no resistors needed. You also need your computer with the Arduino programming software and if you wish to use your circuit while not connected to your computer you will need an external power source.

Putting Together the Circuit.

photo 4.JPG
photo 2.JPG
photo 3.JPG
Okay now this is my favorite part, wiring the circuit! First off you need to place your controller if possible on a breadboard! I am using Arduino micro so it's very convenient to place on a breadboard if you're not using a micro you will need to use some jumper wires to connect your Arduino to a breadboard so you can plug your sensors and output devices in more easily. After you're all situated you're gonna go ahead and place a wire from the Arduino ground to the ground on that motion sensor this pin you should be labeled. You're also going to pick a pin on your Arduino and you're going to connect the out pin from the motion sensor which is labeled "out" to an empty pin on your Arduino I used the pin number five. Then you're going to connect the VCC output from the motion sensors to the VCC output of the Arduino this is the positive five volts that's coming out of the Arduino. Next go ahead and place your LED on the breadboard, the longer lead of the LED is the positive side or if both of your leads are the same you can look for a small notch on one of the sides of the LED this is going to be the negative side place it on your breadboard plug the negative side into the ground of the Arduino, you can use a jumper wire to go from the ground on the LED pen to the ground on the motion sensor then pick a pin that's empty on the Arduino and run a wire from it to the positive side of the LED. I used pin number 7 for this. We are all done wiring now let's go ahead and go to the next step, programming the controller.

Programming the Controller

photo 4.JPG
photo 5.JPG
Alright this part is pretty straightforward if you mess it up the program is going to let you know you miss something or it's just not gonna work. you do have to be careful as you don't want to fry the precious arduino and sensor. The code is pasted below, it's pretty straightforward you dim the pins as what they are either the motion sensor or the LED and if the motion sensor detects motion all you're going to do is have the led light up and if it doesn't detect motion your not going to light the led up. My motion sensor had a built in red LED that would light up when Motion was detected but I wired the white LED to light up as well that is what is being lit up from the code.

HERE IS THE CODE.

//Simple sketch by Eli Glass 7-3-13
//Simple led on/off with moiton sensor program
//Ok we need to declare the variables and pins.
//i have no variables but need to declare the pin the
//led is plugged into and the pin the motion sensor is on
//change the pin number to match what pins you use on your arduino
int motion = 5;
int motionLed = 7;

void setup() {
//ok i need to state what each pin will be doing. the led pin will
//be an output and the motion pin will be an input.
  pinMode(motion, INPUT);
  pinMode(motionLed, OUTPUT);
}

void loop()
{
  //what will happen in the sketch
  //if motion is detected we want to turn the led light on
  //if no motion is detected turn the led off
  //you also need to declare a variable to hold the sensor data
  long sensor = digitalRead(motion);
  //then the if statement to control the led
  if(sensor == HIGH){
     digitalWrite (motionLed, HIGH);
   }
   else
   {
      digitalWrite (motionLed, LOW);
   }
  
}


END OF CODE