Tilt switch parcel handling flag
Build it step by step
0 of 6 doneWire the tilt switch
One pin to D2 and one to GND.
Wire the LED
D6 through 220 ohms to the long leg, short leg to GND.
Wire the reset button
D4 to GND through the button.
Upload the sketch
A short blink every three seconds while upright.
Tip the box and hold it
After about a third of a second the LED stays on.
Stand it up and press reset
The LED returns to the heartbeat blink.
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
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
- SwitchesBuy
- PrototypingBuy
- PrototypingBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Tilt switch pin 1 | Nano D2 | Input with the internal pull-up |
| Tilt switch pin 2 | Nano GND | Closes to ground when the box is upright |
| Red LED long leg | Nano D6 through the 220 ohm resistor | Handling flag |
| Red LED short leg | Nano GND | Common ground |
| Reset button | Nano D4 and GND | Press 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.
| Check | Expected |
|---|---|
| Upright | Short blink every 3 seconds |
| Quick knock | No flag |
| Tip over and hold | Red LED stays on |
| Press reset while tipped | Still on |
| Press reset upright | Flag 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 →
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.
