A Computer in the Palm of Your Hand – Microcontrollers Explained
Microcontrollers. Look around you for a moment, beyond your PC screen. Do you have a remote control laying nearby? Maybe a TV in the corner of your room? A microwave in your kitchen, and a car parked outside? While you might not realize it, all of these objects, which look nothing like the computer that you are reading this blog post from, have a computer inside. Your interaction with computers no longer starts and stops between the time you sit down at your desk for the day, and when you leave for home. Nearly every interaction you have with this world, from driving on your daily commute to how you cook your dinner uses a computer that can fit in the palm of your hand.
Honey, I Shrunk the Computer!
So what is this super small computer? It’s a microcontroller, also called an MCU, and it’s exactly like the computer you are using right now, with a few subtle details of difference. The computer that you know is a general computer. It can do an unlimited number of tasks, depending on the use. However, a microcontroller is what we call a specialized computer. It’s used to do one task really, really well without worrying about anything else.
Take for example your car sitting outside. There’s likely a microcontroller inside of it that will read inputs from the oxygen and knock sensors when your car is running, which will then control things like the mixture of your fuel and the timing of your spark plugs. Or what about the microcontroller inside your television? It’s waiting for inputs from your remote control, and then showing you various outputs in the form of different television channels and volume adjustments.
These two examples are just a few of the unlimited number of uses for microcontrollers. The list can be exhaustive, and basically, anything that needs to have some kind of digital input and output is likely using a microcontroller behind the scenes.
Microcontrollers and Computers
We’ve said that a microcontroller is exactly like the computer you’re using right now, only smaller. But what exactly does that mean? On the surface computers, all look different, with varying sizes of monitors, keyboards, and even internal specifications. But it’s the hardware running on the inside of a computer that shows us just how much it has in common with a microcontroller, including:
- A Way to Processing Things – A microcontroller needs a way to execute programs and perform tasks through a Central Processing Unit (CPU), just like your computer.
- A Way to Store Things – A microcontroller also needs a way to load programs and store data through the use of Random Access Memory (RAM).
- A Way to Interact – You also need a way to communicate with a microcontroller, and rather than a physical keyboard and mouse, you might use LEDs, relays, sensors, and other devices.
- A Way to Stay on Track – A microcontroller needs a way to control the speed of its processor, and it does this by using an oscillator or a clock which acts as the engine to drive your MCU.
Of course, the size of all of these different pieces of hardware is much smaller than a traditional computer. I can open up the case on my desktop computer and pull out the RAM and CPU, but in a microcontroller, everything is nestled together into one small black box. And it’s because of this size that you’ll find microcontrollers being embedded in larger devices.
Take something like a washing machine as an example. There’s a ton of mechanical parts that make up the physical structure of this device, and embedded within this mechanical world is a microcontroller handling all of the digital work. In this instance, the embedded microcontroller will allow you to control specific features and actions of the washing machine.
For example, my washing machine has a feature that automatically senses the needed water level based on how many clothes are inside of it. This process likely uses a microcontroller to sense the depth of the clothes, and also the depth of the water as it’s being added to the tank. When the water level reaches the level of the clothes, the microcontroller will tell the motor controller to turn off the flow of water and begin the washing cycle.
Small Size, Small Costs
There are some benefits for opting to use a microcontroller in specific applications like my washing machine instead of a traditional computer system, and it namely boils down to:
- Saving Money – By integrating the memory, processing power, and peripherals all together on a single chip, then you wind up with a completed circuit board that needs way fewer chips assembled, help to save labor and overall costs.
- Smaller Boards – Since you can fit everything needed to make a computer run onto a single chip, you can also significantly reduce the amount of space and wiring required on a circuit board that might have been taken up by a bunch of separate components.
- Specialized Tasks – Since microcontrollers deal with only specialized applications, you have the advantage of not needing top-end power or memory performance to run these microcontrollers, which makes computing available at a fraction of the cost of a traditional computer.
We also can’t forget that many of these microcontrollers are designed to work in what can sometimes be rather hostile environments. For example, the microcontroller at work in your car likely has to deal with temperatures anywhere from -30°F in a winter in Alaska to something as scorching hot at 134°F over in Death Valley in the peak of summer.
Microcontrollers in Action
How does a microcontroller get to work, transforming inputs like temperature and light into some useful action? Much like a traditional computer, a microcontroller relies on several features to make its computing action happen, including:
Just like in a computer, RAM will be used in a microcontroller to store data and other results that are created while your microcontroller is at work. And when you cut the power on your microcontroller, the memory in your RAM gets erased.
Within the RAM in a microcontroller, you’ll also have something called a Special Function Register (SFR). This memory comes pre-configured from your microcontroller’s manufacturer and controls how specific circuits will behave, such as an analog-to-digital converter, serial communication, and more.
The specialized task that a microcontroller carries out in the form of a program will be stored on its Read-Only Memory and will generally never change. ROM is what allows the microcontroller in your television to always know that pressing the change channel button on your remote should change what’s displayed on your screen.
The size of the program that can be stored in the ROM is entirely dependent on its size. Some microcontrollers ship with built-in ROM, while others can accept ROM added as an external chip. It varies between each microcontroller.
Another feature nestled within a microcontroller is the program counter, which allows your mini computer to execute a program based on a series of programmed instructions. This counter increments by 1 each time a line of instructions is carried out which helps it to keep track of its place in your line of code.
Inputs and Outputs
Unlike your computer, which you’ll likely interface with though a keyboard and mouse, most microcontrollers have other ways that humans interact with them. Typical input and output devices on microcontrollers can include switches, LED displays, and even sensors that can measure temperature, humidity, light levels, etc.
Most of the embedded systems that you’ll encounter out in the wild don’t have a keyboard or screen that you can directly interact with because they weren’t intended for human-to-computer interaction, only machine-to-machine interaction. Because of this, microcontrollers will have up to a dozen General Purpose Input/Output Pins, or GPIO, that can be configured for various input and output devices.
For example, you might have one pin on a microcontroller configured as an input that acts as a temperature sensor. Another pin can be configured as an output and connected to your thermostat which turns on your heater or air conditioning depending on the particular input temperature range that you’ve programmed. As you can see, this input and output dynamic is purely from machine to machine without requiring direct human interaction each time it makes a decision.
Microcontrollers also have the ability to provide a real-time response to events when triggered within a larger embedded system. Take for example our washing machine. When an event occurs which signals that the water level has reached its required height, this will then trigger what’s called an interrupt. This interrupt will send a signal to the processor in your microcontroller to stop doing its current action, which would be to stop the flow of water.
This process of altering the actions of a microcontroller on the fly as it progresses through a specific sequence is called an Interrupt Service Routine, or ISR. These ISRs can come in many shapes and sizes, and it really just depends on the device a microcontroller is being used. You’ll find ISRs being used for events like internal timer overflows, completing an analog-to-digital conversion, or even putting a series of events into motion when a button is pressed.
Keeping Microcontrollers Organized
When working with microcontrollers on your own electronics projects, you’ll find them organized into several categories. These categories are all based on several variables, including the number of bits, amount and type of memory, the instruction set type, and the memory architecture used. Here are some examples:
- 8051 Microcontrollers – This category was first manufactured in 1985 by Intel and remains the most popular choice for both hobbyists and professionals and includes variations in RAM and ROM capabilities.
- Peripheral Interface Controller (PIC) Microcontrollers – This category is highly popular amongst hobbyists and industrialists and is known for its availability, low cost, and 3 architecture choices – 8-bit, 16-bit, and 32-bit.
- AVR Microcontrollers – This category is based on the Harvard 8-bit architecture and was invented in 1966 by Atmel and is also one of the first microcontrollers to use on-chip flash memory for storing programs.
- ARM Microcontrollers – This category includes a 32-bit architecture and is specially designed for microcontroller devices like your smartphone, which makes it one of the most power microcontroller categories out there, but also the most challenging to use.
You might be asking after reading about these categories, how do you know which microcontroller to select for your own project? This selection process requires consideration of a number of criteria and includes things like knowing what kind of hardware you need to interact with, how much memory you need, what kind of architecture you need, and how much you’re willing to pay. All of these considerations and more are summarized over at the ARM community in their 10 Steps to Selecting a Microcontroller blog post.
In the Palm of Your Hand
Microcontrollers are everywhere, serving as the invisible computers in our age of digital electronics and working tirelessly behind the scenes in all areas of our life. Can you count all of the devices in your immediate surroundings that you rely on that might have a microcontroller inside? You might be surprised at what you find. These computers have opened up a world of possibilities for new interactions and intelligence in otherwise common and everyday objects like washing machines, remote controls, power tools, toys, and a whole lot more. Without microcontrollers, the world we know today would never be possible.
Want to work with a microcontroller in your very own electronics project? Autodesk EAGLE includes a ton of ready-to-use microcontroller libraries all for free! Try Autodesk EAGLE for free today to get started.