CNAP
03 / Templates · beginner

Read 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
Circuit
Code
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);
}
Components
  • Arduino Uno
    01
  • Tilt Switch
    02
Wiring
Component PinArduino Pin
Tilt Switch VCC5V
Tilt Switch GNDGND
Tilt Switch OUT (Signal)Pin 2