Wednesday, August 27, 2014

µBoost user guide

This is the permanent spot for the µBoost user guide.

The µBoost is a battery powered USB power supply. It can supply up to 1A @ 5V from 3 primary cells (AA, C or D) or up to 500 mA from 2 primary cells.

You can use any supply voltage as long as it is lower than 5 volts. That means that you can use a LiPoly, NiCd, NiMh or any other chemistry you like.

Use of primary cells at more than 1A of current draw is not recommended. The batteries' internal resistance will cause them to get quite hot. Additionally, the higher the current draw, the less energy the batteries will deliver before they die (the extra energy is lost as heat). This is not a problem with most secondary battery chemistries, such as NiMh, Lion or LiPoly. If you wish, you can add a battery charger in parallel with the battery and µBoost.

The quiescent current of the converter (that is, its consumption with no load) is around 140 µA, which means that without being used, a set of AA batteries would last around 2 years.

The lower the input voltage, the hotter the MOSFET, diode and inductor will get during high current operation. It is not recommended to pull 1A with a 3 volt (2 battery) supply. Even if this weren't excessive switch current for the MOSFET, it would require pulling too much current from the primary cells than is good for them. If you heat up primary or secondary cells they can leak or explode.


Theory of operation

The µBoost is built around an NCP1450 boost converter controller with an external MOSFET switch. Because the switch is external, the controller itself has no limit on how much current it can switch (or supply). The MOSFET itself has a maximum drain current rating of a massive 8 amps, but in a boost converter arrangement the switching current is generally much higher than the output current rating. The same is true of the Schottky diode, which is rated at 3 amps and a maximum forward voltage drop of 0.5 V and the inductor, which is rated for over 2 amps.

A boost converter works because an inductor resists a change in the current flowing through it by generating a voltage. The inductor is connected between the line and load. Immediately after the inductor is a switch between the inductor's output and ground (in this case, the switch is the MOSFET). When the switch is closed, the inductor is connected across the input voltage, and it is effectively "charged." When the switch is opened, the current flow through the inductor is interrupted. The inductor attempts to "correct" this state of affairs by generating a high(er) voltage. That voltage passes through the diode to the load. When the switch is closed, the diode is reverse-biased and blocks current flow the "wrong" way from the load. The output filter capacitor supplies the load during this time.

The two data lines have resistor voltage dividers. These dividers are set to provide a constant voltage to the data pins. These voltages tell Apple iDevices the ampacity of the charger. The spec for these voltages is unpublished, but was discovered by the good folks at AdaFruit.

The only difference between the 500 mA circuit and the 1A circuit is the high side D+ resistors. It is 75k for 1A of charge current and 43k for 500 mA.


Schematic

Tuesday, August 26, 2014

Low power breakthrough

I've made a few battery powered projects since I've started, and the one constant in all of them is that none of them had a power switch. All of them (that had a microcontroller), went to sleep and used either a button interrupt or a timer to wake up again.

I've discovered that all of this time I was missing a crucial power-saving step.

One of the things I'd been doing in setup() was turning off all of the excess peripherals in the PRR - the Power Reduction Register. It turns out, however, that this is insufficient to turn the A/D converter off completely. You also must (first) clear the ADEN bit in ADCSRA. Not doing so wastes around 250 µA. It doesn't sound like a lot, but for the blinky earrings, which are powered by a CR1225, that is enough to kill the battery in about a week rather than allowing it to sit powered off basically for the shelf life of the battery. In the Crazy Clock, it's enough to reduce the (estimated) battery life from 18 months or so down to 3.

So... Before you put an AVR to sleep, clear the ADEN bit in ADCSRA and then set as many bits as you can in PRR.

Friday, August 22, 2014

Measure with a micrometer, mark with chalk, cut with an axe.

I happened to see a link to this from Dave over at eevblog. TL;DR, it's a 5 ppm mechanical wristwatch for $800K.

That CNN says it keeps "perfect" time is laughable, but I'll give them that 5 ppm is certainly a high standard for a purely mechanical movement.

But it got me thinking... I've made all the crazy clocks I can think of... but just how accurate could a lavet stepper clock be?

