03 / Templates · beginner
TILT SWITCH
Open in PlaygroundRead a ball-and-tube tilt switch to detect orientation changes, printing the state to the Serial Monitor. The simulator alternates the switch on a timer since it has no clickable tilt control.
beginnerswitchdigital inputserialtilt
sketch.inoArduino C++
20 lines
// Tilt Switch — Orientation Detector
// Prints whether the switch is level or tilted.
// Note: this simulator's tilt switch has no clickable orientation control,
// so it alternates automatically on a timer to demonstrate the wiring and
// code — on real hardware it reacts to how you physically tilt the board.
const int TILT_PIN = 2;
void setup() {
pinMode(TILT_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
bool tilted = digitalRead(TILT_PIN) == HIGH;
Serial.println(tilted ? "TILTED!" : "level");
delay(200);
}- Arduino Uno01
- Tilt Switch02
| Component Pin | Arduino Pin |
|---|---|
| Tilt Switch VCC | 5V |
| Tilt Switch GND | GND |
| Tilt Switch OUT (Signal) | Pin 2 |