Como ler a entrada do teclado USB no Arduino

How to read USB keyboard input on Arduino

Generally, Arduino boards can only serve as USB slaves. They cannot operate as USB hosts. This is why it is impossible to connect USB devices like a USB keyboard or mouse directly to the Arduino. These generic USB devices can be very useful in many applications. For example, in some applications, a 4X4 keyboard may not be sufficient and you may need a full keyboard for user input. This may be a situation where the embedded device may receive AT commands directly from the user or require elaborate text inputs or text updates.

Although Arduino inherently cannot operate as a USB host, it can be converted into a USB host with the help of an Arduino USB host shield. The USB host shield allows you to connect USB devices to Arduino boards. It is based on IC MAX3421E, a USB peripheral or host controller. It can implement a full-speed peripheral or a full/low speed USB host that conforms to USB 2.0. The shield is also compatible with TinkerKit modules. In this project, we will interface a USB keyboard with Arduino through a USB host shield and detect keys pressed on the keyboard.

Required components

  1. Arduino UNO/Arduino Mega x1
  2. Arduino x1 USB Host Shield
  3. USB Keyboard x1
  4. Micro USB cable (to connect Arduino and computer) x1

Arduino USB Host Shield

Arduino USB host Shield is open source hardware that allows the implementation of high-speed USB peripherals or USB hosts on USB 2.0 compliant Arduino boards. The shield can be used with Arduino UNO and Arduino Mega. The shield is based on the IC MAX3421E, a USB peripheral/host controller. The Arduino communicates with the MAX3421E on the shield via the SPI bus available through the ICSP header. The SPI bus on the Arduino UNO is on pins GPIO10, GPIO11, GPIO12 and GPIO13. On Arduino Mega, the SPI bus is on pins GPIO10, GPIO50, GPIO51 and GPIO52. GPIO7, GPIO8 and GPIO9 on both boards are used for GPX, INT and RES respectively.

Arduino USB host shield example

The shield can connect HID devices to Arduino, such as USB keyboard, USB mouse and USB joystick. It can interface with Arduino-powered game controllers like PS4, Xbox360, and Nintendo Wii. It can connect digital cameras and mass storage devices like pen drives, external hard drives or memory card readers with Arduino. Even Bluetooth dongles can be connected to Arduino via a USB host shield. ADK-compatible Android smartphones and tablets can be connected to Arduino via the shield. The shield can also connect USB to serial converters with Arduino. The shield allows you to connect, add or use many useful devices with your built-in Arduino device.

Arduino library for USB host shield
The library required to work with the Arduino USB Host Shield is “USB Host Library for Arduino”. The source code of this open source Arduino library is available on GitHub. To install the library in the Arduino IDE, navigate to Tools->Manage Libraries. Search for “USB host”. Scroll down to “USB Host Shield Library 2.0” and click install. Try installing the latest version of the library.

Installing the Arduino library for USB Host Shield

Circuit Connections
Insert the USB host shield into the top of the Arduino UNO or Arduino Mega as shown in the image below.

Inserting the Arduino USB host shield into the Arduino UNO

Insert the USB keyboard into the shield and connect the Arduino to the computer via a Micro-USB cable. The Arduino setup with USB host shield and keyboard will be as follows.

Connecting USB keyboard with Arduino UNO via USB Host Shield

Arduino Sketch

How the project works
The hidboot.h and usbhub.h libraries are required to work with a USB keyboard. The hidboot. h is responsible for analyzing USB HID devices such as keyboard and mouse. The library is used to analyze keyboard input and display key presses on the Arduino IDE Serial Monitor. The ASCII key and its key code are displayed on the Serial Monitor when a key is pressed on the keyboard. Sketch can also detect modified keys, i.e. key presses in combination with Alt, Ctrl, Shift and GPU keys.

The code
The outline starts with importing the hidboot.h and usbhub.h libraries. The hidboot.h library is used to parse input from USB HID devices such as keyboards and mice. The usbhub.h library is useful if the keyboard is connected to the shield via a USB hub. Next, the SPI library is imported while the shield communicates with the Arduino through the SPI port.

Some global variables are declared keyasc and keycode and are expressed to store the pressed key, key code and track the status of the pressed key respectively. A KeyboardInput class is defined as a child of the KeyboardReportParser class of hidboot.h. The OnKeyDown and OnKeyPressed methods are imported from the KeyboardReportParser superclass. These methods are overridden in the outline. The OnKeyDown method is overridden to detect a key press on the keyboard. The OnKeyPressed method is overridden to store the key and code of the pressed key in the global variables keyasc and keycode respectively. The Boolean flag is expressed and set to true as a new key, and the key code is recorded.

An object of the USB class is defined together with the objects of HIDBoot and the user-defined class KeyboardInput. In the setup function, the baud rate for serial communication with the Arduino IDE's Serial Monitor is set to 115200. A “start” message is printed on the Serial Monitor if serial communication is established between the Arduino and the computer.

The code then checks for detection of USB devices through the USB host shield. If not detected, prints a message “OSC did not start. ” for Serial Monitor. The SetReportParser method is called on the HidKeyboard object with the KeyboardInput class object passed as an argument.

In the loop function, the task method is called on the USB object. If a key is pressed on the keyboard, it is detected by checking the status of the expressed Boolean variable. If true, the updated key and keycode in the global variables keyasc and keycode are printed to the Serial Monitor and the express boolean is again set to false.

Result
In this project, we detect key presses on the keyboard. The ASCII keys seen on the keyboard can be used as commands or to update text input in embedded applications.

(tagsToTranslate)Arduino

Back to blog

Leave a comment

Please note, comments need to be approved before they are published.