Force of Habit

Sometimes we do things the hard way out of habit. Instead of thinking about a design, we just rely on things we know work. I can’t tell you how many times I’ve spent searching for a remote control to turn off a TV only to realize it would have been faster to just walk over to the TV and turn it off manually.

An old friend of mine showed me a Microchip PIC design to monitor a battery. The controller simply waited for the battery to reach the trip point and caused an indicator to blink. The battery drives a large load, so there is plenty of juice left over even at that point.

At first I didn’t think there was anything odd about what he showed me. He selected an eight-pin device with an onboard A/D. A voltage divider brought the voltage in range and the software set a digital output at the right time.

Sounds fine right? Then it occurred to me that we had both fallen into the habit trap. How do you measure voltage? With an A/D, of course. In this case, he didn’t actually need to know the voltage, just if it was under threshold. I suggested he use a digital input with the divider set to hit the input threshold.

This leads to simpler code, at least potentially. The really interesting thing, though, is to add back some code complexity and monitor the input with an interrupt pin. This might seem like overkill to use an interrupt, but using one will let the code mostly sleep. That leads to very low power consumption.

Of course, you could also argue you don’t need a microcontroller at all for something this simple. That’s true, although I’ve oversimplified the project a bit in the interest of clarity. Still, all the work only happened once the threshold tripped. Besides, the PIC in question was a PIC12F629, which is well under a dollar. Even if a comparator and an LED flasher would work, the difference in cost would be very small. Besides, there are even less expensive, small CPUs out there from Microchip and other vendors. The 12F629 just happened to be in the circuit.

This chip has an interrupt-on-change feature that can monitor I/O pins and tell you if any changed. However, for this case, a single interrupt line is sufficient. The GP2 pin serves as a general-purpose interrupt and can be set to trigger on a falling edge by setting bit 6 of the OPTION register. There’s no need for an external clock, since the PIC can use its internal one. High timing accuracy is unnecessary in this application.

About the only flaw in this scheme is that the input pin threshold may not be exact. In this case, that wasn’t critical, but there are still other tricks you can use. For example, the same PIC has a proper comparator and internal voltage reference. These operate during sleep (but do consume a bit more power) and can wake up the processor.

The moral of the story is really in two parts: First, don’t just fall back on habits when making design decisions. Second, know your hardware and think about how to apply it to the problem at hand.

As an example of my second point, think about this question: How would you do a rough (+/- 1 degree C) temperature measurement? Habit would suggest a thermistor or a thermocouple or an integrated temperature sensor. If you are really old school, you might use an A/D to read a diode temperature junction. Once you’ve got your simplest answer, read this paper, also by Microchip. Did you break your habit?