Well, the going rate on eBay for a rubidium standard with 10 MHz output is around $180 or so. A simple firmware tweak should give us the same 10 Hz interrupt source: 10 MHz with a prescale of 16 is 625 kHz. 625 kHz divided by 1024 is 61 5/128, so 5 cycles of 62 and 123 cycles of 61 would be correct.

The result would be a lavet stepper clock that ticked with an accuracy of better than one part per billion. It would be no more than a tenth of a second off per century.

Of course, it wouldn't be synchronized to anything - it would depend on the user to actually point the hands to the right spot. But as long as it had power, you could count on it being a whole lot more accurate than a wristwatch 3.5 orders of magnitude more expensive.

You could do the same thing with GPS much cheaper, of course. You wouldn't even need a microcontroller. GPS modules have synchronized PPS outputs. You could use a flip-flop and two AND gates to turn the PPS output into alternating positive pulses on the two coil wires. If the PPS pulse isn't long enough, then you might need to add a pulse stretcher, but even then a microcontroller is still overkill.

Sunday, August 17, 2014

More crazy clock work

I actually tested the firmware with a 10 based crystal (2.000 MHz). Firstly, the power savings over 4.096 MHz wasn't really worth writing home about. Saving more power would probably require further reducing the system clock frequency from 500/512 kHz, but if you do that, then you can't use an ordinary AVR programmer with an 400 kHz SPI clock. Surprisingly, the ATTiny85 retains the programmed clock divider during RESET rather than reverting to the fused divider value. So just for my own convenience, I'm sticking with 500/512 kHz, which still results in a single AA battery lasting somewhere around 6 months, if I'm doing the math right.

Anyway, after finding and fixing a minor bug, I've confirmed that the long term accuracy with a ten based crystal instead of a 2 based crystal is still good, so my fractional OCR0A mechanism works. Yay!

However, in looking at the pulse timing on my oscilloscope, I discovered something that I think I'll have to address.

Whenever you are using semiconductors to switch a large inductor on or off (this applies with motors, relays, solenoids... anything with a big coil), you need to deal with the coil collapse voltage that will be induced the moment you switch the transistor off. The usual solution is a reverse-biased diode across the coil, which will allow the diode to conduct when the coil commutates and prevent a large negative voltage from appearing at the transistor.

What I saw on the scope was that despite the presence of the diode, there was still a -1 volt pulse when the coil was switched off. Turns out, I'm using a 1N4148, and the forward voltage drop is about 1 volt.

The issue I see is that the absolute maximum section of the ATTiny85 datasheet says that the limit for any pin is -0.5 volts.

Now, to be fair, I think Atmel really means for that to apply to pins configured as inputs. It's unclear what impact there might be to an induced negative voltage spike like that on an output pin set LOW.

This was never an issue before because in most cases, I've used a transistor to switch the power on and off, and in a typical low-side NPN switching arrangement, the flyback diode is intended to keep the voltage spike under the collector-emitter breakdown voltage, which is always much higher than any reasonable diode forward voltage rating.

Fortunately, the solution is straightforward - pick a better diode. A Schottky diode can be had with a forward voltage of only 0.5 volts, and they make them in the same SOD-323 footprint. It may not - strictly speaking - be necessary to cut that spike in half given that the pin is configured as an output rather than an input, but it's probably good engineering anyway.

Also, I've added another firmware option. This one is a bit more prankish. It runs 10% fast for 12 hours, then 10% slow for 12 hours. It makes the days just fly by! For this one, it actually matters when you put the battery in. The controller has no idea where the actual hands on the clock are pointing - it's just a pulse source. So when the battery goes in, that marks the start of the 12 hour fast period.

EDIT: I asked the flyback diode question over on StackOverflow and it appears that this is not going to be a big deal. The worst case scenario is 1 volt going through a 100 ohm resistor, which would only be a potential of 10 mA on pins designed to source or sink up to 40 mA each.

Saturday, August 16, 2014

Crazy Clock user guide

Crazy Clock store on Tindie

