Danny van den Brande
Published © CC BY-SA

Arduino - Tilt Switch, Switching On A RELAY With The KY-020

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

BeginnerProtip1 hour1,637
Arduino - Tilt Switch, Switching On A RELAY With The KY-020

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
KY-020 Tilt Switch. .
×1
KY-019 1 Channel relay
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Lamp Socket + lamp & power Cable
×1

Story

Read more

Schematics

Schematic

Code

KY-020_Tilt_Switch_With_RELAY.ino

Arduino
In this example we switch on a light with a relay and the KY-020 Tilt Switch
/*
Author: Danny van den Brande, Arduinosensors.nl. 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);
  Serial.begin(9600);
}
void loop ()
{
  val = digitalRead (TiltSwitch);
    if (val == HIGH) 
  {
    digitalWrite (Relay, HIGH);
    Serial.print ("Value: ");//You can remove the serial print section.
    Serial.println (val);
    delay(100);
  }
  else
  {
    digitalWrite (Relay, LOW);
    Serial.print ("Value: ");//You can remove the serial print section.
    Serial.println (val);
    delay(100);
  }
}

Credits

Danny van den Brande
36 projects • 110 followers
"Hello world."

Comments