OS: Mac Arduino: 1.8.13 Keyboard Library: 1.0.2


I have a generic ESP8266 board, and I have the following code

void setup() {
  // put your setup code here, to run once:

void loop() {}

However I get the following ouput:

In file included from /Users/quba/Sandbox/sketch_feb07a/sketch_feb07a.ino:1:0:
/Applications/Arduino.app/Contents/Java/libraries/Keyboard/src/Keyboard.h:25:17: fatal error: HID.h: No such file or directory
#include "HID.h"
compilation terminated.
exit status 1
Error compiling for board Generic ESP8266 Module.
Could not find boards.txt in /Users/quba/Library/Arduino15/packages/arduino/hardware/megaavr/1.8.7. Is it pre-1.5?

2 Answers 2


The Keyboard library is for Arduino ATMega32U4 based Arduinos (and other similar ones with USB) only. The ESP8266 does not have USB, so how can it possibly use the Keyboard library?

  • I have added a picture so you can see the TX and RX pins, as well as the USB Feb 8, 2021 at 8:02
  • 2
    The keyboard library is meant for devices that can talk DIRECTLY to the USB bus, like the AVR ATMega32u4. The ESP8266 on the NodeMCU you show in your picture does not have that capability but it has a separate Serial-to-USB chip that's able to talk to a PC via USB. Even then, it can only act as a peripheral to a PC and not as a USB "Master" that's able to control its own USB peripherals (like a keyboard).
    – StarCat
    Feb 8, 2021 at 9:34

I have been looking around for an answer and found a great explanation here:

Arduino/Genuino Uno does not support Keyboard. You can see it in hardware/ardunino/avr/libraries/HID/HID.h. This file is included from Keyboard.h and contains #if defined(USBCON). But USBCON is not defined for Arduino/Genuino Uno. You need to use another board for Keyboard.h

Following controllers support it:

* ATmega32U6
* ATmega8U2
* ATmega16U2
* ATmega16U4
* ATmega32U2
* ATmega32U4
* and some ATxxUSBxx

Arduino/Genuino Uno uses ATmega32U8.

So ESP8266, as kindly mentioned by StarCat above, cannot communicate directly with the USB bus. It does not have the hardware that allows it to be an HID

In short, the board itself needs to define a USBCON field (and have the associated hardware) which allows the Arduino to be a USB Master.

I would still like to know how to find the code behind this so I can see what I'm getting when I buy a board, but that is part of the learning process.

  • No, the board does not need to "define" anything. The board has to be based around a microcontroller that the library supports. In short: NOT an ESP8266. If you want all-in-one WiFi/Bluetooth and USB then you have to use the (only just released) ESP32-S2 chip. I think you're confusing the board (the whole big black thing) with the microcontroller (the silver box at one end) and thinking that boards provide this kind of functionality when it's solely down to the MCU and what it can provide. You need to learn to read datasheets and understand specifications before you can know what is provided
    – Majenko
    Feb 8, 2021 at 10:41
  • Everything on the board that isn't the silver box is just support components to either allow the MCU to function or to just make life easier for the end user. One of those "support components" is a chip that connects the MCU's UART pins to your computer through USB. Just purely for convenience. That's the long thin black chip at the bottom of your picture. That's all the USB there is there, and has nothing at all to do with the microcontroller - just purely an addition by the board designer to make programming easier.
    – Majenko
    Feb 8, 2021 at 10:43
  • Quintin, a keyboard is not USB master. or do you want to attach a keyboard to esp8266?
    – Juraj
    Feb 8, 2021 at 11:03
  • @Juraj - no I was hoping to use the ESP8266 as a keyboard, with buttons attached. I have opted to write a nodejs server and respond to button presses as web events instead (if I am going to use the 8266). I might just use a Nano 33 I had lying around because I would rather not right client side code. My code works fine on the Nano at least. Thank you Feb 8, 2021 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.