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 About 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $40. On Mokxi they're free.
What you'll learn
- Reading a force sensor with a voltage divider
- Detecting a rising edge to count events
- A two-state lock: locked and unlocked
- Moving a servo to fixed angles
- Using LEDs to show state
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: Arduino Uno R3
- Parts: 13
How it works
The lock starts unlocked, with the green LED on and the servo bolt at 0 degrees. Press the button on pin 2 and it locks: the bolt turns to 90 degrees and the red LED lights.
While locked, the sketch reads a force sensor on A0. A reading over 100 counts as a knock, and each knock flashes the yellow LED. After three knocks, the lock opens again.
It counts a knock only when the reading rises past the level, not while it stays high, so one long press is one knock.
Parts list
13 parts, plus the jumper wires. They retail for about $40; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Force-sensing resistor
- 2 × Resistor, 10k ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Hobby servoIn Halloween Prop Kit, Christmas Lights Kit, Starter Projects Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab Kit.
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, yellowIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, greenIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
Kits that can build it
This starter kit has every part in the circuit. Each kit's page lists what else it can build.
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 |
|---|---|
| Resistor, 10k Ω (1) pin 2 | Arduino Uno R3 pin GND |
| Hobby servo pin GND | Arduino Uno R3 pin GND |
| Resistor, 10k Ω (2) pin 1 | Arduino Uno R3 pin GND |
| LED, yellow pin C | Arduino Uno R3 pin GND |
| LED, green pin C | Arduino Uno R3 pin GND |
| LED, red pin C | Arduino Uno R3 pin GND |
| Hobby servo pin PWM | Arduino Uno R3 pin 9 |
| Resistor, 220 Ω (3) pin 1 | Arduino Uno R3 pin 5 |
| Resistor, 220 Ω (2) pin 1 | Arduino Uno R3 pin 4 |
| Resistor, 220 Ω (1) pin 1 | Arduino Uno R3 pin 3 |
| Pushbutton pin 2a | Arduino Uno R3 pin 2 |
| Force-sensing resistor pin 1 | Arduino Uno R3 pin 5V |
| Hobby servo pin VCC | Arduino Uno R3 pin 5V |
| Pushbutton pin 1a | Arduino Uno R3 pin 5V |
| Force-sensing resistor pin 2 | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω (1) pin 1 | Arduino Uno R3 pin A0 |
| Resistor, 10k Ω (2) pin 2 | Pushbutton pin 2b |
| LED, yellow pin A | Resistor, 220 Ω (1) pin 2 |
| LED, green pin A | Resistor, 220 Ω (2) pin 2 |
| LED, red pin A | Resistor, 220 Ω (3) pin 2 |
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);
}
Code walkthrough
The key functions of the sketch, and what each one does.
lock sketch.ino
Marks the lock as locked, resets the knock count, turns the bolt to 90 degrees, switches the green LED off and the red LED on, and prints "locked".
void lock() {
locked = true;
knocks = 0;
bolt.write(90);
digitalWrite(GREEN, LOW);
digitalWrite(RED, HIGH);
Serial.println("locked");
}unlock sketch.ino
The opposite: bolt back to 0 degrees, green on, red off. The sketch calls it at the end of setup so it starts open.
void unlock() {
locked = false;
bolt.write(0);
digitalWrite(GREEN, HIGH);
digitalWrite(RED, LOW);
Serial.println("unlocked");
}loop sketch.ino
When unlocked, it only watches for the button. When locked, it checks whether the sensor reading is over KNOCK_LEVEL. On a new rise it adds a knock, flashes the yellow LED for 50 ms and unlocks at KNOCKS_TO_OPEN. wasPressed stops one long press counting many times.
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);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Knocks are missed or too easy to trigger This project
RetuneKNOCK_LEVEL, now 100. Lower it to be more sensitive, raise it to need a harder press. - The button never locks it This project
This button reads HIGH when pressed. It has 5 V on one side and a 10k resistor to ground on pin 2. - The analog reading is always near 0 or noisy. Force-sensing resistor
An FSR changes resistance, so build a divider. Put about 10k from the analog pin to ground and the FSR to 5 V. Without the fixed resistor the pin floats. - The force reading is not accurate in grams. Force-sensing resistor
FSRs are not precise and vary part to part. Use them to tell light from hard presses, and calibrate against known weights if you need numbers. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.