Built-in project · Arduino Uno R3

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.

Knock lock on the book’s pins · Arduino Uno R3live0.000 s 0.00x
Press the button to lock, then press the knock sensor three times
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

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.

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 pinConnects to
Resistor, 10k Ω (1) pin 2Arduino Uno R3 pin GND
Hobby servo pin GNDArduino Uno R3 pin GND
Resistor, 10k Ω (2) pin 1Arduino Uno R3 pin GND
LED, yellow pin CArduino Uno R3 pin GND
LED, green pin CArduino Uno R3 pin GND
LED, red pin CArduino Uno R3 pin GND
Hobby servo pin PWMArduino Uno R3 pin 9
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 5
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 4
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 3
Pushbutton pin 2aArduino Uno R3 pin 2
Force-sensing resistor pin 1Arduino Uno R3 pin 5V
Hobby servo pin VCCArduino Uno R3 pin 5V
Pushbutton pin 1aArduino Uno R3 pin 5V
Force-sensing resistor pin 2Arduino Uno R3 pin A0
Resistor, 10k Ω (1) pin 1Arduino Uno R3 pin A0
Resistor, 10k Ω (2) pin 2Pushbutton pin 2b
LED, yellow pin AResistor, 220 Ω (1) pin 2
LED, green pin AResistor, 220 Ω (2) pin 2
LED, red pin AResistor, 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.

sketch.ino · Arduino Uno R3
// 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
    Retune KNOCK_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.