The KY-004 Key Switch Module is a compact and versatile input device that integrates a tactile pushbutton switch. It is widely used in electronics projects for user input, allowing for simple on/off control. This module is particularly popular in hobbyist projects, including those involving microcontrollers like the Arduino UNO.
Pin Number | Description |
---|---|
1 | Signal (S) |
2 | Ground (GND) |
3 | Voltage Supply (V) |
// Define the pin connected to the KY-004 module
const int buttonPin = 2;
// Variable for storing the button state
int buttonState = 0;
void setup() {
// Initialize the button pin as an input
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the state of the button
buttonState = digitalRead(buttonPin);
// Check if the button is pressed
if (buttonState == HIGH) {
// If the button is pressed, do something
// ...
} else {
// If the button is not pressed, do something else
// ...
}
}
Note: The above code assumes that the KY-004 module is connected with a pull-up resistor. If you're using the internal pull-up resistor of the Arduino, you can initialize the button pin with pinMode(buttonPin, INPUT_PULLUP);
.
Q: Can I use the KY-004 module with a 3.3V system?
Q: How do I know if the button is pressed in my code?
Q: What is the purpose of the pull-up resistor?
This documentation provides a comprehensive guide to using the KY-004 Key Switch Module in your projects. For further assistance, consult the community forums or technical support of your microcontroller platform.