Page 1 of 1

ESP32_BAT detect USB connection

Posted: Tue Nov 02, 2021 2:52 pm
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.

Re: ESP32_BAT detect USB connection

Posted: Tue Jan 11, 2022 12:44 pm
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.

Re: ESP32_BAT detect USB connection

Posted: Thu Mar 24, 2022 4:42 pm
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.