Determine if USB power is present?

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
B7labelle
 
Posts: 7
Joined: Sun Nov 05, 2017 6:51 am

Determine if USB power is present?

Post by B7labelle »

Hi guys, I recently bought a Feather HUZZAH and I really like it. I'm using it as part of a temperature monitoring system, and the last hurdle I have is to determine if the board is running off the main USB power supply or the back-up battery.

Someone suggested to me that I use the USB voltage pin to feed the gate of a thyristor, which would then pass or block the voltage from the boards 3.3v pin to a GPIO pin depending on whether the USB voltage was present.

Is this feasible, or am I going to burn up my new little board?

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Determine if USB power is present?

Post by adafruit_support_mike »

I've never seen the thyristor idea before.. it might work in theory, but you'd have to make sure the voltage reaching the IO pin never rises higher than 3.3v

We usually drop a voltage divider from the USB pin to GND and read it with the ADC. The USB pin gets power from the USB cable if one is present, or from the LiPo if not.

A 470k-100k voltage divider will drop 5v to about 880mV. When running from LiPo power, that will drop to about 700mV for a fully-charged LiPo, and go down to about 540mV when the LiPo is almost dead.

The ESP8266's ADC can tell those values apart easily.

User avatar
B7labelle
 
Posts: 7
Joined: Sun Nov 05, 2017 6:51 am

Re: Determine if USB power is present?

Post by B7labelle »

I should have mentioned I am using the ADC pin to monitor a sensor's output, so that pin isn't available.

After allowing myself to sleep on it and doing a little more research this morning, I don't think a thyristor is any better than a transistor for my case.

In case anyone else wants to do this, its actually pretty easy. I figured out something that seems to works:

I have put a 10k resistor between the USB voltage pin and the base pin of a 2n2222 transistor. The collector pin is connected to a GPIO, and the emitter to the system ground.

A simple modification to the Lesson 6 script allows me to turn on and off a board LED when I manually remove the USB voltage. For my project I will have it send me a notification that USB power has been lost, rather than turning on/off the LED.

Code: Select all

/*
Adafruit Arduino - Lesson 6. Inputs
*/

int ledPin = 2;
int buttonApin = 14;

byte leds = 0;

void setup() 
{
  Serial.begin(9600);
  
  pinMode(ledPin, OUTPUT);                // SET LEDPIN (2) as an output.
  pinMode(buttonApin, INPUT_PULLUP);      //Set GPIO 14 as input
}

void loop() 
{
  
  if (digitalRead(buttonApin) == LOW)
  {
    digitalWrite(ledPin, LOW);
    Serial.print("0");
    Serial.println("");
  }
    else {
      digitalWrite(ledPin, HIGH);
      Serial.print("1");
      Serial.println("");
    }

}

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Determine if USB power is present?

Post by adafruit_support_mike »

Glad to hear you got it working. Happy hacking!

User avatar
B7labelle
 
Posts: 7
Joined: Sun Nov 05, 2017 6:51 am

Re: Determine if USB power is present?

Post by B7labelle »

Soooo it turns out this doesn't work on it own.



"USB - this is the positive voltage to/from the micro USB jack if connected"

I thought this meant the pin voltage source was the USB jack. Turns out its also connected to the battery. When the USB power is removed, a voltage remains present on the USB pin.

Edit - so there is a IC which can be used to accomplish what I need. Adafruit sells the 3.3v version, but not variants with triggers at other voltage levels. I selected a 4.2v version (KA75420) from a Chinese supplier, since that seems to be the only outlets I could find those.

Locked
Please be positive and constructive with your questions and comments.

Return to “Feather - Adafruit's lightweight platform”