Built-in project · Arduino Uno R3

Vibration sensor on pin 3, read by an interrupt, on an Arduino Uno

A vibration sensor module on pin 3, watched by an interrupt: every shake is caught the instant it happens, and the LED on pin 13 lights for half a second. The whole build, an Arduino Uno and 1 more part, 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.

Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $31. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Interrupts with attachInterrupt
  • Keeping interrupt functions short
  • Why a shared counter is marked volatile
  • Detecting short events you could miss by polling
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The SW-420 vibration module puts a signal on its DO pin when it is shaken. DO goes to pin 3, which is an interrupt pin on the Uno. An interrupt lets the chip stop what it is doing the instant the pin changes, so a short knock is never missed.

The interrupt function only adds one to a counter. The main loop checks the counter. When it sees knocks, it clears the counter, prints "vibration!", lights the LED on pin 13 and holds it for half a second. Otherwise the LED stays off. Click the sensor in the simulator to knock it.

Parts list

3 parts, plus the jumper wires. They retail for about $31; on Mokxi they're free.

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
Vibration sensor, SW-420 pin GNDArduino Uno R3 pin GND
Vibration sensor, SW-420 pin DOArduino Uno R3 pin 3
Vibration sensor, SW-420 pin VCCArduino Uno R3 pin 5V

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
// A vibration sensor read by an interrupt.
//
//   sensor DO -> pin 3   VCC -> 5V   GND -> GND
//
// Pin 3 is interrupt 1 on the Uno. attachInterrupt() runs shaken() the
// moment the pin changes, whatever loop() is doing, so a short knock is never
// missed. shaken() only counts; loop() lights the LED.
//
// Click the sensor to knock it.

const int SENSOR = 3;
const int LED_PIN = 13;
volatile unsigned int knocks = 0;

void shaken() {
  knocks++;
}

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(SENSOR), shaken, CHANGE);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: vibration sensor on pin 3");
}

void loop() {
  if (knocks > 0) {
    knocks = 0;
    Serial.println("vibration!");
    digitalWrite(LED_PIN, HIGH);
    delay(500);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Code walkthrough

The key functions of the sketch, and what each one does.

shaken sketch.ino

The interrupt function. It runs the moment pin 3 changes, whatever loop is doing, and it only does knocks++. Counting is quick, which is exactly what an interrupt function should do.

void shaken() {
  knocks++;
}

setup sketch.ino

Makes pin 3 an input and pin 13 an output. attachInterrupt(digitalPinToInterrupt(SENSOR), shaken, CHANGE) tells the chip to call shaken on every change of pin 3. Then it starts serial at 9600 baud.

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(SENSOR), shaken, CHANGE);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: vibration sensor on pin 3");
}

loop sketch.ino

If knocks is above zero, it resets it, prints "vibration!", lights the LED and waits 500 ms. If not, it turns the LED off. The work happens here, outside the interrupt.

void loop() {
  if (knocks > 0) {
    knocks = 0;
    Serial.println("vibration!");
    digitalWrite(LED_PIN, HIGH);
    delay(500);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The count or LED never reacts This project
    Interrupts only work on certain pins. On the Uno, pin 3 is one of them. Check DO goes to pin 3.
  • It reads the same all the time, or false triggers. A pot sets the comparator threshold and the sensor is a very sensitive switch. Vibration sensor
    Turn the pot until the LED just goes off at rest, then a tap trips it. Read the digital output DO, and count one edge per knock instead of a level.
  • The output is wired to a 5 V logic level on a 3.3 V board, or power is missing. Vibration sensor
    Power the module from 3.3 V or 5 V to match your board's logic level. Join VCC and GND, then connect DO to a digital input. Use an interrupt for fast knocks.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.