Built-in project · Arduino Uno R3

Tilt switch on an Arduino Uno

A ball tilt switch on pin 2 with the internal pull-up: tip it over and the LED on pin 13 lights, with the ball’s bounce filtered out. The whole build, an Arduino Uno and 3 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 15 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • INPUT_PULLUP removes the need for a resistor
  • Debouncing with timing and millis()
  • Reading a tilt sensor
  • Tracking the last and stable states
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

A tilt switch is a ball in a tube. Standing upright the ball joins two contacts, and tipped over it rolls away. One leg goes to pin 2 and the other to ground, with the internal pull up turned on.

Upright, the pin is pulled to ground and reads LOW. Tipped, the contact opens and the pull up brings it HIGH. The LED on pin 13 lights while the switch is tipped.

A rolling ball bounces for a few milliseconds, so the sketch only accepts a new state once it holds for 50 ms.

Parts list

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

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
Tilt switch pin 1Arduino Uno R3 pin GND
LED, red pin CArduino Uno R3 pin GND
Resistor, 220 Ω pin 1Arduino Uno R3 pin 13
Tilt switch pin 2Arduino Uno R3 pin 2
LED, red pin AResistor, 220 Ω 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
// Tilt switch: a ball in a tube that closes a contact while it stands up.
//
//   pin 2   one leg of the tilt switch, the other leg to GND
//   pin 13  the LED, through 220 ohm to GND
//
// Standing upright, the ball rests on the two contacts and joins them, so pin
// 2 is pulled to ground and reads LOW. Tip it over and the ball rolls away,
// the contact opens, and the internal pull-up brings the pin back HIGH. The
// LED lights while the switch is tipped.
//
// A rolling ball bounces on the contacts for a few milliseconds, so the sketch
// only believes a new state once it has held for 50 ms.

const int TILT = 2;
const int LED = 13;
const unsigned long SETTLE_MS = 50;

int stable = LOW;
int last = LOW;
unsigned long changedAt = 0;

void setup() {
  pinMode(TILT, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt switch");
  stable = digitalRead(TILT);
  last = stable;
}

void loop() {
  int now = digitalRead(TILT);
  if (now != last) {
    last = now;
    changedAt = millis();
  }
  if (now != stable && millis() - changedAt >= SETTLE_MS) {
    stable = now;
    digitalWrite(LED, stable == HIGH ? HIGH : LOW);
    Serial.println(stable == HIGH ? "tipped over" : "upright");
  }
}

Code walkthrough

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

setup sketch.ino

Sets pin 2 to INPUT_PULLUP and pin 13 as an output, starts serial, and reads the starting state into stable and last.

void setup() {
  pinMode(TILT, INPUT_PULLUP);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt switch");
  stable = digitalRead(TILT);
  last = stable;
}

loop sketch.ino

Reads the pin. If it differs from the last reading, it records the time. If it differs from the stable state and has held for SETTLE_MS (50 ms), it accepts it, sets the LED and prints "tipped over" or "upright".

void loop() {
  int now = digitalRead(TILT);
  if (now != last) {
    last = now;
    changedAt = millis();
  }
  if (now != stable && millis() - changedAt >= SETTLE_MS) {
    stable = now;
    digitalWrite(LED, stable == HIGH ? HIGH : LOW);
    Serial.println(stable == HIGH ? "tipped over" : "upright");
  }
}

Troubleshooting

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

  • The LED flickers This project
    Keep the 50 ms settle time. The ball bounces on the contacts.
  • The LED works backward This project
    The LED lights when the pin reads HIGH. Check the tilt switch connects to pin 2 and ground.
  • The reading flickers when the switch is moved. Tilt switch
    The ball bounces on the contacts, giving many fast changes. Debounce in software by waiting 20 to 50 ms before accepting a new state, or average a few readings.
  • The input floats and never gives a clean reading. Tilt switch
    A tilt switch is just a switch, so the pin floats when it is open. Use INPUT_PULLUP with the switch between the pin and GND, or add a 10 k pull-down resistor.
  • 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 LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.
  • A blue or white LED stays dark on 3.3 V. LED
    Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED.

Change it and keep it

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