Home SensorsKit Arduino and KY-020 Tilt switch

Arduino and KY-020 Tilt switch

by shedboy71

In this article, we connect an KY-020 Tilt switch to an Arduino Uno

The KY-023 is a very basic tilt switch

Depending on the inclination of the module, a switch closes the input pins briefly. This occurs due to the fact that there is a ball inside the switch which short-circuits a contact, depending on the position of the module.

Parts Required

You can connect to the module using dupont style jumper wire.

This should work with other Arduino board – I have tried an Uno and Mega

Name   Link
Arduino Uno
37 in one sensor kit
Connecting cables

 

Schematic/Connection

Connect the module’s Power pin (middle) and ground pin (-) to +5v and GND.

Connect the signal (S) pin to pin 2 on the Arduino.

 

 

Code Example

The following code example will switch the onboard LED the Arduino when the module inclination degree changes. Tilt the module to turn the LED on/off.

int Led = 13 ;// define LED Interface
int tiltpin = 2; // define the tilt switch pin
int val ;// define numeric variables val

void setup ()
{
  pinMode (Led, OUTPUT) ;// define LED as output interface
  pinMode (tiltpin, INPUT) ;//define the output interface tilt switch sensor
}

void loop ()
{
  val = digitalRead (tiltpin) ;// read the pin
  if (val == HIGH) //When the tilt sensor detects a signal when the switch, LED flashes 
  {
    digitalWrite (Led, HIGH);
  }
  else
  {
    digitalWrite (Led, LOW);
  }
}

 

Serial Monitor Output

N/A

Links

https://github.com/getelectronics/ArduinoCode/tree/main/37%20Sensor%20Kit/KY-020_Tilt_Switch

 

 

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More