Author Topic: How to detect if USB-Power comes from PC or Wall- Adapter  (Read 10935 times)

0 Members and 1 Guest are viewing this topic.

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
How to detect if USB-Power comes from PC or Wall- Adapter
« on: October 03, 2013, 02:12:47 pm »
Hi

I'm designing a little USB Li-ION Battery charger circuit using a Microchip MCP73838T single cell Li-Ion Charger IC.
http://www.farnell.com/datasheets/1538062.pdf

This IC is capable of up to 1A charging current from a wall- adapter. So for that purpose the device has two seperate inputs. One for USB, where the charging current is limited to 500mA, and one for a wall adapter with up to 1A charging current.
Thats fine, as long as it is ok for you to use 2 seperate inputs, f.e. a USB jack and a DC jack.

But I want to have only one USB jack as a input, and some sort of detection circuit which then enables/disables a  P-Channel Mosfet pair, supplying power either to the USB input or the WA input.
So my detection circuit has to know, if the USB lead is hooked up to a pc or a wall adapter and then has to select the appropriate input and therefore the appropriate charging current.
I think I have to somehow analyze the data pins, but they are both Low when no device is connected.

Has anyone an idea how I could solve this problem, preferably in a elegant way?
 

Offline TMM

  • Frequent Contributor
  • **
  • Posts: 471
  • Country: au
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #1 on: October 03, 2013, 02:31:02 pm »
This may be of some interest to you
http://www.skyworksinc.com/uploads/documents/Automatic_USB_Detection_AppNote_202742Apdf.pdf

Also, technically USB should only supply 100mA unless the device reports that it needs 500mA. There are off the shelf ICs that take care of all of this and a lot of them have li-ion charging as well.
 

Offline ConKbot

  • Super Contributor
  • ***
  • Posts: 1399
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #2 on: October 03, 2013, 07:51:13 pm »
Read the USB battery charging standard. It explains how you can tell how much current the charger can supply, and if it is a dumb charger or a PC.

The simple way to detect a PC is to look for activity on the D+/D- lines. You will see a start of frame clock from time to time with a PC attached. Otherwise it's a dumb adapter. Not all adapters can supply more than 500mA though, you need to check. Basically high power chargers tie the D+/D- lines together, except for Apple who are dicks^H^H^H^H sorry, I mean who use resistor dividers to generate specific voltages.

Having been futzing around with configurations for serial consoles on a PC at work today I find lulz in the ^H
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6289
  • Country: 00
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #3 on: October 04, 2013, 04:52:32 pm »
A common scheme for Android devices is to have the two data pins of the AC charger shorten to each others. Most Android AC charger come this way. The device detects it and switch to AC charging (as opposed to USB charging, as display in the Settings screen). 

http://android.stackexchange.com/questions/24378/what-is-the-difference-between-usb-charging-and-ac-charging

There are also special cables that will do it for you.  E.g.

https://dangerousthings.com/shop/android-fast-charge-usb-cable/

 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 9238
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #4 on: October 05, 2013, 01:27:18 am »
Connect one data line to the supply rail through a 100k or so resistor and the other to a logic input with a 1M pulldown.  If you also want fast charge support for USB 3.0, just check if the USB 3.0 ground is actually connected to ground.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #5 on: October 06, 2013, 08:45:42 am »
Thanks to your replies, and sorry for the late reply, I was busy with university stuff :D

So to summarize things:

If a computer is connected

  • Most computers will have 15kOhm resistors connected from the data lines to GND
  • Apple uses some resistive divider which puts a weak voltage on the data pins

If a Charger/Wall- adapter is connected

  • Most chargers will short the data lines together
  • Some also let them open

Ok so far so good.
Then there is still the (problem) with the 500mA current draw from a USB port without previous enumeration. I imho think that this is only a problem on paper and not so much in the real world. As nearly every "dumb" gadget, without a bigass processor in it, will violate the spec. It's simply not up for debate to invest a couple of bucks for a processor, which can handle USB communication, only to correctly enumerate the device to the USB host.  µC's with built in USB are most of the time really expensive and come in huge pincount packages, which are complete useless, if the USB powered device in question doesn't need a processor at all.
If that would've been a big problem in the past, chip manufactures would have added a USB controller to their products. So f.e. a Li-IOn battery charger would then automatically enumerate itself to the USB host and requesting to draw 500mA.

Even Maxim on their page on "Charging batteries using USB Power" write about it.
http://www.maximintegrated.com/app-notes/index.mvp/id/3241

