No. 7 – Motion detector

Task: As soon as a motion gets detected, a piezo speaker should beep.

Required equipment: Arduino / motion detector / breadboard / cables / piezo speaker

Learning content: Read out the voltage values of a motion detector and use them for an output.


Bew1The motion detector, also known as PIR sensor,is very simply constructed. Once it has detected a movement, it puts out  5V voltage on a pin. Now the microcontroller just has to read this
voltage out and processes it.

The duration of the of the output signal and
the sensibility (reach) of the motion detector
can be adjusted with the two knobs on it
(see image on the right).

Bew2

The plastic lens on top of the motion detector can easily be removed. Underneath of it, there is the IR- detector and the lettering of the three contacts. GND ( – ), OUT (Signal output), VCC ( + ). 

As shown in the image on the left. Furthermore there is a jumper on the bottom of the detector. This jumper makes it possible to switch between two different modes.

 
Bew31) Jumper on the outermost: The signal of the output will be maintained for a certain time, after the detector has recognized a movement. But after that certain time the signal will be deactivated, even if movement could be detected. The signal will be activated again after some time.

2) The other mode gets activated if the jumper is placed on the two inner contacts. The output signal will stay
constantly active, as long as a movement is detected.
This mode is recommended for
projects with Arduino.


Setup:

Bew4

Code:


int piezo=5; //The word “piezo” stands for the value 5.

int movement=7; //The word “movement” stands for the value 7.

int movementstatus=0; //The word “movementstatus” stands for the value 0. Later on there will be saved if a //movement is detected or not

void setup()

{ //The setup starts here

pinMode(piezo, OUTPUT); //The pin connected to the piezo speaker (pin 5) is //defined as an output.

pinMode(movement, INPUT); //The pin connected to the moition detector (pin 7)is //defined as an input.

}

void loop()

{ //The loop part starts here

movementstatus=digitalRead(movement); The value on pin 7 is read out (command: digitalRead). The result //will be safed under “movementstatus”. (HIGH means 5V and LOW means 0V)

if(movementstatus==HIGH) //if a movement is detected (voltage signal high) ..

{ //open program part of the IF-command

digitalWrite(piezo,HIGH); //..the piezo should beep

delay(5000); //5 seconds long

digitalWrite(piezo, LOW); //after that the piezo should be quiet

} //close program part of the IF-command

else

{ //open else-command

digitalWrite(piezo,LOW); //the piezo speaker should be turned off

} //close else-command

} //close loop part