Arduino Combination Lock
Build a working combination lock using Arduino with push buttons and a servo to lock/unlock.
- Arduino
- Intermediate
- 60 minutes
- 4 steps
Shop this project
CartYou'll need
Prices from vilros.com. Tax and shipping at checkout.
Before you start, you'll need
- Arduino Uno
- 4-6 push buttons
- Servo motor
- LED (green and red)
- Resistors (220Ω for LEDs)
- Breadboard
- Jumper wires
Add servo and status LEDs
About 10 minutes
Connect lock mechanism and indicators.
- Connect servo: signal to Pin 9, VCC to 5V, GND to GND.
- Connect green LED with 220Ω resistor to Pin 12 (unlocked indicator).
- Connect red LED with 220Ω resistor to Pin 13 (locked indicator).
You should see
Servo and LEDs are wired.
Program the combination lock
About 25 minutes
Create code to check button combinations.
- Open Arduino IDE.
- Copy the combination lock sketch below.
- Set your secret combination in the code.
- Upload to Arduino.
#include <Servo.h>
Servo lockServo;
const int button1 = 2;
const int button2 = 3;
const int button3 = 4;
const int button4 = 5;
const int greenLED = 12;
const int redLED = 13;
const int correctCombo[] = {1, 3, 2, 4};
int currentCombo[4];
int comboIndex = 0;
boolean locked = true;
void setup() {
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
lockServo.attach(9);
lockServo.write(0);
digitalWrite(redLED, HIGH);
digitalWrite(greenLED, LOW);
Serial.begin(9600);
Serial.println("Combination Lock Ready");
}
void loop() {
if (digitalRead(button1) == LOW) {
addToCombo(1);
delay(300);
}
if (digitalRead(button2) == LOW) {
addToCombo(2);
delay(300);
}
if (digitalRead(button3) == LOW) {
addToCombo(3);
delay(300);
}
if (digitalRead(button4) == LOW) {
addToCombo(4);
delay(300);
}
}
void addToCombo(int button) {
currentCombo[comboIndex] = button;
comboIndex++;
Serial.print("Button pressed: ");
Serial.println(button);
if (comboIndex >= 4) {
checkCombo();
comboIndex = 0;
}
}
void checkCombo() {
boolean match = true;
for (int i = 0; i < 4; i++) {
if (currentCombo[i] != correctCombo[i]) {
match = false;
break;
}
}
if (match) {
unlock();
} else {
Serial.println("Wrong combination!");
blinkRed();
}
}
void unlock() {
Serial.println("Correct! Unlocking...");
lockServo.write(90);
digitalWrite(redLED, LOW);
digitalWrite(greenLED, HIGH);
delay(3000);
lock();
}
void lock() {
Serial.println("Locking...");
lockServo.write(0);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
}
void blinkRed() {
for (int i = 0; i < 3; i++) {
digitalWrite(redLED, LOW);
delay(100);
digitalWrite(redLED, HIGH);
delay(100);
}
}Change correctCombo array to set your own combination (e.g., {2, 4, 1, 3}).
You should see
Code uploads successfully.
Test the combination lock
About 10 minutes
Verify correct and incorrect combinations.
- Open Serial Monitor to see button presses.
- Try wrong combinations—red LED blinks.
- Enter correct combination (default: 1, 3, 2, 4).
- Green LED turns on, servo rotates 90°, then locks after 3 seconds.
You should see
Lock opens with correct combination and rejects incorrect ones.
It didn't work
Servo doesn't move.
Check servo wiring. Verify servo is connected to Pin 9 and powered at 5V. Test servo independently with a simple sweep sketch.
What to do next
Stuck? Contact Vilros Support with your board model and what you see on screen.