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.

Issues
  • Midi host

    Midi host

    This is really cool! Are you open to contributions? I might be able to hook up a parser to get this closer to a full working implementation

    opened by marchingband 9
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 193 Dec 4, 2021
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 16 Nov 19, 2021
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 37 Nov 29, 2021
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 5 Oct 28, 2021
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 81 Dec 1, 2021
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 1 Nov 18, 2021
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 1 Nov 20, 2021
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 5 Nov 3, 2021
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/

Marcel 17 Nov 9, 2021
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

null 33 Dec 5, 2021
ESP32 S2 C++ host library compatible with arduino, esp-idf.

Info I would like to announce USB host library for esp32 S2 compatible with arduino, esp-idf and PIO (when pio will be updated to most recent esp-idf)

null 10 Sep 5, 2021
Code repo for infos and demos on the DaFit Magic 3 Smartwatch

Magic3_DaFit Code repo for infos and demos on the DaFit Magic 3 Smartwatch Demos: Magic3_Display_test_by_atc1441 = Minimal full buffer Display example

null 6 Sep 14, 2021
A repository for I/O ring demos, use cases and performance testing on Windows

IoRing_Demos A repository for I/O ring demos, use cases and performance testing on Windows Overview of I/O ring on Windows: https://windows-internals.

null 15 Nov 11, 2021
A couple of demos showing how to use the Ultra Low Power coprocessor on the ESP32

ESP32 Ultra Low Power (ULP) coprocessor You can watch a video explanation of this code here This repo contains two demo project: ulp-gpio Shows you ho

atomic14 2 Oct 28, 2021
MIDI remapping plugin (WIP)

Wolf MidiCurve Wolf MidiCurve is a MIDI remapping plugin. It can be built as an LV2 or VST plugin and as a standalone Jack application. Build manually

Wolf Plugins 7 Nov 18, 2021
USB serial TTY for the Arduino Uno with ILI9341 LCD screen shield

ILI9341TTY USB serial TTY for the Arduino Uno with ILI9341 LCD screen shield [video] Semigraphics, ANSI, unicode, and an Arduino LCD-screen terminal I

M Rule 60 Nov 27, 2021
Make BAD USB to Reverse Shell with Arduino Digispark (recommended with attiny85)

Make BAD USB to Reverse Shell with Arduino Digispark (recommended with attiny85) Cara Pakai Pastikan Anda telah menginstal Driver Digispark dan mengon

Edo 1 Oct 25, 2021