Atemga32u4 breakout Can't detect USB power- hardware issue?

Hello all,

I have a battery powered application using the Atemga 32u4 breakout that requires auto-detection of when USB power is connected, but not necessarily an actual USB connection (i.e. I need to know if I’m getting external power). From reading through the data sheet, I believe my code (pasted below) should be OK, and I think the issue may be in the setup of the USB hardware on the board (spark fun’s atemga32u4 breakout).

It looks to me (though the Atmega data sheet is a bit vague here, but head to p253) that the pin connections of the breakout board are such that when USB power is disconnected, power to the entire USB module is killed, which disables the ability of the uC to monitor changes in the VBUS pin. The schematic for the breakout and the leonardo board differ here, in that it looks like the leonardo still powers the USB module even when VUSB is removed. From poking around it looks like the leonardo can detect a VBUS change (though I don’t own one so can’t verify), and I haven’t seen anything online referring to VBUS monitoring with the atmega32u4 breakout or the pro micro.

When I use a multimeter to check the pin, I see a change of .8V (no usb) up to 5V(USB) on the VBUS/UVCC line, so the signal is there.

I know that I could add a switch or some other such thing, but I’m out of I/O pins and need to get this working without adding any additional interfacing. Also, this should be able to work, so I want to get it figured out since it’s a useful application.

If anyone has any insight into the issue I’d greatly appreciate it. I think my next step is to try chopping the uVCC line to bring the atemga breakout into accordance with the leonardo and the data sheet.

-Shibbs

Here’s my code below :

//code is compiled and loaded with the Aduion 1.1IDE under the Leonardo boot loader
void setup(){
  //macros to turn on board power and set up the LEDs
  POWER_ON;
  R_LED_INIT_OUTPUT;
  G_LED1_INIT_OUTPUT;
  G_LED1_OFF;  
  G_LED2_INIT_OUTPUT;
  G_LED2_OFF;
  delay(3000);
  //USB init 
  USBCON |= (1<<USBE)|(1<<VBUSTE)|(1<< OTGPADE) ; //enable the Vbus transition enable, as well as enable the USB module 
  UHWCON |= 1<<UVREGE; //turns on the internal regulator, shouldn't be necessaary, but was added later as a check
  sei(); //enable interrupts
}


void loop(){
 //breathe the red LED every 2 seconds
 POWER_ON; 
 R_LED_ON;
 delay(2000);
 R_LED_OFF;
 delay(2000);
 
 //checks the USB power registers
 if( (USBSTA | (1<<VBUS)) ==0 ) // check the value of the VBUS pin
   G_LED1_ON;
 else G_LED1_OFF; //this is the line that gets executed, indicating VBUS is always found ot be low
 
 if(  ( USBINT|(1<<VBUSTI)) !=0){ //check the VBus transition interrupt
    B_LED_ON;  //this is evaluated as true always
 }else B_LED_OFF; 
 USBINT &= ~(1<<VBUSTI); //clear the flag
 
}

//as best as I cna tell, neither interrupt is ever entere into
ISR(USB_GENERAL_vect){
 //Serial.println("saw a change");
 G_LED1_ON;
 USBINT &= ~(1<<VBUSTI);
  
}
ISR(USB_ENDPOINT_vect){
 //Serial.println("saw a change");
 G_LED1_ON;
 USBINT &= ~(1<<VBUSTI);
}

Here are the schematics for the boards in question :

Sparkfun’s breakout schematic : http://dlnmh9ip6v2uc.cloudfront.net/dat … ut-v11.pdf

leonardo schematic : http://www.amr-geneve.ch/wp-content/upl … tic_3b.pdf

atmega datasheet : http://www.atmel.com/Images/doc7766.pdf

Quick update, changing the hardware so that Vbus is the only pin connected to the USB’s 5V power did not fix the issue, so there must be something wrong with the code… Back to the data sheets I guess. Thoughts anyone?

Have you been able to get to the bottom of it?

No, I bailed on it and found a simpler solution (I just check the USB line using another pin that I was able to dual-purpose). Might be worth reinvestigating the issue when I get some time, but a pretty low priority at this point. I do have a leonardo now though, so it should be easier to get working.

Thanks,

I am trying to do same thing and bumped into this issue.

Just to follow up for anyone else in this situation, the following code works:

void setup(){
  USBCON = USBCON | B00010000;
  delay(150);  // Wait at least 150ms or else the UDINT always reads TRUE
  
  if (UDINT & B00000001){
  // USB Disconnected code here
  }
  else {
      // USB is connected code here
  }
}

void loop(){
}