아두이노우노 R4

Arduino Uno R4를 HID 키보드 장치로 만들기

지구빵집 2024. 3. 26. 17:28
반응형

 

Arduino Uno R4의 특징 중 하나로 USB 주변기기에 의한 HID 키보드 장치가 있습니다. 이번 시간에는 Arduino Uno R4의 HID 키보드 장치를 만들어 보도록 하겠습니다. 

 

USB HID 장치란?

 

컴퓨터의 키보드나 마우스로서 문자 입력이나 마우스 커서 조작을 마이크로 컴퓨터에서 할 수 있는 기능입니다. 키보드와 마우스 입력 기능을 가진 아두이노 정품으로는 ATmega32u4를 탑재한 아두이노 레오나르도, 아두이노 마이크로가 있습니다. Arduino Leonardo는 4000엔 정도로 비싼 반면, Arduino Uno R4 Minima는 USB를 지원하며 가격도 Arduino Leonardo보다 저렴합니다. 또한, 교체용 보드는 2000엔 정도로 Arduino Uno의 호환 보드와 비교하면 비싸긴 하지만 저렴하다고 할 수 있습니다. 다만, Uno 모양이 아니어도 괜찮다면 Ch552나 RaspberryPiPico 등이 더 저렴하다고 할 수 있습니다.

 

샘플 프로그램으로 Arduino의 샘플 프로그램 Keyboard and Mouse Control을 참고합니다. 

 

필수 코드 사용하려면 아래 코드가 필요합니다. 

 

#include "Keyboard.h"
  Keyboard.begin();
    Keyboard.write('x');

 

Button 프로그램에 포팅한 예시

 

아래 코드를 Button 프로그램에 포팅합니다. 

 

/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground through 220 ohm resistor
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
#include "Keyboard.h"

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;  // the number of the pushbutton pin
const int ledPin = 13;    // the number of the LED pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status
int buttonState_old = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
  Keyboard.begin();
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
  if (buttonState != buttonState_old) {
    // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
    if (buttonState == LOW) {
      // turn LED on:
      digitalWrite(ledPin, HIGH);
      Keyboard.write('u');
    }
  }
  buttonState_old = buttonState;
  delay(100);
}

 

 

buttonPin인 2 핀을 GND에 연결하면 LED와 함께 u 라는 글자가 입력된다. 

 

여러 종류의 입력에 대응하는, 또한 여러 개의 입력 핀을 지원하는 프로그램으로 만듭니다.  

 

 

/*
  Button to HID Custom!
  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
#include "Keyboard.h"

// constants won't change. They're used here to set pin numbers:
const int Button2 = 2;  // the number of the pushbutton pin
const int Button3 = 3;  // the number of the pushbutton pin
const int Button4 = 4;  // the number of the pushbutton pin
const int Button5 = 5;  // the number of the pushbutton pin
const int Button6 = 6;  // the number of the pushbutton pin

const int ledPin = 13;  // the number of the LED pin

// variables will change:
int buttonState2 = 0;   // variable for reading the pushbutton status
int buttonState3 = 0;  // variable for reading the pushbutton status
int buttonState4 = 0;  // variable for reading the pushbutton status
int buttonState5 = 0;  // variable for reading the pushbutton status
int buttonState6 = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(Button2, INPUT_PULLUP);
  pinMode(Button3, INPUT_PULLUP);
  pinMode(Button4, INPUT_PULLUP);
  pinMode(Button5, INPUT_PULLUP);
  pinMode(Button6, INPUT_PULLUP);

  Keyboard.begin();
}

void loop() {
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(Button2);
  // check if the pushbutton is pressed. If it is, the buttonState is LOW:
  if (buttonState2 == LOW) {
    // Keyboard Write:
    Keyboard.write('w');
  }

  buttonState3 = digitalRead(Button3);
  if (buttonState3 == LOW) {
    Keyboard.write('e');
  }

  buttonState4 = digitalRead(Button4);
  if (buttonState4 == LOW) {
    Keyboard.write('r');
  }

  buttonState5 = digitalRead(Button5);
  if (buttonState5 == LOW) {
    Keyboard.write('t');
  }

  buttonState6 = digitalRead(Button6);
  if (buttonState6 == LOW) {
    Keyboard.write('y');
  }

  delay(100);
}

 

 

이전 입력 문자와 다를 때만 입력하는 프로그램으로 만들기

 

이전 프로그램의 경우, 버튼을 계속 누르고 있으면, delay(100); 마다 마다 문자 입력이 계속됩니다. 한 번 감지하면 두 번째는 보내지 않는 프로그램으로 변경합니다.

 

/*
  Button to HID Custom!
  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/Button
*/
#include "Keyboard.h"

