아두이노 나노 ESP32 - 다중 버튼 | Arduino Nano ESP32 - Multiple Button

이 튜토리얼은 delay() 함수를 사용하지 않고 여러 개의 버튼을 동시에 작동하도록 Arduino Nano ESP32를 프로그래밍하는 방법에 대해 안내합니다. 이 튜토리얼은 다음의 두 가지 방법으로 코드를 제공합니다:

우리는 예시로 세 개의 버튼을 사용할 것입니다. 여러분은 그것을 쉽게 수정하여 두 개의 버튼, 네 개의 버튼 또는 그 이상으로 적용할 수 있습니다.

준비물

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

버튼 정보

버튼(배치, 작동 방식, 프로그래밍 방법 등)에 익숙하지 않다면, 다음 튜토리얼들이 더 많은 정보를 제공할 수 있습니다:

선연결

Arduino Nano ESP32 multiple button Wiring Diagram

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

아두이노 나노 ESP32 코드 - 디바운스를 적용한 다중 버튼

여러 버튼을 사용할 때, 특정 상황에서는 상황이 복잡해질 수 있습니다:

  • 버튼 디바운싱이 필요한 애플리케이션 (버튼에 디바운스가 필요한 이유는 여기에서 확인하세요)
  • 상태 변화(눌림/해제)를 감지해야 하는 애플리케이션

다행히도, ezButton library는 내부적으로 디바운스와 버튼 이벤트를 관리함으로써 이 과정을 간소화합니다. 이는 라이브러리를 사용할 때 타임스탬프와 변수를 관리하는 사용자의 과제를 경감시켜줍니다. 추가적으로, 버튼 배열을 사용하는 것은 코드의 명료성과 간결함을 향상시킬 수 있습니다.

/* * 이 Arduino Nano ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano-esp32/arduino-nano-esp32-multiple-button */ #include <ezButton.h> #define BUTTON_PIN_1 D4 // 아두이노 나노 ESP32 핀이 버튼 1에 연결됨 #define BUTTON_PIN_2 D5 // 아두이노 나노 ESP32 핀이 버튼 2에 연결됨 #define BUTTON_PIN_3 D6 // 아두이노 나노 ESP32 핀이 버튼 3에 연결됨 ezButton button1(BUTTON_PIN_1); // 버튼 1을 위한 ezButton 객체 생성 ezButton button2(BUTTON_PIN_2); // 버튼 2를 위한 ezButton 객체 생성 ezButton button3(BUTTON_PIN_3); // 버튼 3을 위한 ezButton 객체 생성 void setup() { Serial.begin(9600); button1.setDebounceTime(100); // 디바운스 시간을 100밀리초로 설정 button2.setDebounceTime(100); // 디바운스 시간을 100밀리초로 설정 button3.setDebounceTime(100); // 디바운스 시간을 100밀리초로 설정 } void loop() { button1.loop(); // loop() 함수를 반드시 먼저 호출해야 함 button2.loop(); // loop() 함수를 반드시 먼저 호출해야 함 button3.loop(); // loop() 함수를 반드시 먼저 호출해야 함 // 디바운스 후 버튼 상태 가져옴 int button1_state = button1.getState(); // 디바운스 후 상태 int button2_state = button2.getState(); // 디바운스 후 상태 int button3_state = button3.getState(); // 디바운스 후 상태 /* Serial.print("The button 1 state: "); Serial.println(button1_state); Serial.print("The button 2 state: "); Serial.println(button2_state); Serial.print("The button 3 state: "); Serial.println(button3_state); */ if (button1.isPressed()) Serial.println("The button 1 is pressed"); if (button1.isReleased()) Serial.println("The button 1 is released"); if (button2.isPressed()) Serial.println("The button 2 is pressed"); if (button2.isReleased()) Serial.println("The button 2 is released"); if (button3.isPressed()) Serial.println("The button 3 is pressed"); if (button3.isReleased()) Serial.println("The button 3 is released"); }

사용 방법

