A tilt switch contains a metallic ball inside that will commute the two pins of the device from on to off if the sensor reaches a certain angle.

Connect one leg of the tilt switch to 5 volts and the other to digital pin 2 and a 10kΩ pull-down resistor.