// constants won't change. They're used here to set pin numbers:
const int Button2 = 2;  // the number of the pushbutton pin
const int Button3 = 3;  // the number of the pushbutton pin
const int Button4 = 4;  // the number of the pushbutton pin
const int Button5 = 5;  // the number of the pushbutton pin
const int Button6 = 6;  // the number of the pushbutton pin

const int ledPin = 13;    // the number of the LED pin

// variables will change:
int buttonState2 = 0;  // variable for reading the pushbutton status
int buttonState2_old = 0;  // variable for reading the pushbutton status
int buttonState3 = 0;  // variable for reading the pushbutton status
int buttonState3_old = 0;  // variable for reading the pushbutton status
int buttonState4 = 0;  // variable for reading the pushbutton status
int buttonState4_old = 0;  // variable for reading the pushbutton status
int buttonState5 = 0;  // variable for reading the pushbutton status
int buttonState5_old = 0;  // variable for reading the pushbutton status
int buttonState6 = 0;  // variable for reading the pushbutton status
int buttonState6_old = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(Button2, INPUT_PULLUP);
  pinMode(Button3, INPUT_PULLUP);
  pinMode(Button4, INPUT_PULLUP);
  pinMode(Button5, INPUT_PULLUP);
  pinMode(Button6, INPUT_PULLUP);

  Keyboard.begin();
}

void loop() {
  // read the state of the pushbutton value:
  buttonState2 = digitalRead(Button2);
  if (buttonState2 != buttonState2_old) {
    // check if the pushbutton is pressed. If it is, the buttonState is LOW:
    if (buttonState2 == LOW) {
      // Keyboard Write:
      Keyboard.write('w');
    }
  }
  buttonState3 = digitalRead(Button3);
  if (buttonState3 != buttonState3_old) {
    if (buttonState3 == LOW) {
      Keyboard.write('eu');
    }
  }
  buttonState4 = digitalRead(Button4);
  if (buttonState4 != buttonState4_old) {
    if (buttonState4 == LOW) {
      Keyboard.write('r');
    }
  }
  buttonState5 = digitalRead(Button5);
  if (buttonState5 != buttonState5_old) {
    if (buttonState5 == LOW) {
      Keyboard.write('t');
    }
  }
  buttonState6 = digitalRead(Button6);
  if (buttonState6 != buttonState6_old) {
    if (buttonState6 == LOW) {
      Keyboard.write('y');
    }
  }
  buttonState2_old = buttonState2;
  buttonState3_old = buttonState3;
  buttonState4_old = buttonState4;
  buttonState5_old = buttonState5;
  buttonState6_old = buttonState6;

  delay(100);
}

 

이상으로 버튼을 계속 눌러도 한 번만 문자를 보내는 프로그램이 완성된 것 같습니다. 코드 자체는 매우 간단하고, 힘도 많이 들지 않습니다. buttonState가 buttonState_old와 다른 경우 그리고 buttonState가 LOW인 경우에만 문자 입력을 하는 프로그램이 됩니다. 

 

 

 

 

 

 

 

 

 

 

 

 

 

반응형