This page is the permanent home for the Crazy Clock user's guide. It will be updated whenever changes are made. The hardware history:

  • v0.1 - Prototype
  • v0.2 - Changed crystal to HC-49 through-hole footprint and reduced size
  • v0.3 - Slight size reduction
  • v0.4 - Slight size reduction - downgraded over-spec'd parts.
  • v0.5 - Trade out ceramic for tantalum caps in boost section. Dramatic size reduction.
  • v0.6 - Trade out flyback diodes for a single diode array - use same array (wasting one side) for boost section - reducing BOM.
  • v0.7 - Change to a SMD 32.768 kHz crystal.
  • v0.8.x - v0.9.x - All versions the same, just reorganized for smaller sizes.
  • v1.0 & Q80 v1.0 - Same as before, but Q80 variant shaped to be drop-in replacement for Quartex movements.
  • Q80 v1.1 - Move some components for relief, add solder jumper to disconnect coil for programming (if needed).
  • v1.1 & Q80 v1.2.1 - Change crystal to 1206 footprint and add loading capacitors.
The crazy clock comes as either a bare controller board which you can use to retrofit an existing clock movement or as a complete movement with the crazy clock controller installed in place of the original (boring) controller.

To retrofit an existing clock movement, carefully disassemble the movement and remove the stepper coil and controller board, which should be a single assembly that can be easily removed. The board should be on the bottom and there should be two bare contact patches which touch two contacts that come from the battery. Two other points on the board should be soldered (directly or indirectly) to the two ends of the coil wire. For best results, do not disturb the soldered joints of those wires. Instead, use a Dremmel or an Xacto knife and cut the traces leading away from the two coil wire solder patches and the two battery contact patches. Solder 30 gauge wires to each. For best results, use a dab of hot glue to secure the wires to the board as a strain relief. Reinstall the board, routing the wires carefully to avoid interfering with any moving parts of the movement. Connect the ends of the wires to the battery and clock terminals on the crazy clock controller board. Tuck the controller board in an out-of-the-way spot inside the movement, again insuring that neither it nor any of the wires will interfere with any of the movement's moving parts. Alternatively, you can route the wires outside of the movement enclosure and use foam tape to secure the controller externally (doing it this way is less stealthy, but makes reprogramming the controller much easier). There's an instructional video that shows the major points of the process.

If you're buying a clock movement to modify with the Crazy Clock, the recommended model is the Quartex Q-80. It turns out, at least at the present time, these are the movements being used by Zazzle for their clocks. The Crazy Clock fits best as shown in this picture:



The crazy clock will run for around 12 months or so on a single AA battery, depending on the particular clock movement into which it's installed. Simply replace the battery when the clock stops. Other than the changes made to how the clock ticks, actual operation of it is exactly the same as the original movement. You set the time by rotating the set knob and replace and install the AA battery in the same manner as before.

Depending on which firmware you selected, your clock will behave differently from a normal clock, but should still keep average time accurate to within 30 seconds per month (±10 ppm), so long as the clock is kept near room temperature (around 25 °C, or 76 °F). When exposed to extreme temperature swings, you can expect the accuracy to suffer.

If you change your mind, you can re-flash the firmware. You may need to disconnect (one lead of) the stepper coil from the clock terminal before you do so, however, as the stepper pins are also used for programming. The stepper coil is a low impedance load, and that might interfere with the programmer's ability to properly drive the pins. To do this with the Q80 board, you can cut the trace between the two pads of the solder jumper near the bottom right side of the board. Once programming is complete, use a blob of solder to short the two pads together again. The firmware files are available at the Crazy Clock repository on Github. You will need a pogo pin programming adapter and an AVR programmer. You'll also need the AVR gcc and binutils toolchain. Because the controller's system clock runs at approximately 32 kHz, the SPI clock for the programmer must be at least 6 times slower. This means adding either the -i or the -B option (depending on your programmer) to AVRDUDE with a value of 250. If your programmer does not support slowing the SPI clock or if it supports doing so in some other manner, you'll need to make whatever arrangements are required.

If you want to build your own Crazy Clock controller from scratch, you'll need to fuse your ATTiny before programming it. The correct fuse settings are low:0xE6, high:0xD7 and extended:0xFF. If you bought a completed crazy clock board, its fuses will have been correctly set, and changing them is not recommended. An ATTiny45 has sufficient flash memory for any of the firmware loads, and an ATTiny25 has enough for all of them except the "Crazy clock." You can build a crazy clock equivalent on a breadboard or some other equivalent prototyping system. If you do, you will either need to use a SOT-23-5 breakout board for the NCP1402, a ready-made boost converter breakout, or just dispense with the boost converter and use some other power supply. Two AA batteries in series would work fine, as would a CR2032 (but it would only last around a month).

