Detecting USB connection on A-Star 32U4

How do I, in a sketch/program, detect that a USB plug has been inserted ? The USB plug will be a data cable to a laptop.

background: the device is a A-star 32u4 micro board. It is normally powered on VIN with a connection to a large battery bank, supplying 12-13 volts. The micro will run for a period of time (hours to days) monitoring a couple of analog pins, and accumulating data. Occasionally I would like to connect a laptop (via USB), have the sketch detect the connection, and dump accumulated data to the laptop.

It is not clear how to detect that the USB connection is available. I saw a few posts over at talking about trying to detect small spikes in the VCC, but I doubt the USB power (VBUS being much lower that VIN) is going to have an effect.

I’m still reading other posts/responses, but I think I see light at the end of the tunnel.

On the A-Star 32U4 schematic, the device just below VBUSF, is that the power selector for VCC ? I’m not familiar with that schematic symbol. The descriptive writeup says that USB (as a power source) always takes precedence. Is that still true when USB is supplying a lower voltage ?

The other detail that fell out of my reading is Vbus (at the 32U4) is only going to be powered when USB is present. Presumably when a USB device that can supply power is present. That pin can be interrupt masked. What I really want, is to be able to poll that pin, when I enter my run-loop, to see if it shows a USB power source, which hopefully also indicates that something on the other end is ready to do communications.

Am I reading all this correctly ?


I moved this to the A-Star and Orangutan Robot Controllers forum since it is a question about the A-Star 32U4 Micro.

The simplest way to detect USB power on an ATmega32U4 board is to use this code:

bool usbDetected = USBSTA >> VBUS & 1;

This checks the VBUS bit in the USBSTA register, which will be 1 if a high-enough voltage is detected on the ATmega32U4’s VBus line.

Note that this code only detects if USB power is present. It will give a value of 0 if you plug in a cable to the A-Star and don’t connect anything on the other end of the cable. It should give a value of 1 if you plug in a simple USB charger which isn’t actually capable of sending or receiving data from USB.

Additionally, the Arduino core code provides a higher-level function for detecting USB: the “Serial” object can be converted to a bool which is true if the USB COM port has been opened by an application. You can read more about that here:

The device below VBUSF on the A-Star 32U4 Micro Schematic is a P-channel MOSFET. It serves to shut off the flow of power from USB in the case where VIN is connected. If both VIN and USB are connected, the board only draws power from VIN.


OK, that is the behavior I was hoping for. That is the opposite of what the A-Star User’s Guide says …

That mistake was present in an earlier version of the A-Star 32U4 user’s guide but it has been corrected. I suspect you are reading an old version of the user’s guide. The latest version is available here:


That is correct. The PDF version I was using had the old erroneous text in it.