Want to try a small electronics project without complicated circuits? In this beginner tutorial, we’ll build detect tilt using arduino and tilt sensor. You’ll connect one sensor, upload a short Arduino IDE sketch, and see the result. No Wi-Fi account or cloud service is required.

What does it do?
A small metal ball connects or disconnects contacts when you tip the sensor. Arduino uses its internal pull-up resistor, so no external resistor is needed for a bare two-wire switch.
Parts you’ll need
Arduino Uno, 2-pin ball-switch tilt sensor or KY-020 module, jumper wires. You will also need a computer with the free Arduino IDE installed.
Wiring connections
Disconnect USB power before moving any wires. Make these connections carefully:
| Sensor / component pin | Connect to |
|---|---|
| One side of tilt switch | D2 |
| Other side of tilt switch | GND |
Important: ESP32 GPIO pins work at 3.3V. Do not connect a 5V output directly to an ESP32 input. Board and sensor-module pin labels vary, so double-check the markings on your hardware.
Arduino IDE code
Select the correct board and USB port in Arduino IDE, paste the complete sketch below, and click Upload.
const int tiltPin = 2;
void setup() {
Serial.begin(9600);
pinMode(tiltPin, INPUT_PULLUP);
}
void loop() {
int state = digitalRead(tiltPin);
Serial.println(state == LOW ? "Switch closed" : "Switch open");
delay(200);
}
Expected result
Tilt the switch: output changes between Switch open and Switch closed. Open Tools → Serial Monitor and set the baud rate shown in Serial.begin() in the sketch.
Troubleshooting
This example is wired for a bare 2-pin switch. For a 3-pin KY-020 breakout, connect its signal output to D2, plus VCC/GND as the board requires; its polarity may differ.
If nothing appears, first check the USB cable, correct board and port, sensor power and ground, and the Serial Monitor baud rate. Recheck every wire against the connection table before changing code.
What to try next
Once this simple example works, try changing the measurement interval or using the sensor reading to control an LED. This is an easy way to learn inputs, outputs, and basic programming.
For more hands-on electronics tutorials, explore CircuitMagic. If you later want to control Arduino or ESP32 projects from a phone over Bluetooth, see our BLE Controller app tutorial. This project itself does not need Bluetooth.









