Lesson 5 Tilt Switch


The tilt switch used here is a ball one with a metal ball inside. It is used to detect inclinations of a small angle.



Schematic Diagram


Experimental Procedures

Step 1: Build the circuit.


Step 2: Open the code file.

Step 3: Select the Board and Port.

Step 4: Upload the sketch to the board.

Now, tilt the switch, and the LED attached to pin 13 on Uno board will light up.



Code Analysis

Code Analysis 7-1 Whole Code

const int ledPin = 13; //the led attach to

void setup()


  pinMode(ledPin,OUTPUT); //initialize the ledPin as an output

  pinMode(2,INPUT); //set pin2 as INPUT

  digitalWrite(2, HIGH); //set pin2 as HIGH



void loop()


  int digitalVal = digitalRead(2); //Read the value of pin2

  if(HIGH == digitalVal) //if tilt switch is not breakover


     digitalWrite(ledPin,LOW); //turn the led off


  else //if tilt switch breakover


     digitalWrite(ledPin,HIGH); //turn the led on



The whole code are very simple, one pin of the tilt switch is connected to pin2, another pin is connected to GND, when tilt the switch, the two pins of the switch will be connected to GND, then let the LED on the pin13 lights up.