아두이노 - 키패드 1x4 | Arduino - Keypad 1x4

이 튜흐흐토리얼에서는 아두이노와 함께 1x4 키패드를 사용하는 방법을 배울 것입니다. 구체적으로, 우리는 다음을 학습할 것입니다:

아두이노 키패드 1x4

Hardware Preparation

1×Arduino Uno Amazon
1×USB 2.0 cable type A/B 쿠팡 | Amazon
1×Keypad 1x4 Amazon
1×Jumper Wires Amazon
1×(Optional) 9V Power Adapter for Arduino Amazon
1×(Recommended) Screw Terminal Block Shield for Arduino Uno 쿠팡 | Amazon
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno Amazon
공개: 이 섹션에서 제공된 링크 중 일부는 제휴 링크입니다. 이 링크를 통해 구매한 경우 추가 비용없이 수수료를 받을 수 있습니다. 지원해 주셔서 감사합니다.

키패드 1x4에 대하여

1x4 키패드는 한 줄로 배열된 네 개의 멤브레인 버튼으로 구성됩니다. 이는 비밀번호 입력, 메뉴 탐색 또는 제어 인터페이스와 같은 프로젝트에서 사용자 입력을 위해 일반적으로 사용됩니다.

핀배치

1x4 키패드에는 5개의 핀이 있으멠, 이들은 순서대로 키 라벨과 직접적으로 일치하지 않습니다. 구체적으로:

  • 핀 1: 키 2에 연결
  • 핀 2: 키 1에 연결
  • 핀 3: 키 4에 연결
  • 핀 4: 키 3에 연결
  • 핀 5: 모든 키에 연결되는 공통 핀입니다
키패드 1x4 핀아웃
image source: diyables.io

Wiring Diagram

아두이노 키패드 1x4 배선도

이 이미지는 Fritzing을 사용하여 만들어졌습니다. 이미지를 확대하려면 클릭하세요.

아두이노 코드

1x4 키패드의 각 키는 버튼으로 기능합니다. 이것은 우리가 각 키의 상태를 확인하기 위해 digitalRead() 함수를 사용할 수 있다는 것을 의미합니다. 그러나 실제로, 모든 버튼과 마찬가지로, 단일 누름이 여러 번 누름으로 잘못 감지될 수 있는 바운싱 문제를 처리해야 합니다. 이를 방지하기 위해 각 키를 디바운스해야 합니다. 코드의 다른 부분을 차단하지 않고 네 개의 키를 디바운스하는 것은 도전적인 작업이 됩니다. 다행히도 ezButton 라이브러리는 이 과정을 간단하게 해줍니다.

#include <ezButton.h> #define KEY_NUM 4 // the number of keys #define PIN_KEY_1 3 // The Arduino pin connected to the key 1 #define PIN_KEY_2 2 // The Arduino pin connected to the key 2 #define PIN_KEY_3 5 // The Arduino pin connected to the key 3 #define PIN_KEY_4 4 // The Arduino pin connected to the key 4 ezButton keypad_1x4[] = { ezButton(PIN_KEY_1), ezButton(PIN_KEY_2), ezButton(PIN_KEY_3), ezButton(PIN_KEY_4) }; void setup() { Serial.begin(9600); for (byte i = 0; i < KEY_NUM; i++) { keypad_1x4[i].setDebounceTime(100); // set debounce time to 100 milliseconds } } void loop() { int key = getKeyPressed(); if (key) { Serial.print("The key "); Serial.print(key); Serial.println(" is pressed"); } } int getKeyPressed() { for (byte i = 0; i < KEY_NUM; i++) keypad_1x4[i].loop(); // MUST call the loop() function first for (byte i = 0; i < KEY_NUM; i++) { // get key state after debounce int key_state = keypad_1x4[i].getState(); // the state after debounce if (keypad_1x4[i].isPressed()) return (i + 1); } return 0; }

Detailed Instructions

  • 아두이노를 키패드 1x4에 연결
  • 아두이노를 USB 케이블을 통해 PC에 연결
  • 아두이노 IDE를 열고, 올바른 보드와 포트를 선택
  • 아두이노 IDE의 왼쪽 바에서 Libraries 아이콘으로 이동
  • “ezButton”을 검색하고, ArduinoGetStarted.com이 제공하는 버튼 라이브러리를 찾기
  • Install 버튼을 클릭하여 ezButton 라이브러리 설치
아두이노 버튼 라이브러리
  • 위의 코드를 복사하고 아두이노 IDE로 열기
  • 아두이노 IDE에서 Upload 버튼을 클릭하여 아두이노에 코드 업로드
  • 시리얼 모니터 열기
  • 키패드 1x4의 키를 하나씩 누르기
  • 시리얼 모니터에서 결과 확인
COM6
Send
1 2 3 4
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ OUR MESSAGES

  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!