USB Comes To The ESP32

Since the ESP8266 came on the scene a few years ago and revolutionized the way microcontrollers communicate with other devices, incremental progress on this chip has occurred at a relatively even pace. First there was the realization that code could be run on the chip itself. Next the ESP32 was released which built more on that foundation. The next step in that process of improvement may be here now as well, with this project which turns the ESP32 into a USB host.

USB is not a native feature on all microcontrollers or even Arduino-compatible boards. While some do have it built in like those based on the 32u4 for example, most either don’t have it at all or rely on a separate on-board chip to do some form of translating. The ESP32 is lacking this advanced feature so the USB needs to be cobbled together from scratch if you want this specific board to be able to interface directly with peripherals. This project does just that, allowing for four USB 1.1 devices to be connected directly to the ESP32 without a separate dedicated chip.

If you’ve been waiting for USB on this tiny, capable microcontroller this might be your chance to try it out. All of the project’s code is available on the project page. And, while it is limited in scope, it’s easily able to handle a keyboard or mouse. This might be a more cost-effective way of doing something like a KVM switch rather than doing it with three Arduinos.

 

20 thoughts on “USB Comes To The ESP32

  1. From this Hackaday article it was unclear to me what this project does exactly.

    The project description (in the link in the article) does make it clear though:

    Pure software implementation of USB LS HOST stack for ESP32 chip. Now we can connect usb keyboard,mouse and joystick direct to ESP32 board

    Brings back memories of V-USB from Obdev and USBasp AVR programmer which uses this on an ATmega8 to bit-bang 1.5Mbit/s USB to program AVR’s.

    1. So if I understand what you’re saying, the ESP32 can bit-bang low-speed USB, just like any other microcontroller in existence? Wow! Who’d’a thunk it?

      But thank you for clearing something up: I thought it was being used to emulate a keyboard, mouse, and joystick. It appears that it is being used as a USB host, meaning it can accept inputs from such devices.

      1. @BrightBlueJim – no, not just like any other. This is the first time I’ve seen someone bit-bang a host implementation which is a LOT harder than bit-banging a device.

        (USB put a lot of complexity into the host side of things to permit devices to be less complex)

      2. Host Implementation is not easy to bit-bang on just any microcontroller. The fact that the ESP32 has multiple cores is what makes it more possible becuase the other core can be left to do other things.

    2. “to Paulvdh”

      V-USB implements only device side of USB stack. And my code implements host side of USB stack. It not so different, but host needs significant more code and can work with several devices in same time.

      “To Bryan”

      Thanks for pointing to my project, but several small corrections:

      1 New espressif chip esp32-s2 has hardware usb-otg ( host/device ) controller. So You can connect any USB device to the new chip.
      2 My software implementation works on old ESP32 chip, which haven’t hardware USB controller and support right now only USB-HOST stack. So it can’t work as KVM switch , but can work as combined blue tooth device for example. May be later i’m implement device side on this ESP32.

      I have implemented this stack on stm32 too, which has USB device in hardware: https://github.com/sdima1357/stm32f103C8T6_USB_HOST

      So it possible make something like keylogger on one chip.

      1. Hi Dmitry, I have searched for a long time to find a way to use an ESP32 variant to adapt a standard USB keyboard to present it as a Bluetooth HID for linux, windoze etc. How far is your project away from being able to take a USB keyboard and turn it into a bluetooth HID keyboard by plugging it into the software hosted USB port? Are there enough resources left to run the Bluetooth part of the ESP32? Many, many people would like this to use their favorite mechanical keyboards via Bluetooth and I can’t see it being done yet on a single, off-the-shelf board.

  2. Cool project, can’t wait to see this in the form of an arduino library as I’m sure that it would really make this project complete and even more usable in the hacker society. Hat’s of for making a host, can’t wait to play with this, thanks for posting.

  3. The ESP32S2 and ESP32S3 have native USB.
    What I would like to see is a version (of its firmware) that supports RNDIS so that an embedded host can connect to an ESP32Sx module via USB to both power it and use its Wi-Fi via RNDIS; with AT commands on a second USB-CDC pipe for general control.
    This is the way that some G4 modems / “high-end” Wi-Fi modules do it (eg. Qualcomm based) and with RNDIS device code in the ESP32Sx there should be no reason why it can’t be done the same way. With HSUSB’s 480Mb/s data rate the interface will never be the bottle neck. Sub-1.10$ processors like the i.MX RT 1011 with integrated HS USB represent suitable embedded hosts.
    ESP32S2 (single code) would be ideal since applications will tend to be off chip.
    For people working with embedded Linux on their host it is then plug-and-play.

    1. That would be cool. For embedded non-Linux at least, the nina-fw from Arduino and adapted by Adafruit makes an esp32 into an spi wifi adapter. Adafruit sells it both standalone and integrated into several dev boards as “airlift”, and it works pretty nice. (They’re actually going to be releasing an imx rt1011 board, in Arduino uno shape, with integrated airlift, pretty soon apparently.) I don’t know the exact bit rate, but I’d imagine it’s pretty fast given spi’s reputation for speed. Spi is probably more microcontroller-y anyway. That said, even at Full Speed, the two USB pins is fewer than the several the airlift spi needs. And just for cheap good USB wifi, it would be slick: think wiring it right into a raspberry pi zero-like small WiFi free sbc.

      So yeah if you find somebody who does this please share!

    1. Unlikely without a lot of work as this would use all the available resources. The ESP32 has that extra core going for it and you can see the class being thrown into a separate task in the example code.

Leave a Reply to BrightBlueJimCancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.