Firmware Operating Notes

The Crazy, Early and Warpy firmware, by their nature, drift measurably from the correct time, and the controller cannot know where the actual hands of the clock are pointing at any given time or when they are moved by the set knob. For this reason, if you wish to set the clock, you should momentarily disconnect the battery to reset the controller. This will zero any drift. For the Warpy firmware, the clock will begin the fast-running 12 hour period immediately.

The first four bytes of EEPROM on the controller are the seed for the pseudo-random number generator. Bytes 5 and 6 are active if you enable the SW_TRIM option of the firmware. The values are a little-endian, two's compliment trim value in tenths of a part-per-million. Positive values slow the clock down and negative values speed it up. To obtain the calibration value, you can either observe the clock's behavior for an extended period (although with some novelty firmware this can be impossible), or you can load the special calibration firmware image. This image will output a 16.384 kHz (nominal) square wave on one of the coil pins. You can obtain the calibration value by measuring the actual frequency with a frequency counter and determining the offset from the nominal value.

Testing & Troubleshooting

Testing the controller is quite simple. Attach a two-lead, bi-color LED, such as this one, to the "clock" terminal. In fact, the leads on a 5mm LED are spaced perfectly to fit right in. For testing, don't solder the leads, just stick them in and bend them apart a little to make a temporary connection. Similarly, connect a single AA battery, or some other power source (maximum 3 volts) to the BATT connector. Once powered, you should see the LED blink red and green with whatever pattern is appropriate for the firmware loaded.

If you don't see the LED blink, use a voltmeter and check for 3.3 volts between pins 4 and 8 of the ATtiny, or on the top and bottom of the output filter caps (the two that are next to each other to the right of the battery connector). If you see power, then the problem is likely with the microcontroller (to the right of the crystal). If not, then the problem is with the switching supply (to the left of the crystal).

For problems with the power supply, check for any components that are warm or hot to the touch, or for excessive current draw from the battery. In operation, the crazy clock should draw - on average - less than 100 µA. If it's drawing more than half a milliamp, something is likely shorting out. Also, check for any components that appear discolored or misshapen - a sure sign that the magic black smoke has escaped.

For problems with the microcontroller, check for pulses on pins 5 and 6. If you see them there, but not on the LED, then check the two series resistors and diodes for opens or shorts. If you don't see pulses coming from the microcontroller, check to make sure pin 1 (RESET) is high and use a high impedance oscilloscope probe to check for 32.768 kHz oscillations on pins 2 and 3. If you see them, then if you can, try to re-flash the firmware. If it won't accept new firmware but all of the other tests pass, then the controller itself is likely bad. When attempting to program the controller, don't forget that the SPI clock must be no faster than 4 kHz.

For most clock movements, the pulses of their original controllers are around 30-35 ms long, with an amplitude of ±1.5 volts. The default configuration of the crazy clock firmware uses 35 ms for its pulse length. If your movement is significantly different, this may not work properly. If you have an oscilloscope, you can power the original movement and observe its pulses. If your movement has a second hand that moves smoothly rather than jumps from one second to the next, then it's possible that the controller is "ticking" at 8 Hz (which simulates smooth movement). The default crazy clock firmware is incompatible with these movements, but the hardware could still be used (with different firmware) to alter how these clocks function.

Theory of operation

The battery is connected to a boost converter that will increase the voltage to a regulated 3.0 or 3.3 volts from whatever voltage the battery is giving out all the way down to around half a volt or so. The boosted voltage is fed into an ATTiny45 with a 32.768 kHz crystal. Timer 0 is set up for a prescale of 64, for a counting rate of 512 Hz. The firmware will use a special fractional counting method to divide that by 51.2, resulting in timer interrupts at 10 Hz. The SW_TRIM option further "bumps" the timer one count one direction or the other periodically depending on the trim factor. The interrupts will be used to wake the CPU from sleep mode, which will minimize power consumption. The clock marks time by sleeping and then being woken up every 10th of a second.

