Switch Module(KY-004)

아두위키 : Arduwiki


Overview

The KY-004 switch module is a simple switch input module compatible with Arduino boards. It can be connected to an Arduino board to receive switch input.

Since it functions almost identically to a Tact Switch, for details related to switches such as floating phenomena, pull-up, and pull-down resistors, please refer to the Tact Switch document.


Specifications

  • Operating Voltage: 3.3V ~ 5V
  • Switch Output Level: LOW (0V) / HIGH (VCC)


Application Example

1. Checking Switch Input via Serial Monitor

This example demonstrates how to check whether the switch has been pressed or not using the serial monitor.

Circuit Configuration

Code

int switchPin = 2; // Connect the OUT pin of the KY-004 module to Arduino digital pin 2.

void setup() {
  pinMode(switchPin, INPUT);
  Serial.begin(9600); // Start serial communication.
}

void loop() {
  int switchState = digitalRead(switchPin); // Read the switch status.
  
  if (switchState == LOW) { // When the switch is pressed
    Serial.println("Switch pressed");
  } else { // When the switch is not pressed
    Serial.println("Switch not pressed");
  }
  
  delay(100); // Wait for 0.1second
}


Execution Result

The status of the switch will be displayed on the serial monitor.


2. Using Switch and LED Together

This example demonstrates that when the switch is pressed, the LED lights up, and when it is not pressed, the LED turns off.


Circuit Configuration


Code

int switchPin = 2; // Connect the OUT pin of the KY-004 module to Arduino digital pin 2.
int ledPin = 13; // Led Pin

void setup() {
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  int switchState = digitalRead(switchPin); // Read the switch status.

  // 스위치가 눌려있으면(LOW)
  if (switchState == LOW) {
    digitalWrite(ledPin, HIGH); // Turn on the LED
  } else {
    digitalWrite(ledPin, LOW); // Trun off the LED
  }
}


Execution Result

You can confirm that the LED lights up only when the switch is pressed.


Purchase Link

GONGZIPSA