Quote
What Your Mom Didn't Tell You About USB

With any standard, it's interesting to see how actual practice diverges from the printed spec or how undefined parts of the spec take shape. Though USB is, with little doubt, one of the best thought out, reliable, and useful standards efforts in quite some time, it has not been immune to the impact of the real world. Some observed USB characteristics that may not be obvious, yet can influence power designs, are: 
  • USB ports do NOT limit current. Though the USB spec provides details about how much current a USB port must supply, there are mile-wide limits on how much it might supply. Though the upper limit specifies that the current never exceed 5A, but a wise designer should not rely on that. In any case, a USB port can never be counted on to limit its output current to 500mA, or any amount near that. In fact, output current from a port often exceeds several Amps since multi-port systems (like PCs) frequently have only one protection device for all ports in the system. The protection device is set above the TOTAL power rating of all the ports. So a four-port system may supply over 2A from one port if the other ports are not loaded. Furthermore, while some PCs use 10-20% accurate IC-based protection, other will use much less accurate poly-fuses (fuses that reset themselves) that will not trip until the load is 100% or more above the rating.
  • USB Ports rarely (never) turn off power: The USB spec is not specific about this, but it is sometimes believed that USB power may be disconnected as a result of failed enumeration, or other software or firmware problems. In actual practice, no USB host shuts off USB power for anything other that an electrical fault (like a short). There may an exception to this statement, but I have yet to see it. Notebook and motherboard makers are barely willing to pay for fault protection, let alone smart power switching. So no matter what dialog takes place (or does not take place) between a USB peripheral and host, 5V (at either 500mA or 100mA, or even maybe 2A or more) will be available. This is born out by the appearance in the market of USB powered reading lights, coffee mug warmers, and other similar items that have no communication capability. They may not be "compliant," but they do function.

Ok now back to my project:

As I can't go without a little microcontroller on my PCB (ATTiny10) I have tought up the following scenario.

One of the Data Lines is pulled up to +5V via a 100kOhm resistor. The Data pin is also connected to a ADC pin of the ATTiny.
If a computer is connected the 100kOhm pullup resistor and 15kOhm pulldown resistor in the computers USB hub form a voltage divider which gives out voltage of around 700mV. If a mac is connected it's voltage level on the data- line could also be detected as the 100kOhm pullup is really weak.
If now a Walladapter is connected and the datalines are shorted or open, the 100kOhm pullup ties the voltage on the data- line up to +5V which the µC can measure and recognize.

And now I can hear you ask why I have to use a µC for that, becaus that could also be achieved using a handful of discrete parts?

Now I need the µC to ajust the charging current when using a walladapter. Because you can't assume that every walladapter a user can plug into the charger can supply the full 1A of charging current. Therfore I use another ADC pin of the Tiny10 which is connected to the USB bus voltage and a PWM pin to set the current of the charging chip. The PWM gets filtered trough a 2nd order low pass filter, gets decoupled with a diode and then put on the current set pin of the charging chip. The µC then increases the PWM DC thus increasing the charging current. While doing so it measures the USB bus voltage coming from the wall- adapter. If it detects a significant voltage drop, that means that the walladapter is near overload, and so it stops increasing the charge current. The current where the USB voltage started to drop is going to be decreased 10% and then used for charging.
« Last Edit: October 06, 2013, 08:57:34 am by Electr0nicus »
 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2320
  • Country: au
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #6 on: October 06, 2013, 10:16:16 am »
I question your assertion that you need a uC.

I think mojo-chan's suggestion of just using a IC that detects android, apple, and standard computer USB ports and draws the appropriate level of current and charges an Li-ion battery for you is probably kinda the way to go. Because programming your AtTiny to do all this for you is going to be re-inventing the wheel to the nth degree.

Even if you have trouble getting your hands on a dedicated single-chip solution, a basic "this is a PC/this is an standard D+/D- lines shorted" detector IC is just a "jellybean" part these days.

Also, if you decide to do this detection yourself for some reason, I believe the standard that Android USB chargers adhere to states that you draw as much current as you can without having the voltage supply sag below some set limit. <opinion=mine>Handling this with a micro and a second-order filter DAC and etc etc when a simple op-amp can do it for you is bad because it's more difficult, and slower, and all discrete in time and bleargh.

"Decoupled with a diode"? Please elaborate.
 

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #7 on: October 06, 2013, 12:35:18 pm »
I question your assertion that you need a uC.