The clock movement is based on a lavet style stepper motor. To make the second hand tick once, you must apply pulses across the coil, but each must be the opposite polarity of the last. This can be accomplished simply by pulsing one of two digital pins high (keeping the other grounded) and connecting both to the coil. To protect the controller from coil collapse voltages, a reverse biased diode to ground from each coil wire is present. Series resistors on each wire also reduce the voltage since the original working voltage of the system was the battery's 1.5 volts instead of 3.3. Most lavet stepper coils are a little more than 200 ohms, so 200 ohms added series resistance drops the voltage roughly in half.

Although the Crazy Clock is a digital device, it falls under the FCC part 15.103(h) exemption because it's battery powered and both the boost converter and microcontroller run slower than 1.7 MHz.

Schematic





Friday, August 15, 2014

A square peg in a round hole

The initial design of the crazy clock board uses a 4.096 MHz crystal because the prescaler combinations that are available are all powers of 2, and 1024 is the best one for the timer prescaler. But we want a 10 Hz basic interrupt source, and I just sort of gave up on trying to do that math.

But in figuring out the Martian clock's need to insert what amounts to a very whacky number of extra cycles, I remembered some work I did a long time ago in designing a custom programmable PLL circuit.

A lot of the programmable PLLs out there are designed with a special intermediate output to control a  prescaler that can do both divide-by-n and divide-by-n+1. So you can, for example, get a divide-by-5/divide-by-6 prescaler and your PLL will have a very low lock frequency, but still have a very minute tuning resolution because the divide ratio can effectively include fractions.

I used that to insert 99 extra counts for every 3600 by inserting an extra every 37 counts for 36 cycles, then every 36 counts for 63 cycles. 37*36+36*63 = 3600, and 36+63=99. And 24 hours becomes 24:39:36 and you're suddenly on Mars.

Well, could the same thing be done to let the basic mechanism work with a 2 MHz crystal? I can't find 2.048 MHz crystals, but a 2 MHz crystal might further reduce power consumption (going from 16 MHz to 4 MHz cut power consumption roughly in half).

Well, 2 MHz with a divide-by-4 prescaler is 500 kHz. That is, more or less, a match for the prototype's system clock. If we then set up timer 0 with a divide by 1024 prescaler, then what do we use for the CTC value?

500 kHz divided by 1024 is 488 + 9/32. Or put another way, to get 10 Hz, it's 48 and 53/64s. So if we set the CTC top to 49 for 53 cycles, and then 48 for 11, that solves the problem. The fact that some 10 Hz intervals will be 2.048 µS longer than others will be utterly insignificant.

All we have to do is replace sleep_mode() with calls to...

#define CLOCK_CYCLES (64)
// Don't forget to decrement the OCR0A value - it's 0 based and inclusive
#define CLOCK_BASIC_CYCLE (48 - 1)
#define CLOCK_NUM_LONG_CYCLES (53)

void do_sleep() {
  static unsigned char cycle_pos = 0xfe; // force a reset

  if (++cycle_pos == CLOCK_NUM_LONG_CYCLES)
    OCR0A = CLOCK_BASIC_CYCLE;
  if (cycle_pos >= CLOCK_CYCLES) {
    OCR0A = CLOCK_BASIC_CYCLE + 1;
    cycle_pos = 0;
  }

  sleep_mode();
}

Turns out Mouser has a 2 MHz crystal that conveniently fits in the footprint of the prototype board and even has a 10 ppm tolerance... but it's backordered until October. :(

Thursday, August 14, 2014

MicroBoost semi-failure

The 0.2 version MicroBoost boards came, and although they now fit properly in the Altoids gum case look-alike from Adafruit, the diode and MOSFET get super, super hot when charging an iPad. It still works just fine charging at 500 mA, but it looks like it's just not going to be able to do an amp.

I could use an B330LA-E3/61T and a SI3442CDV-T1-GE3 without changing the board. That MOSFET is the compliment to the part I use in Pi Power, and that's a buck converter capable of 2A of output power, so that gives me a good vibe. But I'm just not sure I'm all that interested.