Dear friends
In my application I need to detect if the USB port is plugged to the board. I used a piede of code (see below) that works fine with Arduino Pro Micro 5V, but from another brand (not from Sparkunf). I need this function for checking if the USB port is plugged to my laptop, and starting a communication task. As you can see, I take advantage of the hardware connected VBUS pin from the ATmega32u to the VBUS1 terminal in the USB connector through the UVCC net (see schematics: https://cdn.sparkfun.com/datasheets/Dev … o_v13b.pdf)
The problem arise when using an Arduino Pro Micro 5V form Sparkun (https://www.sparkfun.com/products/12640). The applications works ok, but not the commented function. The USB - laptop communication is ok, but bypassing that function.
Furthermore, when I remove the Sparkfun unit from my circuit and use the other board from another brand, the function works fine (it detects if the board is still plugged into USB).
This is the function I use:
int8_t Check_USB()
{ // check if USB is plugged. Return: 0= USB plugged, -1== USB not plugged
USBCON|= 0x10; //(1<<OTGPADE); // enable VBUS pad
if ((USBSTA & 1) == 0 ) // USBSTA.VBUS== 0: USB not plugged
{
if(Serial) // then close any Serial port
{
Serial.end();
}
return -1; // returning USB not plugged
}
else // bit VBUS==1: USB plugged
{
if(!Serial) // Start comm
{
Serial.begin(9600);
while(!Serial); // waiting for comm
}
// Clear buffer
while(Serial.available())
{
Serial.read();
}
}
return 0; // USB plugged
}
I checked also the connection in the PCB layout, but I couldn’t find any problem.
Any recommendation to solve the problem?
Thanks in advance