I think mojo-chan's suggestion of just using a IC that detects android, apple, and standard computer USB ports and draws the appropriate level of current and charges an Li-ion battery for you is probably kinda the way to go. Because programming your AtTiny to do all this for you is going to be re-inventing the wheel to the nth degree.

Even if you have trouble getting your hands on a dedicated single-chip solution, a basic "this is a PC/this is an standard D+/D- lines shorted" detector IC is just a "jellybean" part these days.


Maybe you can suggest me such chip. I've searched some time on Farnell's and RS's websites and haven't found anything appropriate. Maybe I don't know the right term to search for.

Quote
Also, if you decide to do this detection yourself for some reason, I believe the standard that Android USB chargers adhere to states that you draw as much current as you can without having the voltage supply sag below some set limit. <opinion=mine>Handling this with a micro and a second-order filter DAC and etc etc when a simple op-amp can do it for you is bad because it's more difficult, and slower, and all discrete in time and bleargh.

I didn't knew that a Android standard exists. But your theory that the charger maintains 5V on the output whatever load you hook on its output is busted. Tested it with my android charger. Specs are 750mA @ 5V. Added a 5Ohm pure resistive load and the voltage decreased to 3.5V. Maintaining 5V on the output, whatever load you put on it, wouldn't be possible simply because ohms law. To maintain 5V over a smaller resistor value the current has to increase.
Quote
"Decoupled with a diode"? Please elaborate.

Here you go (click on image to enlarge):

 

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #8 on: October 06, 2013, 02:09:52 pm »
OK I think I'll drop the amateurish solution and use a USB capable AVR. I found a ATXMEGA128B3 which has a USB interface. There seem to be many ready to use USB libraries for those XMEGAs out there. Even AVR studio should have some ready to use examples. Then I'll be able to enumerate  500mA for my charger according to USB specification.

A µC can also add some features to my final product, which I haven't originally planned, but nevertheless would be nice to have :D
The final product will be a standalone mobile PSU solution for homemade electronics projects. It will incorporate a Li- ION charger (which is discussed here) and a 500mA 5V DC-DC converter on one PCB with a battery holder for one 18650 Li-ION cell. So if you made a little electronics project at home and want to use it mobile, the idea is, that you only have to drop in that PCB and you are ready to go. The PCB will not be bigger than the 18650 battery holder itself. Using the Xmega µC, the project can be expanded f.e. having easy to use USB-R232/SPI/TWI conversion features.

I really don't have much time for the project now, but I will keep you updated!
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #9 on: October 06, 2013, 03:00:54 pm »
Quote
Maintaining 5V on the output, whatever load you put on it, wouldn't be possible simply because ohms law.

Not sure where you got that from ohm's law. But the fact that a charger cannot maintain its rated voltage output does not invalidate ohm's law, or imply that no charger could.
================================
https://dannyelectronics.wordpress.com/
 

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #10 on: October 06, 2013, 03:43:24 pm »
Quote
Maintaining 5V on the output, whatever load you put on it, wouldn't be possible simply because ohms law.

Not sure where you got that from ohm's law. But the fact that a charger cannot maintain its rated voltage output does not invalidate ohm's law, or imply that no charger could.

I meant it in that way:

Suppose a WA can supply 5V @ 750mA.
If I hook a resistor of 5Ohms as  load to the WA then if the WA behaves like rs20 said, 
... I believe the standard that Android USB chargers adhere to states that you draw as much current as you can without having the voltage supply sag below some set limit...
and still should maintain roughly 5V output voltage, it would have to deliver 1000mA. Because it only can deliver 750mA the voltage drops. I meant in my previous post, that maintaining 5V whatever load you hook on the output of the WA is not possible because of Ohms Law. You have to maintain 5V voltage drop across a resistor with any value, which only can be achieved adapting the current trough that resistor. But because you have limited output current this will never be the case if you go below the minimum allowed load resistance of that particular WA.
Or maybe I completly misunderstood what rs20 meant :-//
 

Offline ITman496

  • Regular Contributor
  • *
  • Posts: 161
  • Country: us
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #11 on: October 07, 2013, 01:42:03 am »
In my opinion, what he said makes sense.  It's rated to 750mah.  When you exceed it, it begins to lose voltage.

What he said is that the phone will draw as much current as it can until the voltage starts to drop past probably half a volt or less, and then it'll back off slightly, and now it knows the charger's rating.

