Motion alarm with a PIR sensorTinkerPieCircuit tutorial 22 of 24 · Intermediate · 20 minutes
Sound an alarm and flash an LED when a PIR sensor sees movement.
Open this circuit in TinkerPieCircuit (free, runs in your browser)
What you will learn
- How a PIR sensor detects body heat moving
- Digital input HIGH and LOW
- Making a siren with two tones
Parts
Steps
Wire the PIR
VCC →
3V3, GND →GND, OUT →P10.Wire the LED
S1→ resistor → LED (+), LED (−) →GND.Read it
A PIR sensor gives HIGH while it sees motion.
Make a siren
Switch between 1200 Hz and 800 Hz with
tone()and short delays.Test
Press ▶ Start and click the MOTION switch on the sensor.
The code
function setup() {
pinMode(P10, INPUT);
pinMode(S1, OUTPUT);
}
function loop() {
if (digitalRead(P10) == HIGH) {
digitalWrite(S1, HIGH);
tone(BUZZER, 1200);
delay(150);
tone(BUZZER, 800);
delay(150);
} else {
digitalWrite(S1, LOW);
noTone(BUZZER);
}
delay(20);
}
Copy this into the Code tab. It is Arduino-style JavaScript. The real board uses the same pin names (S1, P10…) in TinkerPie Studio.
How it works
A PIR (passive infrared) sensor notices changes in heat from a moving person or animal. It does not send anything out, which is why it is called passive.
Challenge: Keep the alarm going for 5 seconds after the last movement.
← Automatic door with IR and a servo · All 24 circuit tutorials · Thermometer and fever light →