Arduino Nano ESP32를 시작하는 방법은 다음과 같습니다:

  • Arduino Nano ESP32에 익숙하지 않다면, Arduino IDE에서 Arduino Nano ESP32 환경 설정을 어떻게 하는지에 대한 튜토리얼을 참고하세요(BASE_URL/tutorials/arduino-nano-esp32/arduino-nano-esp32-software-installation).
  • 제공된 다이어그램에 따라 구성 요소를 연결하세요.
  • USB 케이블을 사용하여 Arduino Nano ESP32 보드를 컴퓨터에 연결하세요.
  • 컴퓨터에서 Arduino IDE를 실행하세요.
  • Arduino Nano ESP32 보드와 해당 COM 포트를 선택하세요.
  • 위 이미지와 같이 배선하세요.
  • USB 케이블을 통해 Arduino Nano ESP32 보드를 PC에 연결하세요.
  • PC에서 Arduino IDE를 열어주세요.
  • 올바른 Arduino Nano ESP32 보드(예: Arduino Nano ESP32 Uno)와 COM 포트를 선택하세요.
  • Arduino IDE의 왼쪽 바에 있는 Libraries 아이콘을 클릭하세요.
  • "ezButton"을 검색한 다음, Arduino Nano ESP32GetStarted의 버튼 라이브러리를 찾아보세요.
  • EzButton 라이브러리를 설치하려면 Install 버튼을 클릭하세요.
Arduino Nano ESP32 button library
  • 위의 코드를 복사하여 아두이노 IDE에 붙여넣으세요.
  • 아두이노 IDE에서 Upload 버튼을 클릭하여 코드를 Arduino Nano ESP32 보드에 컴파일하고 업로드하세요.
How to upload Arduino Nano ESP32 code on Arduino IDE
  • 아두이노 IDE에서 시리얼 모니터 열기
  • 버튼을 하나씩 누르고 놓기
COM6
Send
The button 1 is pressed The button 1 is released The button 2 is pressed The button 2 is released The button 3 is pressed The button 3 is released
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

아두이노 나노 ESP32 코드 - 배열을 사용한 다중 버튼

위의 코드를 버튼 배열을 사용하여 개선할 수 있습니다. 다음 코드는 이 배열을 사용하여 버튼 객체를 처리합니다.

/* * 이 Arduino Nano ESP32 코드는 newbiely.kr 에서 개발되었습니다 * 이 Arduino Nano ESP32 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-nano-esp32/arduino-nano-esp32-multiple-button */ #include <ezButton.h> #define BUTTON_NUM 3 // 버튼의 수 #define BUTTON_PIN_1 D4 // 버튼 1에 연결된 아두이노 나노 ESP32 핀 #define BUTTON_PIN_2 D5 // 버튼 2에 연결된 아두이노 나노 ESP32 핀 #define BUTTON_PIN_3 D6 // 버튼 3에 연결된 아두이노 나노 ESP32 핀 ezButton buttonArray[] = { ezButton(BUTTON_PIN_1), ezButton(BUTTON_PIN_2), ezButton(BUTTON_PIN_3) }; void setup() { Serial.begin(9600); for (byte i = 0; i < BUTTON_NUM; i++) { buttonArray[i].setDebounceTime(100); // 디바운스 시간을 100 밀리초로 설정 } } void loop() { for (byte i = 0; i < BUTTON_NUM; i++) buttonArray[i].loop(); // 반드시 loop() 함수를 먼저 호출해야 함 for (byte i = 0; i < BUTTON_NUM; i++) { // 디바운스 후 버튼 상태 가져오기 int button_state = buttonArray[i].getState(); // 디바운스 후 상태 /* Serial.print("The button "); Serial.print(i + 1); Serial.print(": "); Serial.println(button_state); */ if (buttonArray[i].isPressed()) { Serial.print("The button "); Serial.print(i + 1); Serial.println(" is pressed"); } if (buttonArray[i].isReleased()) { Serial.print("The button "); Serial.print(i + 1); Serial.println(" is released"); } } }

동영상

비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.

※ 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!