Combination lock: keypad, servo bolt and buzzer, on an Arduino Uno
Type 1234 on a keypad (rows 11 to 8, columns 7 to 4) and a servo on pin 12 draws the bolt for two seconds; each key clicks a buzzer on pin 13, and a wrong code sounds a long beep. The whole build, an Arduino Uno and 5 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 Runs in your browser. Free, and no account needed.
The parts retail for about $35. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A combination lock: the right four keys open the bolt.
//
// keypad rows 1..4 -> pins 11, 10, 9, 8 columns 1..4 -> pins 7, 6, 5, 4
// servo signal -> pin 12 red -> 5V brown -> GND
// pin 13 -> 1k -> an NPN transistor's base; the active buzzer from 5V
// to its collector; emitter -> GND
//
// The sketch keeps the last four keys. After the fourth it compares them
// with the code: right, and the servo turns to open for two seconds; wrong,
// and the buzzer sounds for half a second.
//
// Type 1, 2, 3, 4 on the keypad.
#include <Keypad.h>
const char KEYS[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte rowPins[4] = {11, 10, 9, 8};
byte colPins[4] = {7, 6, 5, 4};
Keypad keypad = Keypad(makeKeymap(KEYS), rowPins, colPins, 4, 4);
#include <Servo.h>
const int SERVO_PIN = 12;
const int BUZZER = 13;
const char CODE[4] = {'1', '2', '3', '4'};
Servo bolt;
char entered[4];
byte count = 0;
void beep(int ms) {
digitalWrite(BUZZER, HIGH);
delay(ms);
digitalWrite(BUZZER, LOW);
}
void setup() {
bolt.attach(SERVO_PIN);
bolt.write(45);
pinMode(BUZZER, OUTPUT);
Serial.begin(9600);
Serial.println("Mokxi Uno: type the code on the keypad");
}
void loop() {
char key = keypad.getKey();
if (!key) return;
beep(100);
entered[count++] = key;
if (count < 4) return;
bool right = true;
for (int i = 0; i < 4; i++) {
if (entered[i] != CODE[i]) right = false;
}
if (right) {
Serial.println("right code: open");
bolt.write(135);
delay(2000);
bolt.write(45);
} else {
Serial.println("wrong code");
beep(500);
}
count = 0;
}
Parts list
7 parts, plus the jumper wires. They retail for about $35; on Mokxi they're free.
- 1 × Arduino Uno R3
- 1 × Half breadboard
- 1 × Keypad, 4x4
- 1 × Resistor, 1k Ω
- 1 × Transistor (NPN or PNP)
- 1 × Buzzer
- 1 × Hobby servo
How it is wired
14 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 pin GND; Transistor (NPN or PNP) pin E; Hobby servo pin GND
- Arduino Uno R3 pin 13; Resistor, 1k Ω pin 1
- Arduino Uno R3 pin 12; Hobby servo pin PWM
- Arduino Uno R3 pin 11; Keypad, 4x4 pin R1
- Arduino Uno R3 pin 10; Keypad, 4x4 pin R2
- Arduino Uno R3 pin 9; Keypad, 4x4 pin R3
- Arduino Uno R3 pin 8; Keypad, 4x4 pin R4
- Arduino Uno R3 pin 7; Keypad, 4x4 pin C1
- Arduino Uno R3 pin 6; Keypad, 4x4 pin C2
- Arduino Uno R3 pin 5; Keypad, 4x4 pin C3
- Arduino Uno R3 pin 4; Keypad, 4x4 pin C4
- Arduino Uno R3 pin 5V; Buzzer pin 1; Hobby servo pin VCC
- Resistor, 1k Ω pin 2; Transistor (NPN or PNP) pin B
- Transistor (NPN or PNP) pin C; Buzzer pin 2
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.