At least that's how I am interpreting it, anyway, could be wrong!
KD2CHS
 

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #12 on: October 07, 2013, 10:40:12 am »
What he said is that the phone will draw as much current as it can until the voltage starts to drop past probably half a volt or less, and then it'll back off slightly, and now it knows the charger's rating.

Exactly that thing I want to replicate with my circuit! You have put it in a nutshell, I described it to complicated I guess :)
I'd suggest using an XMEGA as well, or perhaps one of the older USB parts. It's a shame the new XMEGA E parts don't have USB, but something like the 16k A4U or A3U is really cheap. You can then enumerate as a proper USB device and request 500mA current. Keep in mind that you may not get 500mA though, for example if you are connected to an unpowered hub.

For enumeration just make a simple HID device with no actual interface. No driver required on all major operating systems.

Yes the XMega Devices are the way to go. Even if there are normal AVR devices with USB, they are far more expensive than the XMega devices. XMegas have a huge set of built in features to a fraction of the price of a normal AVR device which has less features. That's a bit strange in my opinion.
 

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #13 on: October 07, 2013, 05:01:49 pm »
Keep in mind that you may not get 500mA though, for example if you are connected to an unpowered hub.

For enumeration just make a simple HID device with no actual interface. No driver required on all major operating systems.

Yes sure. But shouldn't that be handled by the host PC.  Let's look on the following scenario:

You have a USB hub with f.e. 2 USB- Sticks already connected. So the PC should know that this USB hub already needs 2 unit loads (1 USB unit load is 100mA).
Then I come along with my charger. I plug it in (of course the charger is disabled at plugin) and the µC communicates with the USB- Host and tells it, that the charger wants to have a 500mA USB port. But the USB host already knows that 200mA are reserved so it will deny the request. If the µC's request for 500mA gets denied, it will automatically switch to 100mA charging current. There is even a dedicated pin on the charger- chip to switch between 100mA and 500mA mode.

The only case I can think of, where this behavior could be a problem, is when you use a externally powered Hub because then the charging current will also be only 100mA.

I've searched Farnell's website a bit for low- cost, low- pincount, USB-capable microcontrollers. And I found a even better match than a XMEGA which would've also been a good choice, although it would've been in a 64pin package.
I think I will use a Freescale MC9S08JS16CWJ http://www.freescale.com/files/32bit/doc/data_sheet/MC9S08JS16.pdf. That's a USB2.0 capable µC in a wide SOIC20 package. It costs 1.3€ less than that XMega, I wanted to use before. It also comes with a preprogrammed bootloader. This is a huge benefit, as I won't need to buy any proprietary programming tools (because I only have used AVRs before...). There is also a ready to use software USB stack from freescale. As what I've read you will only need to select your application (f.e. a HID device) and then copy in the source files, compile them and you're ready to go.
Appnotes
So I think that chip is exactly what I need.
For adjusting the charging current when a WA is connected, I will use a volatile digital potentiometer, instead of my first approach using PWM. This DP will directly replace the current-set resistor of the charger- chip.
« Last Edit: October 07, 2013, 05:09:18 pm by Electr0nicus »
 

Offline Electr0nicusTopic starter

  • Regular Contributor
  • *
  • Posts: 131
  • Country: at
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #14 on: October 08, 2013, 11:21:45 am »

That Freescale chip looks fine on paper, just a shame it's a Freescale. Their IDE and free tools are a bit lacking, but if it's only a simple project and you are not familiar with the AVR/XMEGA architecture already it probably doesn't matter.

I'm indeed very familiar with the AVR/XMEGA architecture. As I wrote in my last post, I've pretty much exclusively used AVRs/XMEGAs before. But I've decided to use the Freescale, because it's much more tailored to my needs. Sure the XMega can do a whole lot more, but it's a bit of a waste if I only use 20% of the pins and 10% of the functionality of a chip. As I want to sell my little project as a kit sometime in the future, cost really matters. Material costs should be < €15 (if I make a small batch of 20 units) for the whole thing, including PCB, charging circuitry and boost regulator. The battery will be optional and will add €5 to the material price. So if I sell the complete kit for €30 (excl. battery and shipping) then that would be ok for me.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: How to detect if USB-Power comes from PC or Wall- Adapter
« Reply #15 on: October 08, 2013, 11:28:41 am »
Quote
Their IDE and free tools are a bit lacking

Because they don't care about "free" folks, :)

Freescale is a huge player in the power supply mkt. Using a Freescale chip here makes tons more reason (for marketing) than any other chip, in my view.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf