Let’s suppose that you have a greenhouse where the temperature must never fall below freezing. You set up a temperature sensor, and you have two different heaters. You want to switch on the first heater if the temperature falls below 38° Fahrenheit. But if, for some reason, that heater is broken, you want to switch on the second, backup heater when the temperature goes below 36° Fahrenheit.
Programming a MCU to take care of this can be very simple indeed. You could even add extra features, such as a second temperature sensor, just in case the first one fails, and you could tell the chip to use whichever sensor gives a lower reading.
Another application for a MCU would be in a fairly elaborate security system. The chip could monitor the status of various intrusion sensors, and can take various preprogrammed steps, depending on the sensors’ status. You could include delay intervals, too.
Many MCUs have additional useful features built in, such as the ability to control servo motors that turn to a specific angle in response to a stream of pulses. Servos are widely used in radio-controlled model boats, airplanes, and hobby robotics.
Perhaps you are now wondering why, if MCUs can do all this, haven’t we been using them all along? Why did I spend so much time describing the development of an alarm system using discrete components, if one chip could have done everything?
There are three answers:
1. MCUs cannot do everything. They need other components to help them interact with the world, such as transistors, relays, sensors, and amplifiers. You need to know how those things work, so that you can make intelligent use of them.
2. MCUs can introduce their own kinds of problems and errors, associated with using software in addition to hardware. I’ll have more to say about this later.
3. MCUs have limits and restrictions, most obviously their requirement for a 5-volt regulated power supply, and their inability to source or sink much current from each pin. They also demand that you learn a programming language (which differs from one brand of MCU to the next). And to get the program into the chip, you have to be able to plug it into a computer and do a download, which is not always convenient.
In this experiment, you’ll learn how to write a program for a small and simple MCU, and you’ll transfer the program into it and see how it works.
Background
Origins of programmable chips
In factories and laboratories, many procedures are repetitive. A flow sensor may have to control a heating element. A motion sensor may have to adjust the speed of a motor. Microcontrollers are perfect for this kind of routine task.
A company named General Instrument introduced an early line of MCUs in 1976, and called them PICs, meaning Programmable Intelligent Computer—or Programmable Interface Controller, depending which source you believe. General Instrument sold the brand to another company named Microchip Technology, which owns it today.
“PIC” is trademarked, but is sometimes used as if it’s a generic term, like Scotch tape. In this book, I’ve chosen a range of controllers based on the PIC architecture. They are licensed by a British company named Revolution Education Ltd., which calls its range of chips the PICAXE, for no apparent logical reason other than that it sounds cool.
I like these microcontrollers because they were developed originally as an educational tool and because they are very easy to use. They’re cheap, and some of them are quite powerful. Despite their odd name, I think they’re the best way to get acquainted with the core concepts of MCUs.
After you play with the PICAXE, if you want to go farther into MCUs, I suggest the BASIC Stamp (which uses a very similar language, but with additional powerful commands) and the very popular Arduino (which is a more recent design, packed with powerful features, but requires you to learn a variant of C language to program it). I’ll say more about these chips later.
If you search for “picaxe” on Wikipedia, you’ll find an excellent introduction to all the various features. In fact, I think it’s a clearer overview than you’ll get from the PICAXE website.
Supplies
Figure 5-122 shows some of the chips in the PICAXE range. I’ll be telling you how to use the smallest—the 08M—which costs less than $5 and is cheaper than any other MCU that I’ve found. It has only 256 bytes of memory to store a program (not gigabytes, megabytes, or kilobytes, just 256 bytes!), but you’ll be surprised how many possibilities this amount allows for. Figure 5-123 shows a closeup of the 08M with its legs safely embedded in a piece of conductive foam.
Figure 5-122. A page from the PICAXE catalog lists only some of the chips that are available. What began as an educational aid has become a useful prototyping tool.
In the United States, there are three distributors of this chip:
http://www.advancedmicrocircuits.com
http://www.phanderson.com/picaxe
http://www.sparkfun.com
I like P. H. Anderson for its grass-roots hobbyist approach, and they have good prices if you want to buy multiple chips. But SparkFun Electronics offers other associated products that you may find interesting.
All the distributors will want to sell you “starter kits,” such as the one in Figure 5-124, perhaps because the PICAXE itself is so cheap that it doesn’t offer much of a profit margin. Still, for our purposes, you should buy the chip as a standalone item. And buy two of them, just in case you damage one (for example, by connecting voltage to it incorrectly).
Figure 5-123. When supplied by one of its American distributors, a PICAXE 08M arrives embedded in a little square of conductive foam. The chip is the same size as