Introduction to Microcontrollers
Discover what a microcontroller is, how it differs from a microprocessor, and the critical specifications you need to understand in order to pick the best one for your specific product application.
Almost every electronic product needs a “brain” of some sort to control the various product functions. But what “brain” is best for your specific hardware product?
Start by determining if you need a microcontroller unit (MCU) or a microprocessor unit (MPU). Nearly all electronic products use one of these two types of processor chips, and some products use both.
Is your product complex with a need to process significant amounts of data? Does your product require an operating system such as Android or Linux? If so, then you probably need to use a microprocessor.
Or is your product moderately simple without a need for high speed processing or an operating system? Then, you will most likely employ a microcontroller. A microcontroller is a fairly simple computer integrated onto a single microchip.
This single chip will contain a Central Processing Unit (CPU), memory and other peripherals. Having everything integrated on a single chip greatly simplifies designing with microcontrollers compared to microprocessors.
You will commonly see the term System on a Chip (SoC) used to describe microchips with a high level of integration such as microcontrollers.
Below is a table summarizing the key differences between microcontrollers and microprocessors:
|Memory||Small / Internal||Large / External|
|Number of bits||8-32 bits||32-64 bits|
Table 1 – Comparison of a microcontroller versus a microprocessor
Arduino is a the most popular development kit based on a microcontroller. Most of the Arduino models available are based on an 8-bit Atmel AVR microcontroller.
Figure 1 – Arduino development kit
Raspberry Pi is an example of a development platform based on a microprocessor. It’s a full computer system running an advanced Linux operating system. You can connect a keyboard and display to use a Raspberry Pi just like any other computer.
Figure 2 – Raspberry Pi development platform
Designing a custom microprocessor based circuit is many times more complex than designing with a microcontroller. A microprocessor will have high-speed RAM memory that is external to the microprocessor chip itself. This requires very careful layout of the PCB connecting these high-speed signals.
Microprocessors run at much higher clock speeds which adds a lot of complications. For example, the Raspberry Pi 3 is based on a 64-bit quad-core processor running at 1200 MHz, whereas most versions of the Arduino are based on a 8-bit processor running at only 16 MHz
Because of their high speed clocks microprocessors are more likely to cause issues with undesired radio frequency transmission thus complicating FCC certification.
For these reasons, if your product requires a microprocessor it’s almost always best to use a pre-certified module solution. These are called a CPU module or a SOM (System On a Module).
On the other hand, most microcontroller based designs are simple enough to use a System on a Chip (SoC) solution.
In most cases, a microcontroller SoC solution will be an order of magnitude lower cost than a microprocessor SoM solution. A microcontroller SoC can cost as little as a dollar, whereas an ultra-high performance microprocessor SoM (for example, the Nvidia Jetson TX2) will cost hundreds of dollars.
Common Microcontroller Features
Before choosing the microcontroller for your product, you should create a block diagram showing all of the various functionality required for the full system. A block diagram is one part of what I call a product pre-design. I always recommend starting off with a pre-design so you can see the bigger picture before you begin designing the actual schematic.
What needs to connect to the microcontroller? What communication protocols will they require? How many general purpose input/output (GPIO) pins are needed? How many communication ports are needed?
Figure 3 – Block diagram example
You need this information before you can select the microcontroller. There are literally thousands of microcontrollers available. You don’t want to select one with more performance or features than you’ll ever need for your product. Doing so, will only unnecessarily complicate your product’s design.
On the other hand, you don’t want to box yourself in with too limited of a microcontroller either. You may want add new product features in the future so you need a microcontroller that gives you a little room to grow if necessary.
Let’s look in more detail at the most common features and peripherals found on microcontrollers:
Types of memory
RAM and FLASH memory are already built into most microcontrollers. RAM memory is for temporary data storage. RAM is considered volatile memory because it loses its contents once power is turned off.
FLASH is non-volatile memory. It retains its contents even with power turned off. FLASH is primarily used for storing the program (called firmware).
EEPROM (Electrically Erasable Read Only Memory) is also non-volatile memory, but it’s typically used for storing small amounts of data such as configurations and variable states. EEPROM has the advantage of allowing erasure on a per byte basis, whereas FLASH memory only permits an entire block, or page, of bytes to be erased.
Digital I/O (GPIO)
Digital inputs and outputs on microcontrollers are called General Purpose Input Output or GPIO. GPIO pins are logic level pins (either high or low) used for input or output. They can generally drive loads up to a few tens of milliamps.
Keep in mind that it’s usually necessary to have some sort of external drive circuit that will off load the drive requirements. Additionally, all the pins added together must be under the maximum allowed total current.
Make sure to consult the data sheet when deciding what pins to use because many internal functions are not available on certain pins. You also have to adhere to specific mapping limitations.
Mapping means setting which pins serve which function. Most pins on microcontrollers have multiple possible functions to give hardware designers the most flexibility.
Analog inputs and outputs
In addition to digital I/O, most microcontrollers can also accurately measure an analog voltage, and some have the ability to output a precise analog voltage.
Analog voltages are measured by the microcontroller using an embedded specialty circuit called an Analog to Digital Converter (ADC).
There are two methods for outputting an analog voltage from a microcontroller. The most precise, but more complex method is to use a Digital to Analog Converter (DAC) circuit. There are numerous microcontrollers available that include a built-in DAC for this purpose, but many don’t.
The second method, available on most microcontrollers, is to use a Pulse Width Modulation (PWM) generator. A PWM is a digital signal that oscillates between a high and low level. The microcontroller is able to control the percentage of time this signal is high as determined by the firmware program.
The PWM signal can then be fed through a low-pass filter to convert it to an analog voltage. By controlling the percentage of time the PWM signal is high versus low, you can accurately control the analog voltage level on the output side of the filter.
Universal Asynchronous Receiver Transmitter (UART)
The oldest method of serial communication still used today in microcontrollers is called UART. UART’s have been around since the early 1970’s. As the name implies, UART is an asynchronous protocol, meaning there is no clock signal to synchronize the signal timing. The simplest form of UART uses only two signal lines: receive (RX) and transmit (TX).
Synchronous communication supports a much faster data transfer rate. Asynchronous communication must add synchronization bits to the data. Sending these extra synchronization bits increases the overhead and therefore reduces the effective data transfer rate.
The downside of synchronous communication is that it requires an extra wire for the clock signal. It also necessitates a master/slave configuration since the master device must be the one to generate the clock signal.
Personally, for obvious reasons, I’m not a fan of the terminology master and slave. It’s surprising to me that these terms are still used commonly. But, master/slave is indeed the commonly used engineering term to describe this type of communication.
A UART is commonly combined with other standards, such as RS-232 or RS-485, for external serial communication over a longer distance. A synchronous UART called a USART is also available on some microcontrollers.
Serial Peripheral Interface (SPI)
SPI is a synchronous communication protocol. It’s typically much faster than a UART or I2C interface. SPI is a full-duplex communication standard, meaning it can send and receive data simultaneously. As with UART, SPI is designed only for short distance communication which in most cases means between chips on the same PCB.
SPI consists of 4 wires, or actually 3 wires plus one wire for each peripheral device. The SPI signals are MISO (Master-In-Slave-Out), and MOSI (Master-Out-Slave-In), SCLK (Serial-Clock), and SS (Slave-Select). Multiple peripheral devices can be connected to the microcontroller’s SPI port, but only one device is selected at a time.
The primary advantage of SPI is it’s faster data transfer speed. The main disadvantage of SPI is the need for 4 signal wires, versus only 2 signals required for UART and I2C.
Inter-Integrated circuit (I2C)
I2C is a synchronous serial bus protocol used for serial communication between components on the same board. The primary advantages of I2C is that it only requires two wires, is synchronous, and that unlike UART or SPI, I2C is a bus protocol.
Being a true bus means that I2C allows multiple devices to be connected to the same two wires. Instead of a slave-select signal as with SPI, I2C uses a unique address for each device. This fact greatly simplifies on-board signal routing.
As with SPI, I2C uses a clock signal to synchronize the communication. However, unlike SPI, I2C is only half-duplex so it uses a single signal for both data in and data out. I2C can’t send data in both directions simultaneously.
The price for this simplicity is that I2C is considerably slower than SPI. So I2C is primarily used for interfacing two components that don’t require high data transfer speed. It’s especially popular for interfacing sensors (which are relatively slow).
Universal Serial Bus (USB)
You have obviously heard of USB before, since this serial communication protocol is so universal. USB is the fastest serial communication protocol available on most microcontrollers. It is commonly used to transfer larger amounts of data between external peripherals.
USB is available in a variety of speeds so be sure the microcontroller you select has a USB port with the necessary data transfer rate for your application.
Unlike the other serial protocols we’ve discussed which are used for short distance, on-board communication, USB is designed for longer distance communication with external peripherals.
USB is a master/slave protocol. The host (master) can transmit data to the device (slave), and the host can read data from the device. The key is the host always initiates communication, and the peripheral device only responds.
Because the host initiates and manages all communication, a USB host is more complex than a USB device. A microcontroller that offers built-in USB communication can only serve as the peripheral device with a PC typically acting as the host. This strictly defined master/slave relationship necessitates that any USB cables be asymmetrical with different connectors on each end to identify the host.
A newer standard called USB On-The-Go (OTG) allows the master/host and the slave/device to swap roles. So a microcontroller with a USB OTG port can serve as either a USB device or a USB host. Only higher-end microcontrollers will offer USB OTG.
Controller Area Network (CAN)
CAN is a serial communication standard for use in automotive applications only.
If your product doesn’t work with an automobile, then you will have no need for a CAN port on your microcontroller. Only select microcontrollers include a CAN port.
In Circuit Programming (ISP)
There are two broad methods for programming a microcontroller: in circuit or out of circuit.
With In-Circuit-Programming (ISP) the microcontroller is programmed after it has been inserted into the full circuit. This is almost always the ideal method during development, since it allows you to program and quickly test everything. JTAG and SWD are the most commonly used ISP protocols.
The other option is to program the microcontroller chip separately before inserting it into the full system. This rarely makes sense during development but it can sometimes be desirable for production.
Does your product need wireless capabilities? You can purchase specialized microcontrollers that include Bluetooth, ZigBee, Wi-Fi, and others. Bluetooth Low-Energy microcontrollers are especially common.
In most cases it’s better to start with a pre-certified module for any wireless functionality. Wireless design can be complex and requires more expensive FCC certification. So modules are a lower risk, more affordable option.
Let’s look closer at some of the more well-known microcontroller cores:
ARM Cortex-M microcontrollers are powerful, popular, and affordable. In fact, Cortex-M microcontrollers are probably used in more products today than any other microcontroller.
ARM doesn’t actually make microcontrollers, but rather they license their architecture to other chip manufacturers.
ARM Cortex-M is a 32-bit architecture which is significantly more powerful than your average 8 bit microcontroller. In fact, there are Cortex-M implementations that have processing speeds comparable to some microprocessors. 32-bit microcontrollers are required when you require a large amount of memory.
Microcontrollers based on the ARM Cortex-M architecture are available in numerous versions from a wide variety of companies, including ST Microelectronics, Silicon Labs, Microchip Technology, Freescale, NXP, Cypress, Maxim Integrated, Analog Devices, and Texas Instruments.
My favorite line of ARM Cortex-M microcontrollers is the STM32 series from ST Microelectronics. The STM32 line is huge and can be subdivided into the STM32F and STM32L series. The STM32F series prioritizes performance and/or cost, whereas the STM32L series prioritizes low power consumption for portable applications.
For an introduction to programming the STM32 see this article.
Figure 4 – Development platform based on STM32 microcontroller
The highest performing model is the STM32F7 which is able to process 1 billion commands per second! This level of processing speed approaches that of many microprocessors. On the other hand, the STM32L0 processes only 26 million commands per second, but uses significantly less power than the STM32F7.
Finally, unlike some companies (such as Qualcomm), ST Microelectronics provides excellent technical support for startups and small companies. They will even provide a design review of any circuit designs using their products.
Intel developed the 8-bit 8051 microcontroller in 1980 and it’s still around, and fairly widely used. At least eight different semiconductor manufacturers sell modern versions of the 8051.
Figure 5 – Example of an 8051 microcontroller
In most cases, the 8051 is only a viable option for the simplest types of products that are very cost sensitive.
Most versions of Arduino use the AVR line of microcontrollers from Atmel (now owned by Microchip Technology). This can make it easier to transition from an Arduino to an Atmel AVR microcontroller. Atmel AVR microcontrollers are available in both 8-bit and 32-bit versions.
Figure 6 – Atmel AVR microcontroller
Atmel AVR is a fantastic line of controllers for makers and hobbyists, especially given the fact that they power the Arduino.
Microchip Technology produces a popular family of microcontrollers called PIC. They are available in a variety of options depending on your needs, including 8-bit, 16-bit and 32-bit versions. You can select from endless combinations of pins, package styles and on-chip peripherals.
Figure 7 – Microchip’s PIC microcontroller line comes in many versions
As with the Atmel AVR line of microcontrollers, PIC controllers are very popular with electronic hobbyists and makers. If you need a low cost 8-bit or 16-bit, or even a 32-bit controller for your product, then a PIC microcontroller can be a good, low-cost choice.
Designing The Power Supply
The power supply circuit is one of the most critical parts of hardware design. Try to define your grounding and power layout as early as possible.
The microcontroller’s current consumption is determined by the clock frequency, operating voltage, and the loads on the I/O pins.
To ensure that the microcontroller has a nice, clean power supply voltage you must place decoupling capacitors right next its power supply pins.
Microcontrollers that include analog inputs and/or analog outputs will have distinct power and ground pins exclusively for analog. It is particularly important for these analog supply pins to be extremely clean of all noise because any noise will decrease the accuracy. Digital signals are always much more tolerant of noise compared to analog signals.
Including an inductor in series with the analog supply pin to create a LC (inductor-capacitor) filter is usually a good idea. It will filter out any high-frequency noise on the supply line.
If your power supply voltage is above the maximum supply voltage of the microcontroller, then you will need to use a step-down voltage regulator. If the voltage difference is small then a linear regulator is probably best.
For example, if your product uses a 3.7V lithium battery, yet the microcontroller can only work up to 3V, then a low-dropout linear regulator (commonly called an LDO) is the best choice.
However, if the supply voltage is significantly higher than the maximum supply voltage for your microcontroller, then you should use a switching regulator (also called a buck converter) to step down the voltage.
For example, if your product uses a 12VDC power supply and your microcontroller can only handle up to 3V, then you would want to use a switching regulator
Linear regulators are very inefficient and dissipate too much power as heat if their input supply voltage is much higher than the output voltage.
Be advised that when using a switching regulator it may be best to sub-regulate the switching regulator’s output voltage using a linear regulator. A linear regulator provides a much cleaner voltage free of noise and switching transients. A linear regulator is especially critical when using analog I/O on the microcontroller.
Any microcontroller (or microprocessor) requires a precise clock for timing. Most microcontrollers can use either an external or internal clock. For accurate timing applications an external crystal is the best choice.
For applications without a requirement for precise timing, use of the internal clock will simplify the circuit design.
Be sure to carefully follow the layout guidelines specified in the datasheet when doing the PCB layout for the crystal. The traces should be as short as possible, and the load capacitance on the crystal must match the manufacturer’s recommendations.
Other content you may like:
- Datasheet Review: Entry-Level STM32 Cortex-M0 Microcontroller (Blog + Video)
- Home Automation with an Arduino: A Basic Tutorial
- An Introduction to Basic Electronics
- Microcontroller or Microprocessor: Which is Right for Your New Product?
- Datasheet Review: High-Performance STM32 Cortex-M4 Microcontroller