Thursday, February 25, 2016

Fail: controlling a buck converter with PWM

I wanted a second try on my bench power supply build, something that would allow me to programmatically control the output voltage via an 'Arduino'.
The supply already has two buck converters inside the unit, both controlled by their feedback resistors, so it was only natural to try to create a programmable resistor.

Resolution

The feedback resistors are in the potentiometer with ranges a few kiloohms. Since I'd like the output voltage to vary within 0 and 30V with a 50mV precision the needed resolution is 30/0.05 = 600 steps. That means a way to vary the output with 10 bits (1024 steps). A cheap 8-bit DAC (PCF8591) would provide a resolution of only ~120mV.

We should not confuse resolution and precision, but I will break the rules for now.

Why a DAC does not work

Third, just a DAC is not sufficient as we need to have the feedback voltage adapt to the output voltage. We could feed the input through and ADC, sample it, calculate and output the correct DAC value but the latency would be ~1ms while the cheapest converter running at 100kHz is able to do it in 10 times faster. It would also keep the microcontroller busy a lot of the time with no time to draw the fancy graphics or respond to user commands.

Even if we choose that route, we would need to output a maximum of 1.25V (usual internal reference voltage) which means we would only use less than half the range. Yes, there are opamps - for scaling - or external DACs with reference voltage input but I'd like to keep this build minimal.

Digital potentiometer

There are quite a few of them on the market but they provide only 5-8 bits. Internally, they just switch a network of resistors. The also don't like high voltages on their input, generally going up only to 3.3 or 5V.

FET as a voltage controlled resistor

A select number of MOSFETS have a bigger linear region than the usual ones, but let's assume we don't have access to those. Let's pick a typical MOSFET that you would use in an Arduino project and try to work with that.

I'll jump right to the simulation results and try to explain what's happening:



V1, V3 and B1 form a PWM signal with V3 as a triangle generator (carrier) and V1 as driving main sine wave (modulator). B1 is a formula-driven power supply.
V1 is at 50Hz so we give all the components (capacitors) time to follow the output and recover, V3 and B1 work at 33.33kHz, fast enough to not make an annoying sound and slow enough that an Arduino can do it.

Here's a simulation on how the three waveforms interact, the blue trace is the triangle waveform, the green trace is the sine wave (zoomed in), the red trace is the FET gate driving voltage:



A detail of the PWM output with the sine wave zoomed out, where the red trace represents the gate voltage:



The keen eye might have noticed that we are driving the gate with 15V, which is way above what an Arduino can handle. That's because FETs like to be driven hard and don't work properly otherwise. So there's one showstopper.

Analysis

Let's go back to the main simulation and I'll walk you through all the details.



The green trace is the modulating sine wave (at 50Hz) and the full width of the window shows once period of that (20ms). The top of the window represents 100%, the bottom part is 0% modulation.

The blue trace is the output voltage, sampled at the far right of the schematic from the top of C4.

The red trace calculates the virtual resistance of the circuit by using a resistor divider formula. That means that the far left vertical axis should corresponds to the value in kiloohms (since R6 is exactly 1k).

We can see that the linear region is small and asymmetric: 7 to 8.2 milliseconds on the rising edge and 12.8-13ms on the falling edge (being generous).
The usable range, with calibration, would be for a PWM between 50% and 5%, but only on the falling edge. On the rising edge there is a discontinuity until 15% after which it becomes usable up to 40%.

If you were to put this circuit into your own analog circuit those nonlinearities would introduce a lot of noise, certainly not something you'd want in a power supply.

Circuit

The rest of the circuit is fairly straightforward :
R1 is the needed resistor for driving the gate and R2 the corresponding bleeding resistor. C1 smooths a bit the driving waveform, not too much.
I've chosen V2 to be 10V and R6 to be 1k so that the voltage divider calculations yield readable results.
R8 was chosen to be 47k as it gives a better output range and yields the maximum linear region in my testing. Probably because the current through it matches the current of the transistor in the linear region, but don't quote me on that.
IRFZ46Z was chosen because it had a low Vds (usually signals low Vgs as well) and a high internal resistance. And the fact that it was built-in into LTSpice.
C4 smooths the output voltage so that it displays nicely in the simulation.

Part 2 - trying it with a real converter


To be honest, I did this next simulation before the transistor-only one, but the message seems the same.

The circuit is based on the ubiquitous LM2596 chip and the common IRF510 transistor.
It has the same signal generating components but this time the FET gate is driven at only 10V, for a bit more realism.

The text on the left is the chip's model for Spice, stolen from a Yahoo group.



