---
title: "PROJECT 34: AUTOMATIC DOORS LOCK"
description: "## **[Introduction](http://Introduction)** By utilizing an ultrasonic sensor to detect the proximity of a person, it is possible to automate the process of opening and locking a door. This can be achieved by measuring the distance of an approaching object (e.g.,…"
url: "https://nedu.rw/documentation/project-34-automatic-doors-lock/"
updated: "2026-10-13"
category: InnoKit MPK A100 / Experiments you can do with MPK A100
---

# PROJECT 34: AUTOMATIC DOORS LOCK

## **[Introduction](http://Introduction)**

By utilizing an ultrasonic sensor to detect the proximity of a person, it is possible to automate the process of opening and locking a door. This can be achieved by measuring the distance of an approaching object (e.g., a person) and activating a motor through a relay when the object is within a specified threshold. If the object moves away or is beyond the set distance, the motor disengages, ensuring the door remains locked. This project is particularly useful in homes, offices, or other secure areas where touchless and automated access control is desired, combining convenience with safety.

## **[Objectives](http://Objectives)**

- To detect the presence of a person by using ultrasonic sensor 
- To to use relay in the way of controlling the motor

## **[Components to be used](http://Components to be used)**

- MPK A100 Kit
- Ultrasonic sensor
- 12V power adapter
- DC power jack
- Jumper wires
- Motor (Servo motor)

| ![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXc8N5bMsNHlfoq5liFMN7gdx-wKCvi2gaxWaP24nMfn-J1ADbyHAZT4b_C-GlajcqGMFbx9SWc36arLiYBmlrZPhMxTu_ggSWCQN4On6HKTYJco6Glii6fxxQzDjdhwriNf1eeaFTA1s1eFnA0TA6BYd1Gg3lwD6hffN3YBXZNtPuJ9gBo3rvc?key=EIFaBMwRIuJeDoZo9taXbw) | ![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdi1I4TrjAGPk3WuZtAy03QRzV0WDgCfHph_20apIiGa9NOeisZiGD7BdTyMdJt45GR7ELb7p6MZHElcr3uRpvMVNHQJ_iMpFHgz8g5XbSUQpAV0TBkb7EOOTCmolug9naZrOtvYy6-j1COL23PeiU-rKfVcDs0C12xuRoAIHryUPIXwgjxKy0?key=EIFaBMwRIuJeDoZo9taXbw) |
| --- | --- |
| ![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXc9zCg0LEQJjTZZNxdu5SZfQq5yfbXGY8kryeIhEKK3RKpBAMFGF7av5VLPgJjJ33P2pOPAwlTVMIWZhnAFtjwr47joP3UcYtTvCzZiZRA9DQRCNRhxWYfLCLiQ9KOlFVRiFVpuqOQY-CCNUuzQlITK9KwIuBdz7BfuDnNsp0kxbciamgteZA?key=EIFaBMwRIuJeDoZo9taXbw) |  |
| ![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdP0lJblWzlIRKdxE49L9IQFvSNkmjIQoyI_g4u0w_PfAiGK9pt2UwsaL7bzsTtE30qgmh9mxZTqg-yRemE2n42BvNJ2f1edANWMhJZVK_4NWv8zxhC8vGwFqyvQiEuaJHb05j70Xcm0odGYBas4jEyUKAsBwOonE97pcI3zGKJKy_r9UtoCnw?key=EIFaBMwRIuJeDoZo9taXbw) |  |

1. **Circuit diagram**

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXfAKpSayVvJvWUfLKbXU6EAIVzKRBTn6pbEp5drhvq73PsACpbl6LW09j1ivbV65UjaYtXyPAIr2FICf1GzFvtKqhgnhT6Rrg7wAr_rp_Nq2vVxYpksJ2GEGROapSUEl-PasBjuQX1j4sQpUmfxrT4?key=EIFaBMwRIuJeDoZo9taXbw)

***Figure: Schematic diagram for the Ultrasonic sensor and relay connection***

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdJB99KxXxL4h5II1PDAJU07Y0rkjJCaCaD4rwm6fmnLd_2ZT8bLxbRpS6a4Z2kH2FE2EQM-thDwIkJ755bmtDx1oDFAO_5XLFxEIAt244iXc1ezAHJ-uhgG_jXAAVn7N-9Zwqyv_Vtlv_Hg4wyZg?key=EIFaBMwRIuJeDoZo9taXbw)

***Figure: Components used to make connection on the board***

