Introduction: MOTION SENSOR USING ARDUINO
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.This principle is used in many field like automatic open door,smart homes ....etc. Keep following for the Arduino ,HTML and CSS tutorials.
Step 1: ELECTRONICS REQUIRED :-
The parts required for this basic complex instructables are
- Arduino uno
- Jumper wire
- Motion sensor
- Breadboard
- LED
So let's get started with this instructables.
Step 2: BUILDING THE ELECTRONICS:-
- 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: CODING :-
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); }
}