Built-in project · Arduino Uno R3

Tilt hourglass on an Arduino Uno

Six LEDs light one by one on a millis() timer, and tipping the tilt switch on pin 8 clears them and starts again. The whole build, an Arduino Uno and 14 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 $33. On Mokxi they're free.

Tilt hourglass · Arduino Uno R3live0.000 s 0.00x
Hold the button
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Timing without blocking using millis
  • Driving six LEDs with a loop over pin numbers
  • Detecting a change in a tilt switch
  • Changing a constant to change the time scale
  • Difficulty: Beginner
  • Time: About 20 minutes
  • Board: Arduino Uno R3
  • Parts: 16

How it works

Six LEDs on pins 2 to 7 act as the sand. Every INTERVAL, 2 seconds here, the sketch lights the next LED. When all six are on, it prints that time is up.

A tilt switch on pin 8 with a 10k resistor to ground is the turn-over sensor. When its reading changes, the sketch turns every LED off and starts counting again. The sketch uses millis rather than delay, so it never stops watching the switch.

Parts list

16 parts, plus the jumper wires. They retail for about $33; 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 1Arduino Uno R3 pin GND
LED, yellow (1) pin CArduino Uno R3 pin GND
LED, yellow (2) pin CArduino Uno R3 pin GND
LED, yellow (3) pin CArduino Uno R3 pin GND
LED, yellow (4) pin CArduino Uno R3 pin GND
LED, yellow (5) pin CArduino Uno R3 pin GND
LED, yellow (6) pin CArduino Uno R3 pin GND
Tilt switch pin 2Arduino Uno R3 pin 8
Resistor, 10k Ω pin 2Arduino Uno R3 pin 8
Resistor, 220 Ω (6) pin 1Arduino Uno R3 pin 7
Resistor, 220 Ω (5) pin 1Arduino Uno R3 pin 6
Resistor, 220 Ω (4) pin 1Arduino Uno R3 pin 5
Resistor, 220 Ω (3) pin 1Arduino Uno R3 pin 4
Resistor, 220 Ω (2) pin 1Arduino Uno R3 pin 3
Resistor, 220 Ω (1) pin 1Arduino Uno R3 pin 2
Tilt switch pin 1Arduino Uno R3 pin 5V
LED, yellow (1) pin AResistor, 220 Ω (1) pin 2
LED, yellow (2) pin AResistor, 220 Ω (2) pin 2
LED, yellow (3) pin AResistor, 220 Ω (3) pin 2
LED, yellow (4) pin AResistor, 220 Ω (4) pin 2
LED, yellow (5) pin AResistor, 220 Ω (5) pin 2
LED, yellow (6) pin AResistor, 220 Ω (6) 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 hourglass: six LEDs light one at a time as the time runs, and turning
// the hourglass over starts it again.
//
//   pins 2 to 7   the six LEDs, each through 220 ohm to GND
//   pin 8         the tilt switch: one leg to 5V, the other to pin 8 and
//                 through a 10k resistor to GND
//
// The sketch never stops to wait. It notes the time of the last LED with
// millis() and checks on every pass whether INTERVAL has gone by since, so
// it can keep watching the tilt switch the whole time. delay() would leave it
// deaf for the whole interval.
//
// INTERVAL is two seconds here so you can watch it run. Set it to 600000 for
// ten minutes a light, an hour for all six.

const unsigned long INTERVAL = 2000;
const int FIRST_LED = 2;
const int LED_COUNT = 6;
const int TILT = 8;

unsigned long lastTime = 0;
int lit = 0;
int lastTilt = -1;

void setup() {
  for (int i = 0; i < LED_COUNT; i++) pinMode(FIRST_LED + i, OUTPUT);
  pinMode(TILT, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt hourglass");
}

void loop() {
  unsigned long now = millis();
  if (lit < LED_COUNT && now - lastTime >= INTERVAL) {
    lastTime = now;
    digitalWrite(FIRST_LED + lit, HIGH);
    lit++;
    Serial.print("lights on: ");
    Serial.println(lit);
    if (lit == LED_COUNT) Serial.println("time is up");
  }

  int tilt = digitalRead(TILT);
  if (lastTilt >= 0 && tilt != lastTilt) {
    for (int i = 0; i < LED_COUNT; i++) digitalWrite(FIRST_LED + i, LOW);
    lit = 0;
    lastTime = now;
    Serial.println("turned over: starting again");
  }
  lastTilt = tilt;
}

Code walkthrough

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

setup sketch.ino

Sets pins 2 to 7 as outputs with a loop, makes pin 8 an input for the tilt switch, and opens the serial monitor. The LEDs all start off.

void setup() {
  for (int i = 0; i < LED_COUNT; i++) pinMode(FIRST_LED + i, OUTPUT);
  pinMode(TILT, INPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: tilt hourglass");
}

loop sketch.ino

Does two jobs each pass. First, if fewer than six lights are on and INTERVAL has passed, it lights the next one. Second, it reads the tilt switch. If the reading changed since last time, it turns all LEDs off and restarts the timer.

void loop() {
  unsigned long now = millis();
  if (lit < LED_COUNT && now - lastTime >= INTERVAL) {
    lastTime = now;
    digitalWrite(FIRST_LED + lit, HIGH);
    lit++;
    Serial.print("lights on: ");
    Serial.println(lit);
    if (lit == LED_COUNT) Serial.println("time is up");
  }

  int tilt = digitalRead(TILT);
  if (lastTilt >= 0 && tilt != lastTilt) {
    for (int i = 0; i < LED_COUNT; i++) digitalWrite(FIRST_LED + i, LOW);
    lit = 0;
    lastTime = now;
    Serial.println("turned over: starting again");
  }
  lastTilt = tilt;
}

Troubleshooting

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

  • The hourglass restarts on its own This project
    A loose tilt switch wire floats the pin. Check the 10k resistor from pin 8 to GND.
  • It runs too fast to be an hourglass This project
    INTERVAL is 2000 ms so you can watch it. Raise it, for example to 600000 for ten minutes per light.
  • One LED never lights This project
    Check that LED's 220 ohm resistor and that its cathode goes to 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.

Change it and keep it

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