Build it step by step
0 of 7 doneBuild the rail and panel
It should slide by hand.
Fit the motor and string
Pull the panel both ways.
Mount the limit switches
At each end of travel.
Wire the L298N and Uno
Remove the ENA jumper.
Test the motor alone
Short bursts each way.
Add the PIR and upload
Wait for it to settle.
Tune the speed and time-out
Adjust for your door.
How do you build an automatic sliding door model?
A PIR sensor opens a cardboard sliding door with a geared motor and limit switches, like a supermarket door. You need Arduino Uno R3, PIR Motion Sensor HC-SR501, L298N Dual H-Bridge Module, BO motor straight 100 RPM, Limit switch (micro switch with lever) × 2, Battery holder 4 × AA with switch and AA alkaline battery × 4. It takes 7 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 7
Wiring
| From | To | Why |
|---|---|---|
| PIR OUT | Uno D2 | Motion signal |
| PIR VCC and GND | Uno 5V and GND | Power |
| Open limit switch | Uno D3 and GND | Door fully open (internal pull-up) |
| Closed limit switch | Uno D4 and GND | Door fully closed (internal pull-up) |
| L298N IN1 and IN2 | Uno D5 and D6 | Motor direction |
| L298N ENA | Uno D9 | Motor speed (remove the ENA jumper) |
| Motor | L298N OUT1 and OUT2 | Door drive |
| 4xAA holder plus | L298N 12V terminal | Motor power |
| Battery minus | L298N GND and Uno GND | Common ground |
Code
This is a small model of a shop door: a sliding panel of card or foam board on a rail, pulled by a string or a rack by a small geared motor. Two micro switches tell the Uno when the door is fully open or fully closed, so the motor never pushes against the end. The sketch is a four-state machine: closed, opening, open and waiting, and closing. If the PIR sees movement while the door is closing, it reverses at once, just as a real door does. A time-out stops the motor if a limit switch is never reached, which protects the motor and the 4xAA pack when a switch is misplaced or the string jams. Run the motor at moderate speed so a hand caught in the model is not hurt, keep fingers clear of the string and pulley, and never use this idea on a real door that a person or pet could be trapped in. Remove the L298N's ENA jumper so the Uno can set speed. The PIR takes about thirty seconds to settle after power-up. Arduino IDE, board "Arduino Uno".
Runs on a virtual Arduino Uno in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
// Automatic sliding door model: opens on motion, waits, closes, reopens if motion returns while closing.
// Board: Arduino Uno. Limit switches wired to GND, so LOW means pressed.
const uint8_t PIR = 2, LIM_OPEN = 3, LIM_CLOSED = 4, IN1 = 5, IN2 = 6, ENA = 9;
const int SPEED = 180; // 0 to 255
const unsigned long WAIT_MS = 5000, TIMEOUT_MS = 8000;
enum State { CLOSED, OPENING, OPEN_WAIT, CLOSING };
State state = CLOSED;
unsigned long stamp = 0;
void motorStop() { digitalWrite(IN1, LOW); digitalWrite(IN2, LOW); analogWrite(ENA, 0); }
void motorOpen() { digitalWrite(IN1, HIGH); digitalWrite(IN2, LOW); analogWrite(ENA, SPEED); }
void motorClose() { digitalWrite(IN1, LOW); digitalWrite(IN2, HIGH); analogWrite(ENA, SPEED); }
void setup() {
pinMode(PIR, INPUT);
pinMode(LIM_OPEN, INPUT_PULLUP); pinMode(LIM_CLOSED, INPUT_PULLUP);
pinMode(IN1, OUTPUT); pinMode(IN2, OUTPUT); pinMode(ENA, OUTPUT);
motorStop();
delay(30000); // let the PIR settle after power-up
}
void loop() {
bool motion = digitalRead(PIR) == HIGH;
switch (state) {
case CLOSED:
if (motion) { motorOpen(); stamp = millis(); state = OPENING; }
break;
case OPENING:
if (digitalRead(LIM_OPEN) == LOW) { motorStop(); stamp = millis(); state = OPEN_WAIT; }
else if (millis() - stamp > TIMEOUT_MS) { motorStop(); state = OPEN_WAIT; stamp = millis(); }
break;
case OPEN_WAIT:
if (motion) stamp = millis(); // keep it open while someone is there
if (millis() - stamp > WAIT_MS) { motorClose(); stamp = millis(); state = CLOSING; }
break;
case CLOSING:
if (motion) { motorOpen(); stamp = millis(); state = OPENING; } // reopen if someone steps in
else if (digitalRead(LIM_CLOSED) == LOW) { motorStop(); state = CLOSED; }
else if (millis() - stamp > TIMEOUT_MS) { motorStop(); state = CLOSED; }
break;
}
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Wave at the PIR | Door opens and stops at the open switch |
| Stand still in front | Door stays open |
| Walk away | Closes after about five seconds |
| Wave while closing | Reverses and opens |
If it does not work
Motor runs the wrong way
Swap the two motor wires.
Door never stops
Check the limit switch wiring to GND.
Motor stutters
Weak batteries; use fresh cells.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the automatic sliding door model?
Arduino Uno R3, PIR Motion Sensor HC-SR501, L298N Dual H-Bridge Module, BO motor straight 100 RPM, Limit switch (micro switch with lever) × 2, Battery holder 4 × AA with switch and AA alkaline battery × 4.
How long does the automatic sliding door model take to build?
About 120 minutes for a beginner maker, in 7 steps.
How do I know the automatic sliding door model works?
Wave at the PIR: Door opens and stops at the open switch. Stand still in front: Door stays open. Walk away: Closes after about five seconds.
Motor runs the wrong way: what should I check?
Swap the two motor wires.
Door never stops: what should I check?
Check the limit switch wiring to GND.
Motor stutters: what should I check?
Weak batteries; use fresh cells.
