ESP32S2 Arduino USB host printer, MIDI, and keyboard demos

Overview

ESP32 S2 USB Host Jumpstart

A collection of experiments and demos using Espressif ESP32 S2 in USB host mode. Most of the sketches have little or no C++ or Arduino dependencies so can be converted to ESP-IDF programs. The ESP32 S3 does not have Arduino support so converting the sketches back to C ESP-IDF programs would be useful.

The code is based on ESP32 USB host tests and examples included with ESP-IDF.

Use at your own risk. Error handling is lacking. These are demos so do not do anything useful except show it is possible to communicate with real world USB devices. More work is needed to make them useful.

To see the sketch output on the serial monitor set the Core Debug Level to Verbose.

Software

  • Arduino IDE 1.8.16
  • arduino-esp32 (stable) 2.0.1

Hardware

  • Espressif ESP-32-S2-DevKitC-1 board
  • USB OTG to USB host cable
  • USB break out board to access VBUS

The USB printer is self-powered which means it is powered by its battery. It does not need the USB VBUS 5V and does not charge its battery from VBUS.

ESP32-S2-DevKitC-1 -> USB OTG to USB host -> USB cable -> USB Printer

USB printer connected to ESP32 S2

The above configuration fails for bus-powered devices such as keyboards because they depend on VBUS providing 5V. The DevKit USB port does not provide 5V on VBUS. It only works as a power input, not output. A true USB OTG port is able to switch the direction of VBUS based on the USB micro connector ID pin.

The following is my workaround for this.

ESP32-S2-DevKitC-1 -> USB OTG to USB host -> USB breakout -> USB MIDI
        5V                                       VBUS  
        |                                         ^
        v                                         |
        .-----------------------------------------.

USB MIDI keyboard connected to ESP32 S2

The yellow wire connects 5V out on the dev board to the USB breakout to power the USB MIDI keyboard.

A more reliable solution is to make a cable using USB connector breakout boards that looks like the USB OTG to USB host cable but with an extra flying wire for VBUS.

An alternative is to cut open a USB OTG to USB host cable, cut the red power wire then solder a flying wire to VBUS.

dumpdesc -- USB Descriptor Dump

Arduino sketch that shows frequently used USB descriptors in human readable form.

Sample output

[ 15501][I][show_desc.hpp:54] show_config_desc(): bLength: 9
[ 15506][I][show_desc.hpp:55] show_config_desc(): bDescriptorType(config): 2
[ 15513][I][show_desc.hpp:56] show_config_desc(): wTotalLength: 216
[ 15519][I][show_desc.hpp:57] show_config_desc(): bNumInterfaces: 4
[ 15525][I][show_desc.hpp:58] show_config_desc(): bConfigurationValue: 1
[ 15531][I][show_desc.hpp:59] show_config_desc(): iConfiguration: 0
[ 15537][I][show_desc.hpp:64] show_config_desc(): bmAttributes(, Remote Wakeup): 0xa0
[ 15545][I][show_desc.hpp:65] show_config_desc(): bMaxPower: 50 = 100 mA

usbhmidi -- USB Host MIDI

Arduino sketch that is just enough to get a USB MIDI keyboard (and may other MIDI devices) talking MIDI over USB. It might be possible to make this work with the USB transport layer of the Forty Seven Effects MIDI Library. But I not sure I will ever have time for this.

USB MIDI Event Packet Format (always 4 bytes).

Byte 0 Byte 1 Byte 2 Byte 3
CN+CIN MIDI_0 MIDI_1 MIDI_2
  • CN = Cable Number (0x0..0xf) specifies virtual MIDI jack/cable
  • CIN = Code Index Number (0x0..0xf) classifies the 3 MIDI bytes. See Table 4-1 in the MIDI 1.0 spec at usb.org.

Sample output showing MIDI note on/off from a keyboard

