Bunker airlock interlock on an Arduino Uno
Two servo doors that are never open together: press OUTSIDE or INSIDE and the airlock opens one door, purges with a beeper, then opens the other. Teaches interlocks and state machines. The whole build, an Arduino Uno and 7 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Intermediate About 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $34. On Mokxi they're free.
What you'll learn
- Motors
- Lights
- Sound
- Moving a servo to an angle
- Reading a button
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 10
How it works
Two servo doors that are never open together: press OUTSIDE or INSIDE and the airlock opens one door, purges with a beeper, then opens the other. Teaches interlocks and state machines.
Arduino Uno R3 runs the sketch below. The circuit around it: Hobby servo, Traffic light module, Piezo speaker, Pushbutton. pinMode sets RED_PIN[d] as an output. digitalRead reads whether BUTTON[s] is HIGH or LOW. Serial.begin opens the serial port at 115200 baud. tone plays a note on PIEZO. millis reads the time since power-up, so the sketch can time things without stopping.
Parts list
10 parts, plus the jumper wires. They retail for about $34; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 2 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
- 2 × Power, 5 V
- 2 × Traffic light module
- 1 × Piezo speakerIn Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 2 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Traffic light module (2) pin G | Arduino Uno R3 pin 13 |
| Traffic light module (2) pin R | Arduino Uno R3 pin 12 |
| Hobby servo (2) pin PWM | Arduino Uno R3 pin 11 |
| Piezo speaker pin 1 | Arduino Uno R3 pin 8 |
| Traffic light module (2) pin Y | Arduino Uno R3 pin 7 |
| Traffic light module (1) pin Y | Arduino Uno R3 pin 6 |
| Hobby servo (1) pin PWM | Arduino Uno R3 pin 5 |
| Traffic light module (1) pin G | Arduino Uno R3 pin 3 |
| Traffic light module (1) pin R | Arduino Uno R3 pin 2 |
| Pushbutton (1) pin 1a | Arduino Uno R3 pin A0 |
| Pushbutton (2) pin 1a | Arduino Uno R3 pin A1 |
| Hobby servo (1) pin GND | Ground |
| Hobby servo (1) pin VCC | 5 V |
| Traffic light module (1) pin GND | Ground |
| Hobby servo (2) pin GND | Ground |
| Hobby servo (2) pin VCC | 5 V |
| Traffic light module (2) pin GND | Ground |
| Piezo speaker pin 2 | Ground |
| Pushbutton (1) pin 2b | Ground |
| Pushbutton (2) pin 2b | Ground |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Bunker airlock: two doors that are never open together. Two servos, two
// buttons, two lights and a piezo on an Uno.
//
// outer door servo -> pin 5, red lamp -> pin 2, green lamp -> pin 3, yellow -> pin 6
// inner door servo -> pin 11, red lamp -> pin 12, green lamp -> pin 13, yellow -> pin 7
// OUTSIDE button -> A0 to GND INSIDE button -> A1 to GND
// piezo pin 8 and GND
//
// Someone outside presses OUTSIDE: the outer door opens (green) for five
// seconds and closes. Then the airlock purges for four seconds, beeping, with
// both lamps yellow. Then the inner door opens for five seconds, and closes.
// Someone inside presses INSIDE and the same happens the other way round.
// Press either button while a cycle is running and it is ignored, with a note
// on the Serial Monitor.
//
// # An interlock is a rule that cannot be broken by accident
//
// The one rule is: a door may open only if the other door is closed. It is
// written once, in openDoor(), and every route to a door goes through that
// function. Nothing else in the sketch ever moves a servo to the open
// position, so there is no path, however the buttons are pressed, that opens
// both. Real airlocks, lift doors and microwave ovens work the same way.
//
// There is a second half: "closed" has to mean closed. A command to shut a
// door is not the door being shut, because a servo takes time to swing. So
// closing sets a timestamp, and the other door waits for SWING_MS to pass
// after it before it may open.
//
// # The cycle is a state machine
//
// IDLE, FIRST_OPEN, PURGE, SECOND_OPEN. Each state has a deadline; loop()
// looks at the clock and moves on when the deadline passes. No delay(), so a
// button press during a cycle is read (and refused) at once.
#include <Servo.h>
// door 0 is the outer door and door 1 the inner door; button 0 is OUTSIDE
const uint8_t SERVO_PIN[2] = {5, 11};
const uint8_t RED_PIN[2] = {2, 12};
const uint8_t GREEN_PIN[2] = {3, 13};
const uint8_t YELLOW_PIN[2] = {6, 7};
const uint8_t BUTTON[2] = {A0, A1}; // OUTSIDE, INSIDE
const uint8_t PIEZO = 8;
const char* const NAME[2] = {"outer", "inner"};
const int CLOSED_ANGLE = 0;
const int OPEN_ANGLE = 90;
const unsigned long OPEN_MS = 5000;
const unsigned long PURGE_MS = 4000;
const unsigned long SWING_MS = 800;
enum State { IDLE, FIRST_OPEN, PURGE, SECOND_OPEN };
Servo door[2];
bool isOpen[2];
unsigned long closedAt[2];
State state = IDLE;
bool purging = false; // both lamps yellow while the air is changed
uint8_t first; // the door the visitor comes through first
unsigned long deadline = 0;
unsigned long lastBeep = 0;
bool wasDown[2];
void lamps() {
for (int d = 0; d < 2; d++) {
digitalWrite(GREEN_PIN[d], isOpen[d]);
digitalWrite(YELLOW_PIN[d], purging && !isOpen[d]);
digitalWrite(RED_PIN[d], !isOpen[d] && !purging);
}
}
// The only function that opens a door, and the only place the rule is kept.
bool openDoor(uint8_t which) {
uint8_t other = 1 - which;
if (isOpen[other] || millis() - closedAt[other] < SWING_MS) {
Serial.println("interlock: refused, the other door is not shut yet");
return false;
}
isOpen[which] = true;
door[which].write(OPEN_ANGLE);
lamps();
Serial.print(NAME[which]);
Serial.println(" door open");
return true;
}
void closeDoor(uint8_t which) {
isOpen[which] = false;
closedAt[which] = millis();
door[which].write(CLOSED_ANGLE);
lamps();
Serial.print(NAME[which]);
Serial.println(" door closed");
}
void press(uint8_t side) {
if (state != IDLE) {
Serial.println("busy: wait for the cycle to finish");
return;
}
first = side;
if (!openDoor(first)) return;
state = FIRST_OPEN;
deadline = millis() + OPEN_MS;
}
void setup() {
Serial.begin(115200);
for (int d = 0; d < 2; d++) {
pinMode(RED_PIN[d], OUTPUT);
pinMode(GREEN_PIN[d], OUTPUT);
pinMode(YELLOW_PIN[d], OUTPUT);
pinMode(BUTTON[d], INPUT_PULLUP);
door[d].attach(SERVO_PIN[d]);
door[d].write(CLOSED_ANGLE);
closedAt[d] = 0UL - SWING_MS; // shut long ago, as far as the clock knows
}
pinMode(PIEZO, OUTPUT);
lamps();
Serial.println("airlock idle: press OUTSIDE or INSIDE");
}
void loop() {
unsigned long now = millis();
for (int s = 0; s < 2; s++) {
bool down = digitalRead(BUTTON[s]) == LOW;
if (down && !wasDown[s]) press(s);
wasDown[s] = down;
}
if (state == FIRST_OPEN && now >= deadline) {
closeDoor(first);
state = PURGE;
purging = true;
lamps();
deadline = now + PURGE_MS;
Serial.println("purging");
} else if (state == PURGE) {
if (now - lastBeep >= 500) {
lastBeep = now;
tone(PIEZO, 440, 100);
}
// the second door waits for the purge and for the first door to be shut
if (now >= deadline) purging = false;
if (now >= deadline && openDoor(1 - first)) {
state = SECOND_OPEN;
deadline = now + OPEN_MS;
}
} else if (state == SECOND_OPEN && now >= deadline) {
closeDoor(1 - first);
state = IDLE;
Serial.println("airlock idle: press OUTSIDE or INSIDE");
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(115200);
for (int d = 0; d < 2; d++) {
pinMode(RED_PIN[d], OUTPUT);
pinMode(GREEN_PIN[d], OUTPUT);
pinMode(YELLOW_PIN[d], OUTPUT);
pinMode(BUTTON[d], INPUT_PULLUP);
door[d].attach(SERVO_PIN[d]);
door[d].write(CLOSED_ANGLE);
closedAt[d] = 0UL - SWING_MS; // shut long ago, as far as the clock knows
}
pinMode(PIEZO, OUTPUT);
lamps();
Serial.println("airlock idle: press OUTSIDE or INSIDE");
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
unsigned long now = millis();
for (int s = 0; s < 2; s++) {
bool down = digitalRead(BUTTON[s]) == LOW;
if (down && !wasDown[s]) press(s);
wasDown[s] = down;
}
if (state == FIRST_OPEN && now >= deadline) {
closeDoor(first);
state = PURGE;
purging = true;
lamps();
deadline = now + PURGE_MS;
Serial.println("purging");
} else if (state == PURGE) {
if (now - lastBeep >= 500) {
lastBeep = now;
tone(PIEZO, 440, 100);
}
// the second door waits for the purge and for the first door to be shut
if (now >= deadline) purging = false;
if (now >= deadline && openDoor(1 - first)) {
state = SECOND_OPEN;
deadline = now + OPEN_MS;
}
} else if (state == SECOND_OPEN && now >= deadline) {
// ... 5 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The servo jitters, twitches or resets the board when it moves. Hobby servo
Power it from its own 5 V supply, not the Arduino 5 V pin, and join the grounds. A hobby servo can pull several hundred milliamps. Add a 100 uF to 470 uF capacitor across its supply. - The servo hums at the end of its travel or will not reach full angle. Hobby servo
Stay inside the pulse range, about 1000 to 2000 us for most hobby servos. Pushing past the end stop makes it strain. Calibrate the min and max pulse for your servo. - One color never lights, or all three light together. Traffic light module
Check each wire goes to its own pin labeled R, Y, G, and that GND is connected. A pin driven high lights its LED. Resistors are already on the board, so do not add more. - Lights seem dim or the wrong color comes on in a sequence. Traffic light module
Confirm the pins in your code match the wires, and that the module gets the right supply voltage. Keep each state's delay long enough to see, and turn the old color off before the next. - A passive piezo stays silent when you just set the pin HIGH. Piezo speaker
A passive piezo needs a changing signal. Use tone(pin, frequency) or a PWM wave. Around 2 to 4 kHz is loudest. A buzzer with a built in oscillator makes sound from plain DC instead. - Sound is very quiet or the pin seems overloaded. Piezo speaker
Drive the piezo from a pin directly, or through a 100 ohm resistor. For a bigger buzzer, switch it with a transistor. Check that you wired the right two legs and joined grounds. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.