HID.h cannot find PluggableUSB.h with Arduino Leonardo

Greetings,

I have been trying to write code in the Platformio extension in VSCode using the Keyboard.h library with a Arduino Leonardo, one of the boards that supports the Keyboard.h library. First, I had a dependency issue with Keyboard.h not being able to find HID.h, but I managed to resolve that. Now, HID.h is unable to find PluggableUSB.h, one of the libraries in the Arduino core. I have no idea why it cannot find it, and its not a library available from the Platformio library manager. What am I doing wrong?

Below this is the minimal code I have been attempting to compile:

#include <Arduino.h>
#include <HID.h>
#include <Keyboard.h>
#include <PluggableUSB.h>

// put function declarations here:
void setup()
{

}

void loop()
{
  // put your main code here, to run repeatedly:
}

This is my platformio.ini file:

[env:leonardo]
platform = atmelavr
board = leonardo
framework = arduino
lib_deps = 
	arduino-libraries/Keyboard@^1.0.6
	Wire@^1.0
	mitchman631/HID@^1.0

This the error I get when trying to compile:

 *  Executing task: platformio run 

Processing leonardo (platform: atmelavr; board: leonardo; framework: arduino)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/leonardo.html
PLATFORM: Atmel AVR (5.1.0) > Arduino Leonardo
HARDWARE: ATMEGA32U4 16MHz, 2.50KB RAM, 28KB Flash
DEBUG: Current (simavr) External (simavr)
PACKAGES: 
 - framework-arduino-avr @ 5.2.0 
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 7 compatible libraries
Scanning dependencies...
Dependency Graph
|-- Keyboard @ 1.0.6
|-- Wire @ 1.0
|-- HID @ 1.0.0
Building in release mode
Compiling .pio/build/leonardo/src/main.cpp.o
Compiling .pio/build/leonardo/lib709/HID/HID.cpp.o
Compiling .pio/build/leonardo/libe07/Keyboard/Keyboard.cpp.o
.pio/libdeps/leonardo/HID/HID.cpp:19:10: fatal error: USB/PluggableUSB.h: No such file or directory
 #include "USB/PluggableUSB.h"
          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
Compiling .pio/build/leonardo/libe07/Keyboard/KeyboardLayout_en_US.cpp.o
*** [.pio/build/leonardo/lib709/HID/HID.cpp.o] Error 1
In file included from .pio/libdeps/leonardo/Keyboard/src/Keyboard.h:25:0,
                 from .pio/libdeps/leonardo/Keyboard/src/Keyboard.cpp:22:
.pio/libdeps/leonardo/HID/HID.h:24:10: fatal error: USB/PluggableUSB.h: No such file or directory
 #include "USB/PluggableUSB.h"
          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
In file included from src/main.cpp:2:0:
.pio/libdeps/leonardo/HID/HID.h:24:10: fatal error: USB/PluggableUSB.h: No such file or directory
 #include "USB/PluggableUSB.h"
          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
*** [.pio/build/leonardo/libe07/Keyboard/Keyboard.cpp.o] Error 1
*** [.pio/build/leonardo/src/main.cpp.o] Error 1
==================================================================== [FAILED] Took 0.97 seconds ====================================================================

 *  The terminal process "platformio 'run'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

The HID library is already built into the Arduino-core for the Leonardo, so it’s wrong to include mitchman631/HID@^1.0. Same for the Wire library.

So, you should just have

[env:leonardo]
platform = atmelavr
board = leonardo
framework = arduino
lib_deps = 
	arduino-libraries/Keyboard@^1.0.6

While also deleting the .pio folder of the project to get rid of the old libraries. Then, a Keyboard example sketch like this one should work fine.

Oh so manually including HID.h and Wire.h screwed things up then, because it compiled fine once I removed them. I would not have expected that. Thank you!

1 Like