This content relates to a deprecated version of Mbed

Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.

The USBHID class can be used to send and receive messages over USB. For instance, you can define your own protocol and communicate between your computer and the Mbed with all capabilities of a USB communication. To use USBHID, you need a script running on the host side (computer). For instance on a 32 bits Windows 7 machine, you can use pywinusb.

The USB connector should be attached to

  • p31 (D+), p32 (D-) and GND for the LPC1768 and the LPC11U24
  • The on-board USB connector of the FRDM-KL25Z

Hello World

Import program

00001 #include "mbed.h"
00002 #include "USBHID.h"
00004 //We declare a USBHID device. By default input and output reports are 64 bytes long.
00005 USBHID hid(8, 8);
00007 Serial pc(USBTX, USBRX);
00009 //This report will contain data to be sent
00010 HID_REPORT send_report;
00011 HID_REPORT recv_report;
00013 DigitalOut l1(LED1);
00015 int main(void) {
00016     send_report.length = 8;
00018     while (1) {
00020         //Fill the report
00021         for (int i = 0; i < send_report.length; i++)
00022   [i] = rand() & 0xff;
00024         //Send the report
00025         hid.send(&send_report);
00027         //try to read a msg
00028         if(hid.readNB(&recv_report)) {
00029             l1 = !l1;
00030             for(int i = 1; i < recv_report.length; i++) {
00031                 pc.printf("%d ",[i]);
00032             }
00033             pc.printf("\r\n");
00034         }
00035     }
00036 }


Import library

Public Member Functions

USBHID (uint8_t output_report_length=64, uint8_t input_report_length=64, uint16_t vendor_id=0x1234, uint16_t product_id=0x0006, uint16_t product_release=0x0001, bool connect=true)
bool send (HID_REPORT *report)
Send a Report.
bool sendNB (HID_REPORT *report)
Send a Report.
bool read (HID_REPORT *report)
Read a report: blocking.
bool readNB (HID_REPORT *report)
Read a report: non blocking.


You can choose the length of exchanged packets. In this example, mbed leds are controlled by four switches. When you press a button, there is a message sent containing buttons state. According to this message, the mbed will receive back a new message to light on leds.

We need one byte to control leds and one byte to send buttons state.

USBHID, buttons and leds

#include "mbed.h"
#include "USBHID.h"

//We declare a USBHID device: it can send 1 byte and receive 1 byte
USBHID hid(1, 1);

//Two reports where will be stored values to send and received
HID_REPORT recv_report;
HID_REPORT send_report;

//Bus of leds
BusOut leds(LED1,LED2,LED3,LED4);

//Bus of buttons
BusInOut buttons(p21, p22, p23, p24);

int main(void) {
    uint8_t p_bus = 0;
    send_report.length = 1;

    while (1) {
        //If a data is received, update led bus
        if (hid.readNB(&recv_report)) {
            leds =[0];

        //if the bus of buttons has changed, send a report
        if ( != p_bus) {
            p_bus =;
  [0] = p_bus;

Contribute to the USBHID bindings webpage!

A great thing would be to develop in several languages running on different platforms, programs able to communicate with the mbed over USB. Visit the USBHID bindings webpage and develop your own USBHID device!

All wikipages