ESP32_BAT detect USB connection

Schematics, Example Code and Q&A
vicazona
Posts: 1
Joined: Tue Nov 02, 2021 2:35 pm

ESP32_BAT detect USB connection

Unread post by vicazona »

Is there a way to detect when the USB cable is connected and powering the board? I would like to keep the board awake, when that is the case, to allow for setting up Wi-Fi credentials.
benpro
Posts: 2
Joined: Sun Jan 09, 2022 2:01 pm

Re: ESP32_BAT detect USB connection

Unread post by benpro »

I have very similar goals, and it can be done. I'm a little surprised there is not a way to do this natively with the hardware. However, the USB-UART bridge drives its TX line high when the USB is connected. When it is disconnected, the bridge makes it high impedance. I'm using the ESP-IDF library, which sets up this signal to pull high within the ESP. I override this and pull it down using:
gpio_set_pull_mode(GPIO3, GPIO_PULLDOWN_ONLY);

Then you can use software to detect when this line goes low (e.g. using a BREAK character interrupt) indicating the USB is no longer plugged in. Likewise you could use a rising interrupt to indicate the USB is re-plugged.
Daniel
Posts: 133
Joined: Tue Nov 13, 2012 2:10 pm

Re: ESP32_BAT detect USB connection

Unread post by Daniel »

I have seen this thread and some other users emailed me. There isn't a way of detecting if the USB power is present. It is an oversight in the design of the board. There's a way that will work if you are prepared to add external resistors.

The Vin pin will have voltage on it only if the USB connector has power applied to it. You can connect a voltage divider from Vin and ground and tie the junction to an ADC pin. This will allow you to measure the Vin voltage and decide if there's good power available.
Post Reply