Built-in project · Arduino Uno R3

Vibration alarm: a spring switch on pin 3, a buzzer on 13, on an Arduino Uno

A spring vibration switch from pin 3 to ground fires an interrupt the moment it is shaken, and an active buzzer on pin 13, switched by an NPN transistor, chirps for each shake. The whole build, an Arduino Uno and 4 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.

Beginner About 20 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 that catch short events
  • The INPUT_PULLUP mode for switches
  • Flags set in an interrupt and read in the loop
  • Switching a buzzer with a transistor
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

A spring vibration switch is wired from pin 3 to ground. Pin 3 uses the Uno's internal pull-up, so it reads high until the switch closes during a shake.

Pin 3 is an interrupt pin. The chip calls shaken the instant the pin changes, whatever the loop is doing, so a short knock is never missed. shaken only sets a flag.

The loop sees the flag, prints "vibration!" and sets pin 13 high for 50 ms. That drives a transistor through a 1k resistor, and the transistor switches the buzzer on from 5V.

Parts list

6 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
Transistor (NPN or PNP) pin EArduino Uno R3 pin GND
Spring vibration switch, SW-18010P pin 2Arduino Uno R3 pin GND
Resistor, 1k Ω pin 1Arduino Uno R3 pin 13
Spring vibration switch, SW-18010P pin 1Arduino Uno R3 pin 3
Buzzer pin 1Arduino Uno R3 pin 5V
Transistor (NPN or PNP) pin BResistor, 1k Ω pin 2
Buzzer pin 2Transistor (NPN or PNP) pin C

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 alarm: a shake fires an interrupt and the buzzer chirps.
//
//   SW-18010P spring switch: pin 3 -> switch -> GND   (INPUT_PULLUP is its pull-up)
//   pin 13 -> 1k -> an NPN transistor's base; the active buzzer from 5V
//   to its collector; emitter -> GND
//
// Pin 3 is one of the Uno's two interrupt pins. attachInterrupt() asks the
// chip to call shaken() the instant the pin changes, whatever loop() is
// doing, so even a short knock is never missed. shaken() only sets a flag;
// loop() sees it and sounds the buzzer.
//
// Press knock on the switch.

const int BUZZER = 13;
const int SENSOR = 3;

volatile bool knocked = false;

void shaken() {
  knocked = true;
}

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

void loop() {
  if (knocked) {
    knocked = false;
    Serial.println("vibration!");
    digitalWrite(BUZZER, HIGH);
    delay(50);
  } else {
    digitalWrite(BUZZER, LOW);
  }
}

Code walkthrough

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

shaken sketch.ino

It is the interrupt handler and it does one thing: it sets the knocked flag to true. It stays this short so it finishes fast. The flag is marked volatile because an interrupt changes it behind the loop's back.

void shaken() {
  knocked = true;
}

setup sketch.ino

It sets the buzzer pin as an output and the sensor pin as INPUT_PULLUP. Then attachInterrupt makes shaken run whenever pin 3 changes. It also starts the serial port.

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

loop sketch.ino

If knocked is set, it clears the flag, prints a message, and sets the buzzer pin high for 50 ms. Otherwise it sets the buzzer pin low. So each knock gives a short chirp.

void loop() {
  if (knocked) {
    knocked = false;
    Serial.println("vibration!");
    digitalWrite(BUZZER, HIGH);
    delay(50);
  } else {
    digitalWrite(BUZZER, LOW);
  }
}

Troubleshooting

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

  • The buzzer never sounds. This project
    Check the transistor: the collector goes to the buzzer, the emitter to GND, and the base to pin 13 through the 1k resistor.
  • Nothing happens on a knock. This project
    The switch must connect pin 3 to GND. Pin 3 is one of the Uno's two interrupt pins, so it cannot be moved to just any pin.
  • 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 load never turns on, or is always on, because the legs are in the wrong order. Transistor (NPN or PNP)
    Check the pinout in the datasheet for your part. A 2N2222 or BC547 can have emitter, base, collector in a different order. Flat side orientation is easy to get wrong.
  • The base is wired straight to the pin with no resistor, or the motor has no flyback diode. Transistor (NPN or PNP)
    Put about 1k between the pin and the base. For motors and relays, add a diode across the coil with the stripe toward the positive side.
  • The buzzer makes a constant tone, or you cannot change its pitch. Buzzer
    An active buzzer has its own oscillator, so it only beeps on and off. A passive buzzer needs tone() to set pitch. Check which type you have.
  • The buzzer is silent or very quiet. Buzzer
    Check polarity: the plus mark goes to the pin or supply. For loud buzzers, drive them through a transistor instead of straight from a pin.

Change it and keep it

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