Learn

How to use a vibration sensor with an Arduino: a secret knock lock

Intermediate

Or see every lesson.

  • 455parts on the bench
  • 43boards running now
  • 1.00xreal time, on every board
Arduino Uno: a secret knock locklive0.000 s 0.00x
Click to open it in the editor
An SW-420 on pin 2 and a servo bolt on pin 9, on a simulated ATmega328P. Knock, knock, knock, pause, knock.

Wire VCC to 5 V, GND to GND and DO to a digital pin. DO sits LOW while all is still and pulses HIGH while the sensor shakes. Take the first HIGH as the knock, then ignore the pin for about 100 ms, because one knock is a burst of pulses, not one clean edge.

The circuit above is a secret knock lock. An SW-420 is taped to the door, a servo slides the bolt and two LEDs show the answer. Press the knock button on the sensor in the rhythm knock, knock, knock, pause, knock, and the serial monitor prints "knock 1" to "knock 4", then the three gaps it heard, then "unlocked". The servo opens for four seconds and prints "locked" again. Knock four times evenly instead and it prints "gap 100 (want 50)" and "wrong knock", and the red LED flashes.

What you need

  • An Arduino Uno
  • An SW-420 vibration sensor module
  • An SG90 servo (for the lock bolt)
  • A green LED, a red LED and two 220 ohm resistors
  • A breadboard and jumper wires

Wiring

Part and pinGoes toNote
SW-420 VCC5 VWorks from 3.3 V to 5 V
SW-420 GNDGND
SW-420 DOPin 2LOW when still, pulses HIGH on vibration
Servo signalPin 9Red to 5 V, brown to GND
Green LED, through 220 ohmPin 7Unlocked
Red LED, through 220 ohmPin 6Wrong knock

How the SW-420 works

The silver can on the board is a vibration switch: a contact inside that stays closed while the can is still. Knock on the table, or on the door it is taped to, and the contact bounces open and shut for a few tens of milliseconds. The board’s comparator turns that into DO: LOW while the contact is closed, HIGH each moment it is open.

That makes it the opposite of the flame sensor and the IR obstacle sensor, whose DO goes LOW when they see something. On the SW-420, HIGH is the event.

Why one knock counts as eight

The first sketch most people write counts every rising edge on DO. We built the one below and gave the sensor a single knock. It printed "edge 1" to "edge 8". Two knocks, half a second apart, reached "edge 17". The contact really does open and close that many times in one knock, so the sketch is right about the edges and wrong about the knocks.

Counting edges: one knock prints eight
const int VIBRATION = 2;

int edges = 0;
int last = LOW;

void setup() {
  pinMode(VIBRATION, INPUT);
  Serial.begin(9600);
}

void loop() {
  int now = digitalRead(VIBRATION);
  if (now == HIGH && last == LOW) {
    edges++;
    Serial.print("edge ");
    Serial.println(edges);
  }
  last = now;
}

Counting knocks properly

The fix is the same one a clap switch uses. Take the first HIGH as a knock, write down the time with millis(), and ignore DO until 100 ms have passed. With two knocks half a second apart, this sketch printed "listening", "knock 1" and "knock 2", and nothing else. Held shaking for a second, it printed "knock 1" to "knock 10", one every 100 ms, which is right for a sensor that never stops moving.

The knock lock above does exactly this, with DEBOUNCE_MS set to 100. It also waits for 1.5 seconds of quiet before it decides the rhythm is over.

One knock, one count
const int VIBRATION = 2;
const unsigned long QUIET_MS = 100;

int knocks = 0;
unsigned long lastKnock = 0;

void setup() {
  pinMode(VIBRATION, INPUT);
  Serial.begin(9600);
  Serial.println("listening");
}

void loop() {
  if (digitalRead(VIBRATION) == HIGH) {
    unsigned long now = millis();
    if (knocks == 0 || now - lastKnock > QUIET_MS) {
      knocks++;
      lastKnock = now;
      Serial.print("knock ");
      Serial.println(knocks);
    }
  }
}

How the lock hears a rhythm

The lock stores the time of every knock. When the knocking stops it works out each gap and scales them so the longest is 100. The secret is 50, 50, 100: two short gaps and one twice as long. Each gap may be off by 25, so you can knock fast or slow and it still opens, as long as the pattern is right.

Try it. Knock four times evenly and every gap scales to 100. The first one is too far from 50, so the lock prints "gap 100 (want 50)" and gives up. Change the SECRET array in the sketch to your own rhythm and press Run.

What the simulation leaves out

How hard you knock. A real switch opens wider on a hard knock and may not open at all on a gentle one, so a real sensor needs to be firmly taped down, close to where the knocks land. Here every press of knock is the same knock. The bounce pattern repeats exactly from run to run, where a real one never does.

Common mistakes

Counting edges as knocks. One knock is a burst. Take the first edge, then ignore DO for about 100 ms.

Reading it as active low. On the SW-420, DO goes HIGH on vibration.

Using delay() to wait out the burst. It works for one knock, but the sketch misses everything else while it waits. Use millis() as above.

Leaving the sensor loose. It has to be fixed to the thing that vibrates, or it hears nothing.

Using it to measure how strong a shake is. It is a switch, not an accelerometer. For a strength or a direction, use an MPU6050.

Questions

How do I connect an SW-420 vibration sensor to an Arduino?

VCC to 5 V, GND to GND and DO to a digital pin such as pin 2. Read it with digitalRead(): LOW means still, HIGH means it is vibrating.

Why does my vibration sensor count one knock many times?

The switch inside bounces open and shut for a few tens of milliseconds on every knock. In the simulator one knock gave eight rising edges. Count the first, then ignore the pin for about 100 ms.

Can the SW-420 measure how hard something shakes?

No. It only says whether it is vibrating. For the size or the direction of a movement, use an accelerometer such as the MPU6050.

What is the trimmer on the SW-420 for?

It sets the comparator’s threshold. Because the switch puts the comparator’s input at one rail or the other, the trimmer makes a difference only near the ends of its turn. Leave it in the middle.

Can I try an SW-420 vibration sensor in a simulator?

Yes. The knock lock at the top of this page runs in your browser: press the knock button on the sensor in the rhythm and the serial monitor prints "knock 1" to "knock 4", then the gaps it heard.

Build this for real

Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.