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

이 튜혠리얼에서는 아두이노 나노와 1x4 키패드를 사용하는 방법을 배우게 됩니다. 다룰 내용은 다음과 같습니다:

아두이노 나노 키패드 1x4

Hardware Preparation

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

키패드 1x4에 대하여

1x4 키패드는 한 줄로 늘어선 네 개의 버튼이 있는 키패드입니다. 이는 주로 비밀번호 입력, 메뉴 이동, 장치 제어에 사용됩니다.

핀배열

1x4 키패드에는 다섯 개의 핀이 있습니다. 이 핀들은 키패드의 키와 동일한 순서로 배열되어 있지 않습니다.

  • 핀 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 5 // The Arduino Nano pin connected to the key 1 #define PIN_KEY_2 4 // The Arduino Nano pin connected to the key 2 #define PIN_KEY_3 7 // The Arduino Nano pin connected to the key 3 #define PIN_KEY_4 6 // The Arduino Nano 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 케이블을 사용하여 아두이노 나노를 컴퓨터에 연결하세요.
  • 아두이노 IDE를 열고 올바른 보드와 포트를 선택하세요.
  • 아두이노 IDE의 왼쪽에 있는 Libraries 아이콘을 클릭하세요.
  • 검색 상자에 ezButton을 입력하고 Arduino NanoGetStarted.com에서 버튼 라이브러리를 찾으세요.
  • Install 버튼을 눌러 ezButton 라이브러리를 추가하세요.
아두이노 나노 버튼 라이브러리
  • 코드를 복사하고 Arduino IDE에서 열기
  • Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino Nano로 전송
  • 시리얼 모니터 열기
  • 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!