Unlike microprocessors, microcontrollers can be used all by themselves.

If you’re building a computer, then go ahead and use a microprocessor. But almost anything else that’s not entirely mechanical will probably need a microcontroller. Maybe even several. If it has buttons, then it can use a microcontroller. If it monitors temperature, or light, or position, then it can use a microcontroller. If it records information, or drives a display, or communicates with another device, then it can use a micro controller.

A microcontroller is really just a single chip containing a microprocessor, memory, and input and output ports. Most microcontrollers will have more components such as analog-to-digital converters, comparators, pulse-width modulation, and communications.

What you’re not going to find on a microcontroller are address lines or data lines. These are all contained inside the microcontroller. What’s exposed to the outside world is ready for you to use.

Listen to the full episode to learn more about the additional features as well as some examples of how you can use a microcontroller. You can also read the full transcript below.

Transcript

If you’re building a computer, then go ahead and use a microprocessor. But almost anything else that’s not entirely mechanical will probably need a microcontroller. Maybe even several. If it has buttons, then it can use a microcontroller. If it monitors temperature, or light, or position, then it can use a microcontroller. If it records information, or drives a display, or communicates with another device, then it can use a microcontroller.

Make sure to listen to the previous episode about microprocessors. I try not to repeat things as much as possible and instead will refer you to other episodes. I’m going to compare microcontrollers with microprocessors to help you understand microcontrollers better.

A microcontroller is really just a single chip containing a microprocessor, memory, and input and output ports. Most microcontrollers will have more components such as analog-to-digital converters, comparators, pulse-width modulation, and communications.

What you’re not going to find on a microcontroller are address lines or data lines. These are all contained inside the microcontroller. What’s exposed to the outside world is ready for you to use.

Let’s say that you want to control some lights by turning them on and off in some interesting pattern. All you have to do is figure out which ports you want to use and connect each port to a light. This assumes that the lights can be driven directly from the microcontroller. If the lights need more current than the microcontroller can supply, then you might need to use other components such as transistors. I’ll explain transistors in a future episode.

Each port is a pin on the microcontroller and can be configured to be an output port or an input port. To control things such as lights, you’ll want to use output ports. If you want to monitor a switch or a button to know when somebody pushes it, then you can can connect the button to a port configured as an input port.

The biggest difference between ports and data lines relates to timing. Data lines on a microprocessor constantly send and receive data that has to be timed exactly. Ports though maintain their values when configured for output until your code decides to change the value. And input ports are ready to handle that button press at any time.

You can connect a microcontroller to the lights so that when a port value is high, then the light is on and when the port value is low, then the light is off.

What about all those other features that microcontrollers usually have included?

First let’s look at the memory. There’ll be two different kinds of memory. One is flash memory. That’s where the microcontroller holds the code that you write. You program a microcontroller just like a microprocessor. But instead of an operating system running on a computer that can load your application off a hard drive along with several other programs, a microcontroller will be running a single program. This program is stored in flash memory so it stays put even when power is turned off.

The other type of memory is normal random access memory just like in a computer. Anything stored in this memory remains only as long as the microcontroller has power.

Maybe you want to read some voltage that can change slightly. You can read a voltage from a potentiometer and maybe increase or decrease the speed that the lights cycle through their pattern. Listen to episode 181 about voltage dividers for more information about potentiometers.

Microcontrollers are digital and don’t internally understand voltages that can be adjusted gradually like this. So to get around this, most microcontrollers have one or more port pins that can operate as analog-to-digital converters. This means that the pin can accept any voltage between a reasonable range and convert that to a binary value that the microcontroller can use.

Maybe you’re building something like a thermostat and you want the user to be able to set a desired temperature and then turn a heater on if the actual temperature falls below the set temperature. That’s where you can use comparators to compare inputs and let your code know which one is bigger and which one is smaller.

If you’re into robotics, then you can use microcontrollers to cause the robot to move. Most robots use special motors called servos. These servos are not like regular DC motors. They don’t just start turning when you apply power. You have to send them power, sure, but you also have to send them a special signal called a pulse width modulated signal. You can create this signal on any output port but you have to carefully control the timing as the signal goes from low to high over and over again. The servo looks at the ratio of how long the timing signal remains high to how long it remains low and uses this ratio to either control its speed or its position. This is what gives robots their precise control. But it’s also difficult and takes up a lot of the microcontroller’s time to constantly manage this type of signal. That’s why some micro controllers do this for you.

Finally, I’ll mention communications. This is another highly useful feature of many microcontrollers that allows them to communicate with other microcontrollers and some other specialized chips. Just like letting the microcontroller handle pulse width modulation helps to simplify your code, the same thing can be said for communication. Sending and receiving data over a couple communication pins is something that the microcontroller can do for you. All you have to do is decide what data to send and let the microcontroller send it. And for receiving data, you just let the microcontroller handle the receipt and then you just need to decide what to do with the data you received.

Communicating with other chips is what microprocessors do best with their address and data lines. But microcontrollers with dedicated communications ports are not far behind.