[ 69235][I][usbhmidi.ino:30] midi_transfer_cb(): midi_transfer_cb context: 0
[ 69235][I][usbhmidi.ino:37] midi_transfer_cb(): midi: 08 80 3c 40
[ 74603][I][usbhmidi.ino:30] midi_transfer_cb(): midi_transfer_cb context: 1
[ 74604][I][usbhmidi.ino:37] midi_transfer_cb(): midi: 09 90 3c 7a
[ 74690][I][usbhmidi.ino:30] midi_transfer_cb(): midi_transfer_cb context: 2
[ 74690][I][usbhmidi.ino:37] midi_transfer_cb(): midi: 08 80 3c 40

usbhhidboot -- USB Host HID Boot Keyboard

Arduino sketch that is just enough to get a USB HID keyboard working. It uses HID boot mode which means it does not need to fetch or parse the HID report descriptor. More work required to convert the HID reports into ASCII chars and key up/down events. Does not implement SetProtocol so some keyboards may require this.

Maybe model this on the official USB Host KeyboardController object. https://www.arduino.cc/en/Reference/KeyboardControllerConstructor

Some explanation of USB HID reports. https://wiki.osdev.org/USB_Human_Interface_Devices

Sample output with additional comments

// All keys up
[ 29449][I][usbhhidboot.ino:36] keyboard_transfer_cb(): data: 00 00 00 00 00 00 00 00
// Shift key down
[ 31770][I][usbhhidboot.ino:36] keyboard_transfer_cb(): data: 02 00 00 00 00 00 00 00
[ 32265][I][usbhhidboot.ino:36] keyboard_transfer_cb(): data: 02 00 00 00 00 00 00 00
// Shift and 'A' keys down
[ 33514][I][usbhhidboot.ino:36] keyboard_transfer_cb(): data: 02 00 04 00 00 00 00 00
[ 34026][I][usbhhidboot.ino:36] keyboard_transfer_cb(): data: 02 00 04 00 00 00 00 00
// All keys up
[ 34185][I][usbhhidboot.ino:36] keyboard_transfer_cb(): data: 00 00 00 00 00 00 00 00

usbhprinter -- USB Host Printer Class

Arduino sketch that is just enough to print one line on a USB thermal receipt printer. Would be nice to create a subclass from the Arduino stream class so it appears similar to a Serial device. And an ESC POS library to print in graphics mode, double wide, italics, bold, etc. Lots more work required.

The sketch currently prints 1 line with "Hello from ESP32 S2".

The sketch reads a line from the serial monitor and writes it to the printer. This proves USB printer communication is working. This may be the first ESP32 typewriter.

You might also like...
USB host implementation using PIO of raspberry pi pico (RP2040).

Pico-PIO-USB USB host implementation using PIO of raspberry pi pico (RP2040). You can add additional USB port to RP2040. 🚧 This library is WIP. API m

ESP32 drum computer / sample player / midi sequencer (Arduino audio project)

esp32_drum_computer ESP32 drum computer / sample player / midi sequencer (Arduino audio project) The project can be seen in my video https://youtu.be/

ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project)
ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project)

esp32_esp8266_organ ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project) link to the video The required li

ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project)
ESP32, ESP8266 based MIDI Organ using the ML_SynthTools library (little example arduino project)

ml_synth_organ_example MIDI Organ using the ML_SynthTools library (little example arduino project) link to the video This project is an example suppor

 split89 keyboard - a 3d printed 89 key split TKL keyboard base powered by ATmega32U4 Pro Micro controllers with QMK Configurator support.
split89 keyboard - a 3d printed 89 key split TKL keyboard base powered by ATmega32U4 Pro Micro controllers with QMK Configurator support.

split89 keyboard - a 3d printed 89 key split TKL keyboard base powered by ATmega32U4 Pro Micro controllers with QMK Configurator support. This keyboar

