Built-in project · Arduino Uno R3

Tilt switch on A5 lights the LED on pin 8, on an Arduino Uno

A tilt ball switch from 5 V to A5, with 220 ohm down to ground: analogRead sees about 1023 while it stands upright, and the LED on pin 8 lights. 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 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

  • A tilt switch is a ball in a tube
  • Reading a switch with analogRead
  • A pull down resistor sets the open state
  • Using a threshold to decide yes or no
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Arduino Uno R3
  • Parts: 6

How it works

A tilt switch holds a small metal ball. Upright, the ball joins two contacts, and tipped over it rolls away. Here the switch connects 5 V to A5, and a 220 ohm resistor pulls A5 down to ground.

Upright, A5 sits at 5 V and analogRead gives about 1023. Tipped, the resistor pulls it to 0. The sketch counts anything above 1000 as closed and lights the LED on pin 8.

An analog pin can read a switch just like a digital pin; it just gives a number. The sketch prints a message only when the state changes.

Parts list

6 parts, plus the jumper wires. They retail for about $31; 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, 220 Ω (1) pin 2Arduino Uno R3 pin GND
LED, red pin CArduino Uno R3 pin GND
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 8
Tilt switch pin 1Arduino Uno R3 pin 5V
Tilt switch pin 2Arduino Uno R3 pin A5
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin A5
LED, red pin AResistor, 220 Ω (2) 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 ball switch read on an analog pin: upright, the LED on pin 8 lights.
//
//   5V -> tilt switch -> A5 -> 220 ohm -> GND
//   pin 8 -> 220 ohm -> LED -> GND
//
// Inside the switch a metal ball rests on two contacts. Upright, the ball
// joins them and A5 sits at 5 V, so analogRead() gives about 1023. Tip it
// and the ball rolls away: the resistor pulls A5 down to 0. Anything over
// 1000 counts as closed.
//
// An analog pin reads a switch as well as a digital one does; this one just
// gives a number instead of HIGH or LOW. Click the switch to tip it.

const int TILT_PIN = A5;
const int LED_PIN = 8;

bool wasClosed = false;
bool first = true;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt switch on A5, LED on pin 8");
}

void loop() {
  int reading = analogRead(TILT_PIN);
  bool closed = reading > 1000;
  digitalWrite(LED_PIN, closed ? HIGH : LOW);
  if (first || closed != wasClosed) {
    first = false;
    wasClosed = closed;
    Serial.print(closed ? "upright, A5 reads " : "tipped, A5 reads ");
    Serial.println(reading);
  }
  delay(20);
}

Code walkthrough

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

setup sketch.ino

Makes pin 8 an output for the LED and starts the serial port. A5 does not need setup for analogRead.

void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt switch on A5, LED on pin 8");
}

loop sketch.ino

Reads A5 and treats a value over 1000 as closed. It sets the LED to match, and prints "upright" or "tipped" with the reading when the state changes or on the first pass. The 20 ms delay keeps it steady.

void loop() {
  int reading = analogRead(TILT_PIN);
  bool closed = reading > 1000;
  digitalWrite(LED_PIN, closed ? HIGH : LOW);
  if (first || closed != wasClosed) {
    first = false;
    wasClosed = closed;
    Serial.print(closed ? "upright, A5 reads " : "tipped, A5 reads ");
    Serial.println(reading);
  }
  delay(20);
}

Troubleshooting

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

  • The LED is always off This project
    Check that the switch connects 5 V to A5 and the 220 ohm resistor connects A5 to ground.
  • The reading jumps around This project
    The resistor to ground is needed so the open pin does not float.
  • 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.