This time a more realistic load of 10 ohms is used.
You can ignore R4 and C2, those are there only for graphing purposes.
The input voltage (V2) is 10V but the results are similar for 30V.
R2, R3 and R7 were tweaked to their death until they have the best response.

The principle: R2 and R3*+R7 form the voltage divider that feeds the feedback pin on the converter. The chip cuts power when the input voltage on the feedback pin is above 1.25V and raises it when it goes below. R3 is then gradually shorted by the MOSFET, proportional to the PWM percentage.

Of course, there might be heavy oscillations between the V3 frequency (33kHz) and the chip frequency (150kHz), but without going into pole compensation and Bose Bode plots it should be ok for experimentation. Actually, some converters reduce their frequency to 25kHz in special cases, so there's something to think about.

Waveform




The green trace is the sinewave signal, the blue one is the voltage at R3 and C3.

We can see a big rise on startup when the output should actually be zero. Not sure if this is an artefact of the simulation or if it would happen in real life as well. I would bet it does. That pulse can be unhealthy for any device that's connected to it.
The pulse gets larger if the load is smaller or the input voltage is higher or the gate driving voltage is higher -that means lighter loads (like a microcontroller) would see the full input voltage (30V) for almost half a millisecond.

There are serious discontinuities at the 6ms and 14ms mark, but that's to be expected, we're doing this cheap.

What's not that great is the big spike at the 10ms mark. At this point the feedback voltage is zero volts which means the converter either has to restart or gives up trying to regulate. The overvoltage is measured to be almost 30%.

Conclusion


While not a great way to do experiments, simulation can tell you if things are going to be horribly wrong. Since it uses mostly ideal components it is also biased towards optimistic results - reality is almost always harsher.

I think there's no way to do this reliably with a minimum number of components. You'd have to give up accuracy (and safety!) or perform linearity calibrations or use some kind of additional feedback.
Forgot to mention that this kind of supply will not go lower than 1.25V.

You can use a multiplying DAC, but the complexity of that is out of scope for this article.

Or you can just use one of the many tried and tested microcontroller-based power supplies, but their complexity is an order of magnitude larger if you start to consider pre-regulators as well.

Even if you manage to build one fast and cheap you'd still have to test it only to find out it has problems with varying loads, noise or bad start-up performance. Or that it makes audible noises depending on load and output voltage. Or that in certain combinations it locks up and blows itself or your circuit under test.

To sum it up

If you are a beginner just buy a decent power supply - even a 30USD one is fine - and a decent soldering iron. These are the kind of projects you don't want to get started into unless you can deliver better than the commercial offers.
The Korad KA3005P supply is ~100EUR and offers USB control as well.
If you absolutely cannot afford any of these just scavenge an LM317 and add a potentiometer to it (no Arduino, please). It's still cheaper to just buy 5 of these.

Friday, February 19, 2016

Inside stuff - Racechip car tuning thingie

I bought my unit half a year ago, so you can say I have plenty of experience using it. It functions as a man-in-the-middle between the ECU and the injectors. I believe it alters the injectors' on-time and perhaps even the timing with regard to the TDC.

See also: http://hackcorrelation.blogspot.de/2016/04/racechip-tuning-box-part-2-reverse.html

Short review


I wanted to lower the consumption of my 3L V6 diesel engine to slightly more frugal values, don't care as much about power. Sure, some extra low-end torque would be nice as I feel my particular car is lazy in shifting up.

Well, to my surprise, the unit actually does something, so it's not just a random box a la OBD-tuners. By the way, you cannot tune an engine through OBD, the engine protocol is filtered out by a gateway. Just some diagnostics are allowed through.

The highway mileage improved from 8L/100km (30 US MPG, 35 UK MPG) to less than 7L/100km. With the default settings (the unit can be adjusted) the low-end torque has also improved, as it was pretty sluggish under 1700 rpm.
However, the city consumption went up from ~10L/100 (23 US, 28 UK) to 12-14 liters, quite a bummer. More interestingly, the engine idle consumption increase was the noticeable one, going from 0.8L/h to 1.2L/h, that's the fuel you use while stuck in traffic or at traffic lights.

Sunday, February 14, 2016

Essential Android software for power users

Similar to my Windows tools post, this one focuses on tools that make life a lot easier - on Android - or help you pinpoint and troubleshoot problems.

AdAway

If you cannot live without an ad blocker, this is probably the best one to use. Requires the F-Droid store.

Root Explorer

Quite expensive at 4 USD but I think it's worth the money - if you have root that is.
You can find files, pack/unpack archives, change permissions, move stuff around, handle multiple tabs and work with SMB and FTP shares, Google Drive, Dropbox.
There might be some free alternatives (ES File Explorer, Root Browser) around but at that time (~2012) there weren't many tools like this..

