Using an Arduino UnoⓇ, B.R.I.T developed a simple motion sensor with an integrated locking system. The ultrasonic sensor continuously measures distance, detecting any changes when an object passes in front of it. This change triggers an alarm and prompts the motor to rotate, locking the door. The alarm will persist until the reset button is pressed and held, returning the system to its initial unlocked state.
A simple yet functional and scalable motion-sensor lock system.
1. Arduino UnoⓇ Code
#include <Servo.h>
Servo myServo;
int time_traveled;
const int buzzer_pin = 4;
const int red_led = 6;
const int trig_pin = 3;
const int echo_pin = 2;
const int button = 11;
void setup() {
Serial.begin(115200);
pinMode(red_led, OUTPUT);
pinMode(buzzer_pin, OUTPUT);
pinMode(trig_pin, OUTPUT);
pinMode(echo_pin, INPUT);
pinMode(button, INPUT_PULLUP);
myServo.attach(9);
}
void loop() {
myServo.write(0); // Set servo to initial position
// Trigger ultrasonic pulse
digitalWrite(trig_pin, LOW);
delayMicroseconds(10);
digitalWrite(trig_pin, HIGH);
delayMicroseconds(10);
digitalWrite(trig_pin, LOW);
time_traveled = pulseIn(echo_pin, HIGH);
Serial.println(time_traveled);
delay(25);
// If object is close
while (time_traveled < 800) {
myServo.write(60); // Lock position
digitalWrite(red_led, HIGH); // Turn on alert LED
// Buzzer beep loop
for (int i = 0; i < 250; i++) {
digitalWrite(buzzer_pin, LOW);
delay(2);
digitalWrite(buzzer_pin, HIGH);
delay(2);
}
digitalWrite(red_led, LOW); // Turn off LED
delay(1500);
// Check if button is pressed to stop
if (digitalRead(button) == LOW) {
break;
}
}
}
2. Pictures & Videos
7. References
[1] Pixabay, MasterTux, “Castle, Combination lock, Security image. Free for use.“
https://pixabay.com/photos/castle-combination-lock-security-4282913