Home Arduino KY-004 Key Switch Module

KY-004 Key Switch Module

by iainhendry

 

The KY-004 Key Switch Module is a push button that will output a high signal when pressed.

 

Connection

Connect the Power line (middle) and ground (-) to +5 and GND. Connect signal (s) to pin 3 on the Arduino for this example

KY-004 Arduino
S Pin 3
middle +5V
GND

 

Code

[codesyntax lang=”cpp”]

int led = 13; //Define the LED pin
int buttonpin = 3; //Define the push button pin
int val; //Define a numeric variable

void setup()
{
	pinMode(led,OUTPUT);
	pinMode(buttonpin,INPUT);
}

void loop()
{
	val = digitalRead(buttonpin); // check the state of the button
	if(val==HIGH) // if button is pressed, turn LED on
	{
		digitalWrite(led,HIGH);
	}
	else
	{
		digitalWrite(led,LOW);
	}
}

[/codesyntax]

 

Links

37 in 1 box Sensor Kit For Arduino Starters brand in stock

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