How to detect if USB-Power comes from PC or Wall- Adapter - Page 1
Forum section: electronics / projects

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 03, 2013, 02:12:47 pm
content: HiI'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.pdfThis 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?

author: TMM
author_rank: Frequent Contributor
timestamp: October 03, 2013, 02:31:02 pm
content: This may be of some interest to youhttp://www.skyworksinc.com/uploads/documents/Automatic_USB_Detection_AppNote_202742Apdf.pdfAlso, 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.

author: ConKbot
author_rank: Super Contributor
timestamp: October 03, 2013, 07:51:13 pm
content: Quote from: mojo-chan on October 03, 2013, 02:22:09 pmHaving been futzing around with configurations for serial consoles on a PC at work today I find lulz in the ^H

author: zapta
author_rank: Super Contributor
timestamp: October 04, 2013, 04:52:32 pm
content: 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-chargingThere are also special cables that will do it for you. E.g.https://dangerousthings.com/shop/android-fast-charge-usb-cable/

author: NiHaoMike
author_rank: Super Contributor
timestamp: October 05, 2013, 01:27:18 am
content: 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.

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 06, 2013, 08:45:42 am
content: Thanks to your replies, and sorry for the late reply, I was busy with university stuffSo to summarize things:If a computer is connectedMost computers will have 15kOhm resistors connected from the data lines to GNDApple uses some resistive divider which puts a weak voltage on the data pinsIf a Charger/Wall- adapter is connectedMost chargers will short the data lines togetherSome also let them openOk 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/3241QuoteOk 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.

author: rs20
author_rank: Super Contributor
timestamp: October 06, 2013, 10:16:16 am
content: 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.

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 06, 2013, 12:35:18 pm
content: Quote from: rs20 on October 06, 2013, 10:16:16 amMaybe 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.QuoteI 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.QuoteHere you go (click on image to enlarge):

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 06, 2013, 02:09:52 pm
content: 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 haveThe 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!

author: dannyf
author_rank: Super Contributor
timestamp: October 06, 2013, 03:00:54 pm
content: QuoteNot 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.

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 06, 2013, 03:43:24 pm
content: Quote from: dannyf on October 06, 2013, 03:00:54 pmI 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,Quote from: rs20 on October 06, 2013, 10:16:16 amand 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

author: ITman496
author_rank: Regular Contributor
timestamp: October 07, 2013, 01:42:03 am
content: 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!

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 07, 2013, 10:40:12 am
content: Quote from: ITman496 on October 07, 2013, 01:42:03 amExactly that thing I want to replicate with my circuit! You have put it in a nutshell, I described it to complicated I guessQuote from: mojo-chan on October 07, 2013, 08:36:02 amYes 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.

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 07, 2013, 05:01:49 pm
content: Quote from: mojo-chan on October 07, 2013, 08:36:02 amYes 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 MC9S08JS16CWJhttp://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.AppnotesSo 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.

author: Electr0nicus
author_rank: Regular Contributor
timestamp: October 08, 2013, 11:21:45 am
content: Quote from: mojo-chan on October 08, 2013, 09:55:32 amI'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.

author: dannyf
author_rank: Super Contributor
timestamp: October 08, 2013, 11:28:41 am
content: QuoteBecause 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.