Arduino: build motion detectors with alarm - how it works
With the Arduino, you can not only build fun LED cubes, but also more practical things like a motion detector with alarm function. You can find out how to do this in this practical tip.
Arduino: how does the PIR module work?
To understand how to output tones with the Arduino, you should first read our article on it. You can then build on it.
- In our test, we use a HC-SR501 module as a motion detector, which can be bought on the Internet for little money.
- The module is relatively easy to use. As soon as the PIR sensor detects movement, a voltage of 5 volts is applied to the data pin, which can be measured by the Arduino.
- In turn, two small potentiometers are attached to the module. Use the left potentiometer to control the duration of the output signal and the right one to adjust the sensitivity.
Arduino motion detector: build the circuit
Connect the PIR module to the ground and to a 5V pin. The data pin is connected to a pin of your choice.
- Also connect the buzzer to a ground and a pin of your choice.
- However, be sure to use a PWM pin if you want to use a passive buzzer.
Program the Arduino motion detector
We have programmed a small code for you that you can use for your motion detector. However, note that the code is designed for a passive buzzer. If necessary, you can adapt it to your needs.
- int PIR_Pin = 12;
- int buzzer = 9;
- boolean b;
- void setup () {
- pinMode (PIR_Pin, INPUT);
- pinMode (buzzer, OUTPUT);
- }
- void loop () {
- b = digitalRead (PIR_Pin);
- if (b == HIGH) {
- tone (9, 2000);
- } else {
- noTone (9);
- }
- }
In the next practical tip, you will learn how the for loop works with the Arduino.