Gesture-Detecting-Macro-Keyboard - Glorified Bluetooth macro keyboard with machine learning (TensorFlow Lite for Microcontrollers) running on an ESP32.
Gesture-Detecting-Macro-Keyboard - Glorified Bluetooth macro keyboard with machine learning (TensorFlow Lite for Microcontrollers) running on an ESP32.

Gesture detection tldr; Glorified Bluetooth macro keyboard with machine learning (TensorFlow Lite for Microcontrollers) running on an ESP32. Main feat

Simple ATTiny85 based PS/2 to Amiga keyboard protocol converter that fits inside the keyboard.
Simple ATTiny85 based PS/2 to Amiga keyboard protocol converter that fits inside the keyboard.

Simple ATTiny85 based PS/2 to Amiga keyboard protocol converter that fits inside the keyboard.

A fully-functional open source and open hardware mechanical USB computer keyboard with only three keys!
A fully-functional open source and open hardware mechanical USB computer keyboard with only three keys!

threeboard threeboard is a fully-functional open source and open hardware mechanical USB computer keyboard with only three keys. It supports multiple

USB2Sun - USB keyboard/mouse to Sun Converter

USB2Sun While I've found many projects for connecting the Sun serial based keyboards to a modern machine via USB, I've never found anything to connect

Owner
null
ESP32 software USB host through general IO pins. We can connect up to 4 USB-LS HID (keyboard mouse joystick) devices simultaneously.

esp32_usb_soft_host esp32 USB-LS pure software host thru general IO pins. Up to 4 HID devices simultaneously. board ~$3 :https://www.aliexpress.com/pr

Samsonov Dima 313 Jan 1, 2023
ESP8266 software USB host through general IO pins. We can connect up to 2 USB-LS HID (keyboard mouse joystick) devices simultaneously.

esp8266_usb_soft_host Test for esp8266 usb host . Works with ESP-IDF v3.4 80MHz and 160MHz. WorkInProgress Test run with mouse & combined mouse & keyb

Samsonov Dima 28 Sep 1, 2022
esp32s2 implement a usb port display with 320*240 size with ~13pfs

esp32s2_usb_display overview it's a USB mini display for Linux platform, such as raspberry Pi, Centos X86 server. it refer many opensource projects: r

null 10 Oct 13, 2022
Tiny and portable usb host and device stack for mcu with usb ip

Tiny and portable usb host and device stack for mcu with usb ip

sakumisu 545 Jan 2, 2023
Raw HID keyboard forwarder to turn the Pi 400 into a USB keyboard

Raspberry Pi 400 as a USB HID Keyboard Hook your Pi 400 up to your PC somehow, using a USB Type-C cable into the power port. Anker make good ones- I u

Philip Howard 182 Dec 12, 2022
Use Atari keyboard as USB keyboard with a Teensy 3.6

Atari Keyboard Convert an Atari 600/800/1200 XL into a USB keyboard. I bricked my Atari mainboard. My goal is to use the keyboard of the Atari on a Ra

Jos Koenis 2 Dec 3, 2021
Code for my LED strip attached to a MIDI keyboard

midi-lights The original purpose of this code was simply to light an LED strip in response to MIDI received on the ESP8266 Serial port using this sche

Anand Lobo 2 Nov 29, 2022
Link your MIDI keyboard to ACE Virtual Singer

ACED Link your MIDI keyboard to ACE Virtual Singer Running Clone this project and change the forms in the beginning of ACED.cpp to apply your own sett

null 3 Oct 13, 2021
usb to 5 din midi converter-filter-router, sound generator

multi What is multi? It's a PCB (shield/hat) hosting a seeeduino Xiao. It has 6 potentiometers, 2 pushbuttons and a 1/8" audio out connected to the Xi

pangrus 29 Dec 31, 2022
ESP32 S2 USB host with examples.

This new USB host library based is based on changed and hopefully somehow final API from espressif. The idea is to make this library usable with esp-i

null 22 Nov 10, 2022