How Microcontrollers Work
Microcontrollers are hidden inside a surprising number of products these days. If your microwave
oven has an LED or LCD screen and a keypad, it contains a microcontroller. All modern
automobiles contain at least one microcontroller, and can have as many as six or seven:
The engine is controlled by a microcontroller, as are the anti-lock brakes, the cruise control and
so on. Any device that has a remote control almost certainly contains a
microcontroller: TVs, VCRs and high-end stereo systems all fall into this category.
Nice SLR anddigital cameras, cell phones, camcorders, answering machines, laser
printers, telephones (the ones with caller ID, 20-number memory, etc.), pagers, and feature-
laden refrigerators, dishwashers, washersand dryers (the ones with displays and keypads)... You
get the idea. Basically, any product or device that interacts with its user has a microcontroller
buried inside.
In this article, we will look at microcontrollers so that you can understand what they are and how
they work. Then we will go one step further and discuss how you can start working with
microcontrollers yourself -- we will create a digital clock with a microcontroller! We will also build a
digital thermometer. In the process, you will learn an awful lot about how microcontrollers are
What is a Microcontroller?
A microcontroller is a computer. All computers -- whether we are talking about a personal desktop
computeror a large mainframe computer or a microcontroller -- have several things in common:
All computers have a CPU (central processing unit) that executes programs. If you are
sitting at a desktop computer right now reading this article, the CPU in that machine is executing
a program that implements the Web browser that is displaying this page.
The CPU loads the program from somewhere. On your desktop machine, the browser
program is loaded from the hard disk.
The computer has some RAM (random-access memory) where it can store "variables."
And the computer has some input and output devices so it can talk to people. On your
desktop machine, the keyboard and mouse are input devices and the monitor and printer are
output devices. A hard disk is an I/O device -- it handles both input and output.
The desktop computer you are using is a "general purpose computer" that can run any of
thousands of programs. Microcontrollers are "special purpose computers." Microcontrollers do
one thing well. There are a number of other common characteristics that define microcontrollers.
If a computer matches a majority of these characteristics, then you can call it a "microcontroller":
Microcontrollers are "embedded" inside some other device (often a consumer product)
so that they can control the features or actions of the product. Another name for a microcontroller,
therefore, is "embedded controller."
Microcontrollers are dedicated to one task and run one specific program. The program is
stored in ROM(read-only memory) and generally does not change.
Microcontrollers are often low-power devices. A desktop computer is almost always
plugged into a wall socket and might consume 50 watts of electricity. A battery-operated
microcontroller might consume 50 milliwatts.
A microcontroller has a dedicated input device and often (but not always) has a
small LED or LCD display for output. A microcontroller also takes input from the device it is
controlling and controls the device by sending signals to different components in the device. For
example, the microcontroller inside a TV takes input from the remote control and displays output
on the TV screen. The controller controls the channel selector, the speaker system and certain
adjustments on the picture tube electronics such as tint and brightness. The engine controller in a
car takes input from sensors such as the oxygen and knock sensors and controls things like fuel
mix and spark plug timing. A microwave oven controller takes input from a keypad, displays
output on an LCD display and controls a relay that turns the microwave generator on and off.
A microcontroller is often small and low cost. The components are chosen to minimize
size and to be as inexpensive as possible.
A microcontroller is often, but not always, ruggedized in some way. The microcontroller
controlling a car's engine, for example, has to work in temperature extremes that a normal
computer generally cannot handle. A car's microcontroller in Alaska has to work fine in -30
degree F (-34 C) weather, while the same microcontroller in Nevada might be operating at 120
degrees F (49 C). When you add the heat naturally generated by the engine, the temperature can
go as high as 150 or 180 degrees F (65-80 C) in the engine compartment. On the other hand, a
microcontroller embedded inside a VCR hasn't been ruggedized at all.
The actual processor used to implement a microcontroller can vary widely. For example, the cell
phone shown on Inside a Digital Cell Phone contains a Z-80 processor. The Z-80 is an 8-
bit microprocessordeveloped in the 1970s and originally used in home computers of the time. The
Garmin GPS shown in How GPS Receivers Work contains a low-power version of the Intel
80386, I am told. The 80386 was originally used in desktop computers.
In many products, such as microwave ovens, the demand on the CPU is fairly low and price is an
important consideration. In these cases, manufacturers turn to dedicated microcontroller
chips -- chips that were originally designed to be low-cost, small, low-power, embedded CPUs.
The Motorola 6811 and Intel 8051are both good examples of such chips. There is also a line of
popular controllers called "PIC microcontrollers" created by a company called Microchip. By
today's standards, these CPUs are incredibly minimalistic; but they are extremely inexpensive
when purchased in large quantities and can often meet the needs of a device's designer with just
one chip.
A typical low-end microcontroller chip might have 1,000 bytes of ROM and 20 bytes of RAM on
the chip, along with eight I/0 pins. In large quantities, the cost of these chips can sometimes be
just pennies. You certainly are never going to run Microsoft Word on such a chip -- Microsoft
Word requires perhaps 30 megabytes of RAM and a processor that can run millions of
instructions per second. But then, you don't need Microsoft Word to control a microwave oven,
either. With a microcontroller, you have one specific task you are trying to accomplish, and low-
cost, low-power performance is what is important.
Using Microcontrollers
In How Electronic Gates Work, you learned about 7400-series TTL devices, as well as where to
buy them and how to assemble them. What you found is that it can often take many gates to
implement simple devices. For example, in the digital clock article, the clock we designed might
contain 15 or 20 chips. One of the big advantages of a microcontroller is that software -- a small
program you write and execute on the controller -- can take the place of many gates. In this
article, therefore, we will use a microcontroller to create a digital clock. This is going to be a rather
expensive digital clock (almost $200!), but in the process you will accumulate everything you
need to play with microcontrollers for years to come. Even if you don't actually create this digital
clock, you will learn a great deal by reading about it.
The microcontroller we will use here is a special-purpose device designed to make life as simple
as possible. The device is called a "BASIC Stamp" and is created by a company called Parallax.
A BASIC Stamp is a PIC microcontroller that has been customized to understand the BASIC
programming language. The use of the BASIC language makes it extremely easy to create
software for the controller. The microcontroller chip can be purchased on a small carrier board
that accepts a 9-volt battery, and you can program it by plugging it into one of the ports on your
desktop computer. It is unlikely that any manufacturer would use a BASIC Stamp in an actual
production device -- Stamps are expensive and slow (relatively speaking). However, it is quite
common to use Stamps for prototyping or for one-off demo products because they are so
incredibly easy to set up and use.
They are called "Stamps," by the way, because they are about as big as a postage stamp.
The specific BASIC Stamp we will be using in this article is called the "BASIC Stamp Revision D".
The BASIC Stamp Revision D is a BS-1 mounted on carrier board with a 9-volt battery holder, a
power regulator, a connection for a programming cable, header pins for the I/O lines and a small
prototyping area. You could buy a BS-1 chip and wire the other components in on a breadboard.
The Revision D simply makes life easier.
You can see from the previous table that you aren't going to be doing anything exotic with a
BASIC stamp. The 75-line limit (the 256 bytes of EEPROM can hold a BASIC program about 75
lines long) for the BS-1 is fairly constraining. However, you can create some pretty neat stuff, and
the fact that the Stamp is so small and battery operated means that it can go almost anywhere.