1. **Arduino codes**

    1 2 3 4 5 6 7 8 9
 10 11 12 13 14 15 16 17 18 19 20 21 22 2324 25 26 27 28 29 30 31 32    #define TRIG_PIN 4 // The Arduino Nano pin connected to TRIG pin of ultrasonic sensor #define ECHO_PIN 3 // The Arduino Nano pin connected to ECHO pin of ultrasonic sensor #define RELAY_PIN 2 // The Arduino Nano pin connected to relay #define DISTANCE_THRESHOLD 50 // in centimeters float duration_us, distance_cm; voidsetup() {  Serial.begin(9600);// Initialize the Serial to communicate with the Serial Monitor.  pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode  pinMode(TRIG_PIN, INPUT); // set arduino pin to input mode  pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode } voidloop() { // Produce a 10-microsecond pulse to the TRIG pin.  digitalWrite(TRIG_PIN, HIGH);  delayMicroseconds(10);  digitalWrite(TRIG_PIN, LOW); // Measure the pulse duration from the ECHO pin  duration_us =  pulseIn(ECHO_PIN,  HIGH); // calculate the distance  distance_cm = 0.017 \* duration_us;  if(distance_cm  digitalWrite(RELAY_PIN, HIGH); // turn on Relay  else(distance_cm  digitalWrite(RELAY_PIN, LOW); // turn off Relay // print the value to Serial Monitor  Serial.print(RELAY_PIN, LOW("distance: ");  Serial.print(RELAY_PIN, LOW(distance_cm);  Serial.print(RELAY_PIN, LOW(" cm");  delay(RELAY_PIN, LOW(500); }

1. **Code knowledge**

    6   #define DISTANCE_THRESHOLD 50 // in centimeters

- DISTANCE_THRESHOLD defines the maximum distance, in centimeters, at which the relay should be turned on. Here, if the distance is less than 50 cm, the relay will turn on.

    7   float duration_us, distance_cm;

**duration_us:** This variable stores the time, in microseconds, that it takes for the ultrasonic pulse to travel to and from an object.

**distance_cm:** This stores the calculated distance from the ultrasonic sensor to the object in centimeters.

- ***Measure the Echo Duration***

    19 20   // Measure the pulse duration from the ECHO pin  duration_us =  pulseIn(ECHO_PIN,  HIGH);

**pulseIn(ECHO_PIN, HIGH);:**This command measures the duration of the high signal received by the ECHO pin in microseconds, which corresponds to the time taken by the ultrasonic wave to travel to the object and back.

- ***Calculation of the Distance***

    21 22   // calculate the distance  distance_cm = 0.017 \* duration_us; // calculate the distance

**distance_cm = 0.017 \* duration_us;:** This converts the duration in microseconds to distance in centimeters. The factor 0.017 is derived from the speed of sound in air, assuming the wave travels at approximately 340 m/s (or 0.034 cm/µs). Dividing by 2 is implicit since pulseIn() measures the round trip.

- ***Control the Relay Based on the Distance***

    23 24 25 26     if(distance_cm  digitalWrite(RELAY_PIN, HIGH); // turn on Relay  else(distance_cm  digitalWrite(RELAY_PIN, LOW); // turn off Relay

**if (distance_cm Checks if the measured distance is less than the threshold distance (50 cm). If the condition is true, it sets RELAY_PIN high (5V), turning on the relay. Otherwise, it sets RELAY_PIN low (0V), turning off the relay.**

- ***Display the Distance on the Serial Monitor***

    27 28 29 30    // print the value to Serial Monitor  Serial.print("distance: ");  Serial.print(distance_cm);  Serial.print(" cm");

**Serial.print** and **Serial.println** display the measured distance in centimeters on the Serial Monitor, which helps you observe and verify the distance readings.

- ***Add a Delay***

    31    delay(500);

**delay(500);:**Pauses the program for 500 milliseconds before the next reading. This prevents the code from running too quickly and gives you time to observe each measurement.

1. **Results**

Expected Outputs:

The expected outputs relate to the behavior of the *relay* (which controls the door mechanism) and the feedback on the *Serial Monitor*.

1. ***Visible Output (Door Mechanism)***

- When an object is within the threshold distance (less than 50 cm): The relay will be activated, which is used to power a motor to unlock or open the door. The door remains open as long as the object is within the threshold.
- When the object moves away (distance greater than or equal to 50 cm): The relay is deactivated, stopping the motor and locking or closing the door.

***The Serial Monitor*** provides a real-time measurement of the distance detected by the ultrasonic sensor in centimeters.
