Home › Lessons › USB and power interfaces › Detecting USB plug-in with an ESP32

Detecting USB plug-in with an ESP32

USB and power interfaces · Intermediate · about 20 minutes · part of Zen’s Electronics Design Mastery

A device knows a cable was plugged in by watching VBUS. Divide the 5 V down to a safe level for a 3.3 V GPIO and attach an interrupt: every insertion and removal becomes an event your code can react to (start charging, show an icon, wake up).

Detecting USB plug-in with an ESP32: the circuit on the simulator's bench

Open this lesson in the labFree, in your browser, no account needed.

How it works

Divider: V_GPIO = 5 V × 20 k ÷ (10 k + 20 k) = 3.33 V, a solid HIGH that stays inside the 3.3 V limit. With no cable the 20 k pulls the pin to 0 V (LOW).

Grounds must be shared: the breakout’s GND goes to the ESP32’s GND, or the divider has no reference.

Try it on the bench

  1. Select the USB cable and press “Unplug and remove”: the serial monitor reports the removal at once.
  2. Drag a new cable from the charger’s USB-A port to the breakout’s USB-C port: “USB attached”.
  3. Replace the charger with a USB-C PD charger and a C → C cable, then set the breakout’s Rd to missing: no VBUS, no event, even though the cable is in.

Parts on the bench

In the course

Zen’s Electronics Design Mastery, module “Capstone — Connected Tracker”.

More on usb and power interfaces