loading
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.

Step 1: The Parts.

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.

Step 2: Putting Together the Circuit.

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.

Step 3: Programming the Controller

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
<p>Hi there,</p><p>I have an idea that i'd like to create - i'd like a light sensor to control a PIR which then turns on a string of leds.. The idea that this light is only 'armed' at night so in the day it stays off.. would this be possible with arduino and modifying a bit your card?</p>
<p>yes it is possible and pretty simple in fact, you can do that by using RTC module for arduino </p>
<p>I have a question I hope you can help me people. How can a motion sensor be used in medical fields?</p>
If you wanted it to stay on until motion isn't detected you would just remove the &quot;delay&quot; after the part where it detects motion.
After the &quot;digitalWrite(motionlLED, HIGH);&quot;<br>You would add a delay. So you would add this line of code <br>&quot;delay(xAmount);&quot;<br>You would put A number in instead of saying xAmount and this would be in microseconds so that's 1000 micro seconds to 1 second then that by 60 for 60 seconds would be 60,000 (so now your code would be &quot;delay(60000);&quot;<br>This will tell the microcontroller to wait 60 seconds before performing any other action. A lot of this motion sensors you get off of eBay Will have a built in timer function so that they have a positive output for a range of seconds like from 5 to 200 seconds, this is helpful because the delay function is on the sensor itself letting the micro controller do other functions.
<p>What if I want it to just stay on until motion is no longer detected?</p>
<p>After the &quot;digitalWrite(motionlLED, HIGH);&quot;<br>You would add a delay. So you would add this line of code <br>&quot;delay(xAmount);&quot;<br>You would put A number in instead of saying xAmount and this would be in microseconds so that's 1000 micro seconds to 1 second then that by 60 for 60 seconds would be 60,000 (so now your code would be &quot;delay(60000);&quot;<br>This will tell the microcontroller to wait 60 seconds before performing any other action. A lot of this motion sensors you get off of eBay Will have a built in timer function so that they have a positive output for a range of seconds like from 5 to 200 seconds, this is helpful because the delay function is on the sensor itself letting the micro controller do other functions.</p><p>**************Here is the complete code you would need:*************</p><p>//Simple sketch by Eli Glass 1-28-15</p><p>//Simple led on/off with moiton sensor program<br>//Ok we need to declare the variables and pins.<br>//i have no variables but need to declare the pin the<br>//led is plugged into and the pin the motion sensor is on<br>//change the pin number to match what pins you use on your arduino<br>int motion = 5;<br>int motionLed = 7;<br><br>void setup() {<br>//ok i need to state what each pin will be doing. the led pin will<br>//be an output and the motion pin will be an input.<br> pinMode(motion, INPUT);<br> pinMode(motionLed, OUTPUT);<br>}<br><br>void loop()<br>{<br> //what will happen in the sketch<br> //if motion is detected we want to turn the led light on<br> //if no motion is detected turn the led off<br> //you also need to declare a variable to hold the sensor data<br> long sensor = digitalRead(motion);<br> //then the if statement to control the led<br> if(sensor == HIGH){<br> digitalWrite (motionLed, HIGH);</p><p>delay(60000);</p><p> }<br> else<br> {<br> digitalWrite (motionLed, LOW);<br> }<br><br>}</p>
<p>So what would I code if I wanted the light to stay on for say, 60 seconds after detecting motion?</p>
<p>So what would I code if I wanted the light to stay on for say, 60 seconds after detecting motion?</p>

About This Instructable

43,312views

68favorites

License:

Bio: Attending OU (University of Oklahoma) for a bachelors degree in computer science. I have already obtained and associates in science. I absolutely love working with ... More »
More by eglass1:Map Microsoft onedrive as letter drive on computer Create a simple delay in your VB Visual Basics Program Communicating with arduino micro controller and computer by usb with a vb program  
Add instructable to: