Library 31- USB HID Host for STM32F4

USB HID (or Human Input Device) is a library, that allows you to read mouse or keyboard data with your STM32F4xx device. So you can use keyboard to write characters to STM32F4xx or use mouse for LCD or any other stuff.



  • Interface with USB mouse
    • 3 buttons and movement supported
  • Interface with USB keyboard
    • QWERTY or AZERTY supported


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx EXTI
    • MISC
  • TM
    • defines.h
  • USB
    • USB HID Host stack provided by STMicroelectronics (included in library)
Data + PA12 PB15 USB Data+ line
Data – PA11 PB14 USB Data- line
ID PA10 PB12 USB ID pin
VBUS PA9 PB13 USB activate

Technically, Data+ and Data- are enough for USB communication in any way. But, STM32F4- and STM32F429- Discovery boards uses ID and VBUS pins for activate USB communication (STMPS2151 chip for USB). If you are using STM32F4 or STM32F429 Discovery boards, you need at least VBUS pin. If you are working on custom application, you can disable ID and VBUS pins. To disable pins, open project’s defines.h file and add lines below:

By default, USB FS mode is used, also used on STM32F4-Discovery board. If you want to enable USB HS in FS mode for STM32F429 Discovery board, open project’s defines.h file and add lines below:

By default, AZERTY keyboard format is in use. If you will work with QWERTY format, add lines below in defines.h file:

That’s all. You are now ready to work with USB HID devices.

Clock was set down to 168MHz for STM32F429 because you can not get 48MHz for USB from 180MHz core clock.

Functions and enumerations


Example below works with USB mouse and USB keyboard:

  • If you connect USB mouse, RED led will be ON to indicate this
    • RED led will be always on.
    • If you move mouse, then GREEN led should BLINK
    • If you press left button, GREEN led will be ON
    • If you press right button, GREEN led will be OFF
  • If you connect USB keyboard, GREEN led will be ON to indicate this
    • If keyboard is inserted, then GREEN led will be ON
    • If you press lower b, RED led will be ON
    • If you press lower v, RED led will be OFF
  • If both leds blinks, then USB device is not recignized
  • If not leds on, then device is not inserted

Project is available on my Github account, download library below.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...