Knock lock on the book’s pins, on an Arduino Uno
A servo bolt on pin 9, a lock button on pin 2 and LEDs on pins 3, 4 and 5. Three presses of the knock sensor on A0 open the lock again. The whole build, an Arduino Uno and 11 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 code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Knock lock: three knocks open it, the button locks it again.
//
// knock sensor a force sensor from 5V to A0, and 10k from A0 to GND
// button pin 2, with 10k to GND (pressed reads HIGH)
// yellow LED pin 3, through 220 ohm: flashes for every knock heard
// green LED pin 4, through 220 ohm: unlocked
// red LED pin 5, through 220 ohm: locked
// servo signal on pin 9: the bolt, 0 degrees open, 90 locked
//
// On the bench the knock sensor is a piezo disc on A0 with a 1 M resistor
// across it: a knock bends the disc and it makes a small voltage. Mokxi's
// piezo only plays sound, so a force sensor stands in for it here, on the
// same pin and with the same code: press it once for each knock.
//
// The sketch starts unlocked. Press the button to lock: the bolt turns and
// the red LED lights. Then knock three times and the lock opens again.
#include <Servo.h>
const int SENSOR = A0;
const int BUTTON = 2;
const int YELLOW = 3;
const int GREEN = 4;
const int RED = 5;
const int KNOCK_LEVEL = 100; // a reading over this counts as a knock
const int KNOCKS_TO_OPEN = 3;
Servo bolt;
bool locked = false;
int knocks = 0;
bool wasPressed = false;
void lock() {
locked = true;
knocks = 0;
bolt.write(90);
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
Serial.println("locked");
}
void unlock() {
locked = false;
bolt.write(0);
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
Serial.println("unlocked");
}
void setup() {
pinMode(BUTTON, INPUT);
pinMode(YELLOW, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
bolt.attach(9);
Serial.begin(9600);
Serial.println("Mokxi Uno: knock lock. Press the button to lock, knock three times to open.");
unlock();
}
void loop() {
if (!locked) {
if (digitalRead(BUTTON) == HIGH) lock();
return;
}
bool pressed = analogRead(SENSOR) > KNOCK_LEVEL;
if (pressed && !wasPressed) {
knocks++;
digitalWrite(YELLOW, HIGH);
Serial.print("knock ");
Serial.println(knocks);
delay(50);
digitalWrite(YELLOW, LOW);
if (knocks >= KNOCKS_TO_OPEN) unlock();
}
wasPressed = pressed;
delay(10);
}
Parts list
13 parts, plus the jumper wires. Every one is in the editor's parts bin.
- 1 × Arduino Uno R3
- 1 × Full-size breadboard
- 1 × Force-sensing resistor
- 2 × Resistor, 10k Ω
- 1 × Hobby servo
- 1 × Pushbutton
- 3 × Resistor, 220 Ω
- 1 × LED, yellow
- 1 × LED, green
- 1 × LED, red
How it is wired
12 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; Resistor, 10k Ω (1) pin 2; Hobby servo pin GND; Resistor, 10k Ω (2) pin 1; LED, yellow pin C; LED, green pin C; LED, red pin C
- Arduino Uno R3 pin 9; Hobby servo pin PWM
- Arduino Uno R3 pin 5; Resistor, 220 Ω (3) pin 1
- Arduino Uno R3 pin 4; Resistor, 220 Ω (2) pin 1
- Arduino Uno R3 pin 3; Resistor, 220 Ω (1) pin 1
- Arduino Uno R3 pin 2; Pushbutton pin 2a
- Arduino Uno R3 pin 5V; Force-sensing resistor pin 1; Hobby servo pin VCC; Pushbutton pin 1a
- Arduino Uno R3 pin A0; Force-sensing resistor pin 2; Resistor, 10k Ω (1) pin 1
- Pushbutton pin 2b; Resistor, 10k Ω (2) pin 2
- Resistor, 220 Ω (1) pin 2; LED, yellow pin A
- Resistor, 220 Ω (2) pin 2; LED, green pin A
- Resistor, 220 Ω (3) pin 2; LED, red pin A
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.