Arduino Tilt Switch KY-020 code

Hello world! I made another example. Today for the KY-020 Arduino Tilt Switch.
We are simply going to switch on a relay to turn the lamp on and off.

Ky-020, Arduino Tilt Switch

Parts list:
Arduino Uno, I used the RobotDyn Uno.
KY-020 Tilt Switch.
KY-019 1 Channel relay.
Jumper wires MM/FF/MF
Lamp Socket + lamp & power Cable. (Optional)


Arduino Tilt Switch.
Author: Danny van den Brande, BlueCore Tech.
In this example we switch on a light with a relay and the KY-020 Tilt Switch
int Relay = 2;
int TiltSwitch = 3; 
int val ;
void setup ()
  pinMode (Relay, OUTPUT);
  pinMode (TiltSwitch, INPUT);
void loop ()
  val = digitalRead (TiltSwitch);
    if (val == HIGH) 
    digitalWrite (Relay, HIGH);
    Serial.print ("Value: ");//You can remove the serial print section.
    Serial.println (val);
    digitalWrite (Relay, LOW);
    Serial.print ("Value: ");//You can remove the serial print section.
    Serial.println (val);

What is a tilt switch?
A tilt sensor can measure the tilting in often two axes of a reference plane in two axes. In contrast, a full motion would use at least three axes and often additional sensors. One way to measure tilt angle with reference to the earth’s ground plane, is to use an accelerometer. Typical applications can be found in the industry and in game controllers.

Nintendo used tilt sensor technology in five games for its Game Boy series of hand-held game systems. The tilt sensor allows players to control aspects of the game by twisting the game system. Games that use this feature:
Yoshi’s Universal Gravitation (Game Boy Advance)
WarioWare: Twisted! (Game Boy Advance)(not released in Europe)
Koro Koro Puzzle Happy Panechu! (Game Boy Advance)(Japan only)
Kirby Tilt ‘n’ Tumble (Game Boy Color)(not released in Europe)
Command Master (Game Boy Color)(Japan only)

Tilt sensors can also be found in game controllers such as the Microsoft Sidewinder Freestyle Pro and Sony’s PlayStation 3 controller.

Danny van den Brande

Programmer, Game Designer, 3D Designer, Web Designer, Graphic Designer. Jup this website looks crappy.. :) But enjoy the projects! Everything you need is here.

Leave a Reply

Your email address will not be published. Required fields are marked *