Built-in project · Arduino Uno R3

Tilt switch on pin 3 lights the LED on 13, on an Arduino Uno

A tilt switch from 5 V to pin 3 with a 10k pull-down: the LED on pin 13 follows it, lit while the switch is closed. The whole build, an Arduino Uno and 2 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.

Beginner About 10 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

  • How a tilt switch works
  • A pull-down resistor
  • Reading a digital input
  • Printing only on a change of state
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 4

How it works

The tilt switch has a small ball inside. When the switch stands up, the ball rolls onto the contacts and closes the circuit from 5V to pin 3. When it is tipped, the ball rolls off and the circuit opens. A 10k resistor from pin 3 to GND holds the pin LOW while the switch is open.

The sketch reads pin 3 every 10 ms and copies it to the LED on pin 13. It prints closed or open only when the state changes. Click the tilt switch to tip it.

Parts list

4 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
Resistor, 10k Ω pin 2Arduino Uno R3 pin GND
Tilt switch pin 2Arduino Uno R3 pin 3
Resistor, 10k Ω pin 1Arduino Uno R3 pin 3
Tilt switch pin 1Arduino 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
// Tilt switch on pin 3 lights the LED on 13.
//
//   tilt switch: 5V -> pin 3, and 10k from pin 3 to GND
//
// A ball inside the switch rolls onto its contacts when it stands up, and off them when it is tipped.
//
// Click the tilt switch to tip it.

const int SENSOR = 3;
const int LED_PIN = 13;
int last = -1;

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt switch on pin 3, the LED on pin 13");
}

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "closed" : "open");
  }
  delay(10);
}

Code walkthrough

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

setup sketch.ino

Makes SENSOR (pin 3) an input and LED_PIN (pin 13) an output. It starts the serial port at 9600 baud and prints a hello message.

void setup() {
  pinMode(SENSOR, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt switch on pin 3, the LED on pin 13");
}

loop sketch.ino

Sets now to 1 when pin 3 reads HIGH, meaning the switch is closed, and writes that to the LED. When now differs from last, it prints closed or open. A 10 ms delay ends each pass.

void loop() {
  int now = digitalRead(SENSOR) == HIGH ? 1 : 0;
  digitalWrite(LED_PIN, now ? HIGH : LOW);
  if (now != last) {
    last = now;
    Serial.println(now ? "closed" : "open");
  }
  delay(10);
}

Troubleshooting

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

  • The LED flickers or acts randomly. This project
    The 10k resistor from pin 3 to GND is missing or loose. Without it the pin floats while the switch is open.
  • The LED is on when the switch is tipped. This project
    Closed means HIGH, because the switch connects to 5V. Which way is closed depends on how the switch sits.
  • 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.
  • 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.