Introduction
In this tutorial, we will learn about the KY-004 module, what is a tactile switch and we will build a simple project to switch on an Arduino’s built-in LED using the KY-004.
Key Switch Module KY-004
The KY-004 Module will be our main component for this tutorial. This module has a tactile switch and mounted on a breakout board with a resistor. Figure 1 shows the module as seen in fritzing.
What is a Tactile Switch?
A tactile switch is a low profile switch generally installed on a PCB. It creates an electrical continuity between its terminals when the button is pressed.
Pinout
The KY-004 module has three pins.
Component Pin | Arduino Uno board Pin |
---|---|
(-) | GND |
middle | +5V |
S | Signal |
Project - Arduino LED Switch
After learning about the KY-004 module, it is now time to build a project using the module. Our project will use the KY-004 module to switch on/off the built-in LED of an Arduino.
Components
For this project, we need the following components:
- Arduino Uno board (1 pc.)
- KY-004 Key Switch Module (1 pc.)
- Jumper wires
Wiring Diagram
Figure 2 shows the connection between the Arduino Uno and the KY-004 Key Switch Module.
The KY-004 module pins are connected to the Arduino Uno board as follows:
Component Pin | Arduino Uno board Pin |
---|---|
(-) | GND |
middle | +5V |
S | 7 |
The Code
Below is the Arduino sketch for our project. I have added comments to explain important parts of the code. Save the code as KY-004.ino and upload it to your Arduino board.
// Arduino and KY-004 module
void setup() {
pinMode(7, INPUT_PULLUP); // KY-004 module is conected to pin 7
pinMode(13, OUTPUT); // built-in LED is connected to pin 13
}
void loop() {
if (digitalRead(7) == LOW) {
digitalWrite(13, HIGH); // if button is pressed, turn on LED
}
else digitalWrite(13, LOW); // if button is not pressed, turn off LED
}
Project Test
Apply power to your Arduino Uno board. The built-in LED of the Arduino will light up when the button on the KY-004 module is pressed and will turn off when the button is released.