Automatic door: Arduino project with wiring and code
Use the ultrasonic sensor (TRIG 7, ECHO 6) and the servo on pin 9 as a door. When something is closer than 30 cm, open the door (90°). Close it (0°) only after nothing has been near for 1 second.
You will learn: Combining a sensor and an actuator, and timing with millis() so the door does not slam shut too early.
Project 21 of 62 in the project gallery.
Parts you need
| Part | How many | What it does in this project |
|---|---|---|
| Arduino Uno | 1 | Runs your sketch and powers the circuit. |
| Ultrasonic distance sensor (HC-SR04) | 1 | The sensor. It measures how far away the nearest object is. |
| Servo motor | 1 | The door motor. It swings to 90° to open when something is closer than 30 cm and back to 0° to close. |
| Breadboard and jumper wires | 1 | Hold the parts and join them without soldering. |
How to wire it
Put the parts on the breadboard, join the board's 5V and GND pins to the breadboard's power rails, then make these connections:
| Part | Connection |
|---|---|
| Ultrasonic distance sensor | Trig → pin 7, Echo → pin 6 |
| Servo motor | signal → pin 9 |
In the Studio, the 💡 guide rings show where each wire goes, and the ✔ Check button tells you which connection is missing.
Starter code
Start from this sketch and write the missing lines. The comments say what each part has to do.
#include <Servo.h>
const int TRIG = 7;
const int ECHO = 6;
Servo door;
long lastSeen = 0; // when something was last near (ms)
long readCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long us = pulseIn(ECHO, HIGH);
return us / 58;
}
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
door.attach(9);
door.write(0);
}
void loop() {
long cm = readCm();
// if cm < 30: open the door (90) and remember the time: lastSeen = millis();
// else if millis() - lastSeen > 1000: close the door (0)
delay(50);
}
Build it step by step
- Open the project. Press Build it in the Studio. The parts are already in the tray.
- Place and wire the parts. Follow the wiring table above, or switch on the guide rings.
- Write the code. Complete the starter sketch in the Code panel, in text or with blocks.
- Run it. Press ▶ Run and use the part's slider, button or switch to test it. The Serial Monitor shows what your sketch prints.
- Check it. Press ✔ Check: the simulator tests the circuit and the code and tells you exactly what to fix.
Questions students ask
What does the Automatic door project do?
Use the ultrasonic sensor (TRIG 7, ECHO 6) and the servo on pin 9 as a door. When something is closer than 30 cm, open the door (90°). Close it (0°) only after nothing has been near for 1 second.
What parts do I need for the Automatic door project?
An Arduino Uno, ultrasonic distance sensor, servo motor, a breadboard and jumper wires.
How do I wire the Automatic door project?
Ultrasonic distance sensor: Trig → pin 7, Echo → pin 6; Servo motor: signal → pin 9. Power comes from the board's 5V (or 3.3V) and GND pins along the breadboard rails.
What will I learn from the Automatic door project?
Combining a sensor and an actuator, and timing with millis() so the door does not slam shut too early.
Can I build this without real hardware?
Yes. The project runs in the free Science Tinker microcontroller simulator in your browser: place the parts, wire them, write the sketch and press Run. The ✔ Check button tests your circuit and code and tells you what to fix. Nothing to install and no account needed.
Where is the finished code?
The page gives the starter sketch so that you write the important lines yourself. If you are stuck, the 💡 Solution button in the Studio shows a complete, working sketch that you can run and study.