Skip to content

USB OTG

The ESP32-P4 includes a USB 2.0 OTG controller capable of operating in both host and device modes at High Speed (480 Mbps). The board provides two USB connectors and a jumper for mode selection.

ConnectorTypePurpose
USB-AStandard Type-A socketHOST mode — connect USB peripherals (keyboards, flash drives, etc.)
Type-C USB (OTG)Type-C receptacleDEVICE mode — board acts as a USB device to a host computer
Type-C UARTType-C receptacleProgramming and serial debug (not part of USB OTG)

The mode-switching jumper (J1) controls which USB port is active for the OTG controller.

Jumper positionActive portModeUse case
HOSTUSB-A socketHostConnect external USB devices
DEVICEType-C OTG portDeviceBoard appears as USB device to your computer
  1. Power off the board before changing the jumper position.

  2. Locate jumper J1 near the USB connectors.

  3. Move the jumper to the desired position:

    • HOST: Pins toward the USB-A connector
    • DEVICE: Pins toward the Type-C OTG connector
  4. Power the board back on. The selected USB port is now active.

In host mode, the ESP32-P4 can enumerate and communicate with standard USB devices through the USB-A port.

The ESP-IDF USB Host library supports several standard device classes:

  • HID: Keyboards, mice, gamepads
  • MSC: USB flash drives, external storage
  • CDC-ACM: Serial devices, modems
  • Custom: Vendor-specific protocols via raw transfers

Host mode example (MSC — USB flash drive)

Section titled “Host mode example (MSC — USB flash drive)”
#include "usb/usb_host.h"
#include "usb/msc_host.h"
void app_main(void)
{
// Install USB Host library
usb_host_config_t host_config = {
.skip_phy_setup = false,
.intr_flags = ESP_INTR_FLAG_LEVEL1,
};
usb_host_install(&host_config);
// Initialize MSC host driver
msc_host_driver_config_t msc_config = {
.create_backround_task = true,
.task_priority = 5,
.stack_size = 4096,
};
msc_host_install(&msc_config);
// The driver will detect and mount USB flash drives automatically.
// Access files via standard VFS paths once mounted.
}

In device mode, the board presents itself as a USB device when connected to a host computer via the Type-C OTG port.

  • CDC-ACM: Virtual serial port (useful for custom communication protocols)
  • MSC: Present the microSD card or flash as a mass storage device
  • HID: Emulate a keyboard or mouse
  • DFU: Device firmware update over USB

Device mode example (CDC-ACM — virtual serial port)

Section titled “Device mode example (CDC-ACM — virtual serial port)”
#include "tinyusb.h"
#include "tusb_cdc_acm.h"
void app_main(void)
{
tinyusb_config_t tusb_cfg = {
.device_descriptor = NULL, // Use default
.configuration_descriptor = NULL,
};
tinyusb_driver_install(&tusb_cfg);
tinyusb_config_cdcacm_t acm_cfg = {
.usb_dev = TINYUSB_USBDEV_0,
.cdc_port = TINYUSB_CDC_ACM_0,
.callback_rx = NULL, // Set your RX callback
};
tusb_cdc_acm_init(&acm_cfg);
// The board now appears as a COM port on the host computer
}
SymptomCauseFix
USB device not detected (host mode)Jumper not set to HOSTPower off, move J1 to HOST position
Board not recognized by computer (device mode)Jumper not set to DEVICEPower off, move J1 to DEVICE position
”Device not recognized” on WindowsMissing TinyUSB driverInstall the WinUSB driver via Zadig
USB flash drive not mountingInsufficient powerSome drives draw too much current; try a powered hub
Intermittent disconnectionsPoor cable qualityUse a short, high-quality USB cable