Projects / Tilt switch parcel handling flag

Tilt switch parcel handling flag

Beginner50 minGuided buildReviewed

Build it step by step

0 of 6 done
  1. NANO

    Wire the tilt switch

    One pin to D2 and one to GND.

  2. NANO9.3 mA

    Wire the LED

    D6 through 220 ohms to the long leg, short leg to GND.

  3. NANO

    Wire the reset button

    D4 to GND through the button.

  4. NANO

    Upload the sketch

    A short blink every three seconds while upright.

  5. NANO9.4 mA

    Tip the box and hold it

    After about a third of a second the LED stays on.

  6. NANO9.4 mA

    Stand it up and press reset

    The LED returns to the heartbeat blink.

In short

How do you build a tilt switch parcel handling flag?

A ball tilt switch rides inside a parcel or crate and a Nano lights and holds a red LED once the box has been tipped past the switch's angle. It is a simple rough-handling indicator, not a measurement. You need Arduino Nano, Tilt sensor (ball switch, SW-520D), LED 5 mm red, Resistor 220 Ω, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm). It takes 6 steps and about 50 minutes.

Level
Beginner
Time
About 50 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Tilt switch pin 1Nano D2Input with the internal pull-up
Tilt switch pin 2Nano GNDCloses to ground when the box is upright
Red LED long legNano D6 through the 220 ohm resistorHandling flag
Red LED short legNano GNDCommon ground
Reset buttonNano D4 and GNDPress to clear the flag

Code

A ball tilt switch holds a small metal ball that rolls onto two contacts when the switch is upright and rolls away when it is tipped. Wired with the internal pull-up, the pin reads low while upright and high once the box tips. The sketch asks for the tipped state to last 300 milliseconds before it counts, so a knock does not set the flag, and then keeps the red LED lit until someone presses the reset button with the box upright. When upright and not flagged, the LED gives a short blink every three seconds so you know the unit is running. The trip angle depends on how the switch is mounted and on the individual part, so test your own box. This is a rough indicator for learning and it is not a precise or certified handling record.

Runs on a virtual Arduino Nano in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// Tilt switch parcel handling flag.
// The ball switch is closed (LOW) when upright and opens when the box is tipped over.
const uint8_t TILT_PIN = 2;
const uint8_t RESET_PIN = 4;
const uint8_t LED_PIN = 6;
const unsigned long TIP_MS = 300;       // must stay tipped this long, so a small bump is ignored

bool flagged = false;
unsigned long tippedSince = 0;

void setup() {
  pinMode(TILT_PIN, INPUT_PULLUP);
  pinMode(RESET_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  bool tipped = digitalRead(TILT_PIN) == HIGH;
  if (tipped) {
    if (tippedSince == 0) tippedSince = millis();
    if (millis() - tippedSince > TIP_MS) flagged = true;
  } else {
    tippedSince = 0;
  }

  if (digitalRead(RESET_PIN) == LOW && !tipped) flagged = false;   // reset only while upright

  if (flagged) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    // a short heartbeat blink shows the unit is alive and upright
    digitalWrite(LED_PIN, (millis() % 3000) < 40 ? HIGH : LOW);
  }
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
UprightShort blink every 3 seconds
Quick knockNo flag
Tip over and holdRed LED stays on
Press reset while tippedStill on
Press reset uprightFlag cleared

If it does not work

Flags with no tip

Check the switch is mounted upright and the wires are firm.

Never flags

Tip further, or turn the switch so the ball rolls away in your direction of tilt.

Heartbeat missing

Check the LED direction and the resistor.

Reset does nothing

It only works while the box is upright.

Flags on a bump

Raise TIP_MS.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the tilt switch parcel handling flag?

Arduino Nano, Tilt sensor (ball switch, SW-520D), LED 5 mm red, Resistor 220 Ω, Tactile push button 6 mm, Breadboard 830 Tie Points and Jumper wires male to male (40 pcs, 20 cm).

How long does the tilt switch parcel handling flag take to build?

About 50 minutes for a beginner maker, in 6 steps.

How do I know the tilt switch parcel handling flag works?

Upright: Short blink every 3 seconds. Quick knock: No flag. Tip over and hold: Red LED stays on.

Flags with no tip: what should I check?

Check the switch is mounted upright and the wires are firm.

Never flags: what should I check?

Tip further, or turn the switch so the ball rolls away in your direction of tilt.

Heartbeat missing: what should I check?

Check the LED direction and the resistor.

Reset does nothing: what should I check?

It only works while the box is upright.