Home and IoTA Code Lock with a Keypad
Type the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.
Home and IoTA PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.
Written by the RoboHub team. A question? Write to us.
Choose a variation and the parts list updates.
The HC-SR501 has three pins: connect VCC to 5V, GND to ground and OUT to pin 2. The sensor gives a 3.3 V signal: if the Uno R4 doesn't notice movement, put a logic level shifter between OUT and pin 2.
Connect a red LED through a 330 Ω resistor to pin 7, and the buzzer's “+” to pin 8 and “−” to ground.
After power-on the sensor needs about 30 seconds to settle to the room. Don't move in front of it during that time.
const int PIR = 2, LED = 7, BUZZER = 8;
void setup() {
pinMode(PIR, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("Warming up the sensor...");
delay(30000); // the PIR needs about 30 s after power-on
Serial.println("Armed");
}
void loop() {
if (digitalRead(PIR) == HIGH) { // movement
Serial.println("Motion!");
for (int i = 0; i < 10; i++) { // 2 seconds of siren
digitalWrite(LED, HIGH);
tone(BUZZER, 1800); delay(100);
digitalWrite(LED, LOW);
tone(BUZZER, 1200); delay(100);
}
noTone(BUZZER);
}
delay(50);
}The sensor has two small knobs: one sets the sensitivity (up to 7 metres), the other how long the signal stays on. For quick testing, turn the time knob fully anticlockwise.

Illustration, made with AI
Illustration: at night in a hallway the motion sensor faces the door; a cat walks past and the red LED is lit, with a buzzer beside it.
Step 4: Tune the sensor
Home and IoTType the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.
GamesAn LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.
Lights and displaysCars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.