How to Build a USB Keyboard using Arduino

Âdithya K
3 min readMay 21, 2020

Many will have a need to create their own HID device for various purpose with some modification like gaming, simulation etc…

The ATMega32u4 does support USB natively, so this might be better. The ATMega32u4 is the MCU used on the Arduino Leonardo and the Arduino MICRO.

To program the above mentioned two boards its little easy and here is the steps

  1. Open the Arduino IDE, choose the board and port.
  2. Compile and upload the code Keyboard-inbuilt in GitHub.

NOTE: The header #include<Keyboard.h> is a built in function.

In case if we have a Arduino board which has some other Microcontroller rather than ATMega32u4, Even then we can convert it to a Keyboard.

Let’s consider Arduino UNO which has a ATmega382P. We can change board working by changing the firmware, Let’s see how ?

The easy way for this is Arduino Device Firmware Update(DFU). The Atmega8U2/Atmega16U2 chip on the Arduino UNO can be programmed directly using USB protocol called Device Firmware Update.

This process is normally used to update the firmware to a more recent version, as explained in the offical Arduino guide, Updating the Atmega8U2 on an Uno or Mega2560 using DFU.

However, in addition to the ability to flash standard USB Serial firmware, we can also flash alternative firmware as well. This allows the device to be recognized as many other device types, including keyboard, mouse, joystick, midi device, etc. This is made possible in part to the wonderful open source LUFA (Lightweight USB Framework for AVRs) USB stack.

In this demonstration, we will flash generic USB HID keyboard firmware. The USB HID protocol provides manufactures the generic specifications to interact with nearly every operating system in existence.

Download these two firmware files from GitHub

Installation process on Ubuntu or Debian systems,

run the following command in terminal

sudo apt-get install dfu-programmer dfu-util

The first step is to make sure you are able to flash the standard Arduino firmware. This will confirm that the programmer and the environment are both functional. NOTE: There is no chance of ‘bricking’ the device using this method.

sudo dfu-programmer at90usb82 erase
sudo dfu-programmer at90usb82 flash --debug 1 Arduino-usbserial.hex
sudo dfu-programmer at90usb82 reset

NOTE : at90usb82 may based on the chip present

Remove and plug the Arduino, then open the Arduino IDE and ensure that you can still upload a sketch. After uploading the keyboard program, we can move forward with flashing the HID keyboard firmware.

sudo dfu-programmer at90usb82 erase
sudo dfu-programmer at90usb82 flash --debug 1 Arduino-keyboard-0.3.hex
sudo dfu-programmer at90usb82 reset

Installation process for windows users follows

  1. Download Atmel’s flip programmer from here.
  2. Now power the Arduino and connect a jumper that connects the RESET pin and GND pin in ICSP header which is shown bleow.
  3. Now Ground the SMD Capacitor(left side) found below the RX LED , just touch the Capacitor with a jumper wire other end connected to GND till the LED blnks. The LED blinking indicates that the Arduino has entered DFU mode.
  4. Now remove the connection in ICSP. Open the device manager under other devices you could see either Arduino Uno DFU or Unknown device with a yellow bang.
  5. Now right click the device and choose update driver manually, browse my computer for driver software.
  6. choose the path where you installed the flip software
  7. the installation is successful you see the chip name under Atmel USB Devices in Device manager.
  8. Now you can start to flash the firmware in the Arduino UNO. Check the above video for the procedure of flashing the firmware.
  9. first flash the Arduino-usbserial-uno.hex, plug cycle the Arduino make sure you can upload the keyboard-Demo program though Arduino IDE.
  10. flash the Arduino-keyboard-0.3, plug cycle and open Notepad for testing.
  11. If you short circuit GND with pin 5, pin 6, pin 7 Letter ‘a’, ‘b’, ‘c’ will be printed
  12. Try with testing the other keys with the help of Keyboard Modifiers
RESET pin and GND pin in ICSP header
SMD Capacitor below RX LED

--

--