Titanium Backup

Another paid tool, even more expensive (6 USD, I might've paid less) but I've used it hundreds of times. It does require root as well.
You can back up an almost complete state of your phone/tablet and restore it to another device.
The backups can be shared between accounts, so you can restore a purchased program with complete settings on another account/device.
I run a weekly backup and just dump the complete contents of the internal and external sd cards to the NAS. Later in time I delete obsolete backups, but they are really handy when things go wrong or if/when you lose your phone.

ColorNote

A TODO app that is lightweight, free, has only the needed features and syncs across devices. I just wish it had web access.

Terminal Emulator

Kill processes, change permissions, run top or logcat and do generic *nix stuff. Hard to use without a real keyboard, but it's not the application's fault. Hacker's keyboard helps though.


Micro CPU Monitor

See in one glance whether something is eating up the CPU. Then open up the developer tools, check 'Show CPU Usage' and see who is at the top. Or open terminal emulator and start killing stuff. Has saved my battery many times in spite of hanging applications.

Nova Launcher

You've spent days setting up your desktop icons and widgets - then you install an update or switch to a another phone and everything is gone, because the new phone uses a different launcher. This is the solution to that and provides other nice features.

Samba File Server

Access your phone's storage as a network share. Handy if you don't want to connect it to a computer or you have automatic backup scripts running on a NAS machine.

Soft Lock Screen

Remove the wear and tear on that power button.

Honorable mentions:
Hacker's Keyboard - add arrows, modifiers and function keys to the keyboard
Alarm Clock xTreme - if you have trouble waking up this is for you
Pocket - share web pages between devices. Can cache them for offline viewing
RealCalc - a decent scientific calculator
SecondScreen - adjust HDMI output parameters: resolution, overscan, DPI, ...
Trickster Mod - play with max/min CPU frequencies and schedulers, see actual sleep time
Google Translate - the picture translate mode is really useful, when it works. Real-world scenario:

VpnRoot - can create a VPN connection without enforcing security on your phone
QuickDic - an offline dictionary, useful when abroad with no internet connection.

Thursday, February 4, 2016

I2C, Energia, Stellaris Launchpad and HT21D

A friend asked me to help him connect the HT21D temperature and humidity sensor to a TI board.
He's using the TM4C123 board while I'm using the older LM4F Launchpad.

The first thing to know is that neither of the boards have pullups adequate for I2C. You need to add a 4.7k resistor from SDA to VCC and another one from SCK to VCC. I've tested it with 1k resistors and it works with those as well.

The second thing is that the Stellaris boards have several I2C modules, so you need to select one. Even worse is the fact that the Energia library by default chooses the Boosterpack:


if(i2cModule == NOT_ACTIVE) {
  i2cModule = BOOST_PACK_WIRE;
}

So you first need to go into this folder "\hardware\lm4f\libraries\Adafruit_HTU21DF_Library-master\" and edit two files as explained below.

Adafruit_HTU21DF.cpp
From:
boolean Adafruit_HTU21DF::begin(void) {
  Wire.begin();

  reset();
To:
boolean Adafruit_HTU21DF::begin(int moduleNo) {
  Wire.setModule(moduleNo);

  reset();

Adafruit_HTU21DF.h
From:
  boolean begin(void);
To:
  boolean begin(int moduleNo);

In the example file
From:
void setup() {
  Serial.begin(9600);
  Serial.println("HTU21D-F test");
  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}
To:
void setup() {
  Serial.begin(9600);
  Serial.println("HTU21D-F test");
  if (!htu.begin(0)) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
}
Instead of begin(0) you can choose whichever I2C module you'd like, the LM4F has 4 of them.
The Wire::begin() function does not need to be called since the setModule() function will call it. But if you want to use the device as a slave, you need to call begin(slaveAddress).


Fixing an RF socket, coffee machines and other stuff

This is somewhere between a tutorial and a rant about the X2 self-healing capacitors.



The above cap seems fine and measures within spec but the appliance it's set into does not function correctly. In this case it was an RF socket from Aldi operating on 433Mhz. The LED would turn on but the load wouldn't. There was only a small click from the relay. From a set of 4 pieces, 3 have failed in the same way.
Sometimes, switching to a lighter (LED bulb) or heavier (washing machine) load would make the problem go away for a few weeks or even months.

A [Philips] Senseo Coffee machine failed to turn on - the red light would turn on for less than a second and then switch itself off.

A Philips drip coffee maker would turn on initially but turn itself off when starting to heat the water.

The common issue in all these cases was the X2 cap, replacing it cured the problem. So if you have the above symptoms just try swapping the capacitor with a new one and see if it fixes the issue. If you would like to understand why, read on.