Sunday, December 11, 2016

Bitbanging the Quick Charge 2.0 - Without any ICs!

Qualcomm's Quick Charge 2.0 sounds like a cool technology: USB power supply can output 9V or 12V, if QC2.0 compatible device requests so. I have one power supply with QC 2.0 compatible port, but what I don't have is a QC 2.0 compatible device that could take the advantage of that port.
Initially I started researching the QC 2.0 technology, hoping that I could find an IC with simple interface that could be used to request the power supply to output selected voltage. Then I found out that doing those requests is easier than I thought and it could be done without any "QC 2.0 compatible magic ICs".


Information below worked in my case, but I cannot promise it will work in other cases. If it works, the USB-port starts outputting 9V or 12V, which may break devices connected to that port. If it doesn't work, voltages applied to D+ and D- lines may break something in the power supply. I don't recommend doing following things based on information I've shared, but if you are still going to do it, you are doing it at your own risk.

Quick Charge 2.0 Handshake

Handshaking between the power supply and the device is required before the power supply can take requests from the device.
Both the handshake and requests to change the output voltage happen by changing the voltages on D+ and D- lines. Handshaking has two simple steps:
  1. Device applies 0.325V-2V to D+ -line. During this time PSU may keep D+ and D- shorted.
  2. After D+'s voltage has stayed on above range for 1.25s, power supply discharges voltage on D- through a pull-down resistor. Device should allow the voltage on D- -line to drop below 0.325V, for example by keeping D- disconnected.

Requests to change the voltage

After the hanshake has been done, device can request other output voltages by applying following voltages to D+ and D- lines:

D+ voltage D- voltage Output
0.325-2V 0.325-2V 12V
>2V 0.325-2V 9V
0.325-2V GND 5V

If the voltage on  D+ drops below 0.325V, the power supply will consider device as disconnected and go back to default mode of outputting 5V. Handshake is required in order to change the output voltage again.


I made simple test circuit out of 3.3V regulator, few resistors and three jumpers. Two resistors form a voltage divider that outputs 1.1V and first jumper connects that to D-.
Another two resistors form  another similar voltage divider and second jumper connects the output to D+. Third jumper increases the output of the voltage divider that can be connected to D+ to above 2V.
I don't have a way to connect D- to ground, but disconnecting the D+ is another way to go back to 5V output.

Sources of information

The simplest description of using QC2.0 including above table was found in pages 6 and 7 of this Texas Instruments' PDF:
That PDF references this datasheet, which contains same information in more technical form:

Wednesday, November 16, 2016

Dimming with regular power switch - DIY 3-step dimming / DoRS


Light bulbs that are dimmable with regular light switch have many names depending on a manufacturer: DoRS (Dim on Regular Switch), 3-step dim, Free dimming, 4-step dim... They all have the same main concept: Switch lamp on and turn it off and quickly on again to switch the dimming level.
This same concept is used in flashlights which have multiple modes, usually couple different brightness levels and some kind of blinking mode.


To use power switch as an input for a microcontroller is easier than some might think. Connecting a resistor (lets say 100k) between GPIO pin and positive side of capacitor (maybe 47uF?) and connecting negative side of the capacitor to ground is all the hardware that is needed.

So, how it works?

Above is digram of voltages over time. Red line is VCC of the microcontroller, which is first turned ON and then briefly OFF and ON again. Blue line is voltage of the capacitor connected to one of the GPIO pins through a resistor. When microcontroller is powered on (points A and C) it reads state of the pin where the capacitor is connected. At A it reads as LOW and microcontroller knows it was powered off for a while. GPIO pin will be changed to output HIGH, which makes the capacitor start charging until it reaches the VCC voltage. That is the normal state, where nothing else will happen automatically.

After switching off the VCC, capacitor will discharge through the resistor and the clamping diode inside the microcontroller internal input circuit. If VCC is switched on before capacitor's voltage reaches lower limit where microcontroller reads input as high, microcontroller will read the state of the pin at start up (at C on the diagram) and it will be HIGH. That tells the microcontroller that power was switched off only for a short duration and it will change into secondary state (dimmed LED).

But that's just two steps, not three!

One GPIO pin with resistor and capacitor gives two step dimming: One after switching the power on and second after cycling the power off and on again. Adding third (and then fourth, fifth...) step can be done in couple of ways. I chose to add another resistor and capacitor to second GPIO. I start to charge the capacitor only after the first power cycle has been detected. Another way would be to store current state in EEPROM, read it back after switching the power on and changing to next state if the GPIO reads as  HIGH.


Here is my demonstration video. I used single LED, but it could be replaced with mosfet and multiple LEDs to create lamp that is more usable in real life.


Below is the code used in the demonstration. It's something I put together on a minute or so, but it doest the job!
If the code doesn't show up, here is the link to the PasteBin: