Embedded Systems

Microcontrollers

A microcontroller is a compact integrated circuit containing a processor, memory and peripheral hardware in a single chip. Microcontrollers are used to control electronic equipment, read sensors, communicate with other devices, control motors and displays, and perform many other dedicated control functions.

What Is a Microcontroller?

A microcontroller, often abbreviated as MCU, is a programmable integrated circuit designed to control electronic systems.

Unlike a simple logic IC, a microcontroller contains a processor that executes instructions stored in memory.

A typical microcontroller contains:

  • CPU core
  • Program memory
  • RAM
  • GPIO pins
  • Timers and counters
  • Interrupt controller
  • Communication peripherals
  • Analog-to-digital converter
  • Clock circuitry
  • Power-management functions

Basic Microcontroller Structure

             ┌──────────────────────┐
             │   MICROCONTROLLER    │
             │                      │
             │      CPU CORE        │
             │         │            │
             │   ┌─────┴─────┐      │
             │   │           │      │
             │  RAM       Flash     │
             │                 │     │
             │   ┌─────────────┐    │
             │   │ Peripherals │    │
             │   └─────────────┘    │
             │      │  │  │  │      │
             └──────┼──┼──┼──┼──────┘
                    │  │  │  │
                   GPIO ADC UART
                         │
                       SPI/I2C

The CPU executes the program while the peripherals interact with the outside world.

Microcontroller vs Microprocessor

Feature Microcontroller Microprocessor
CPU Integrated Integrated
RAM Usually integrated Usually external
Program memory Usually integrated Often external or system-dependent
Peripherals Many integrated Often external
Power consumption Generally low Often higher
Typical use Dedicated control General-purpose computing

CPU Core

The CPU is the part of the microcontroller that executes the program instructions.

It performs operations such as:

  • Arithmetic calculations
  • Logical operations
  • Data movement
  • Comparisons
  • Branching
  • Peripheral control

Microcontroller Architectures

Different microcontrollers use different processor architectures.

Common architectures include:

  • ARM Cortex-M
  • AVR
  • 8051
  • PIC
  • RISC-V
  • Xtensa

The architecture determines the instruction set, registers, memory organization and many other characteristics.

Flash Memory

Flash memory is commonly used to store the microcontroller's program.

The contents remain stored when power is removed.

The amount of Flash available determines how large the firmware can be.

RAM

RAM is temporary working memory used while the program is running.

It can contain:

  • Variables
  • Buffers
  • Temporary calculations
  • Stack data
  • Communication data

RAM contents are normally lost when power is removed.

EEPROM

Some microcontrollers contain EEPROM or provide another form of non-volatile data storage.

It can be used for information such as:

  • Configuration settings
  • Calibration values
  • User preferences
  • Device identification

GPIO

GPIO means General-Purpose Input/Output.

GPIO pins allow the microcontroller to interact directly with external electronics.

A GPIO pin can commonly be configured as:

  • Digital input
  • Digital output
  • Pull-up input
  • Pull-down input
  • Alternate-function peripheral pin

Digital Input

A digital input allows the microcontroller to detect a HIGH or LOW signal.

Examples include:

  • Push buttons
  • Switches
  • Digital sensors
  • Logic signals
  • Alarm outputs

Digital Output

A digital output allows the microcontroller to control external devices.

Examples include:

  • LEDs
  • Transistors
  • Relays through driver circuits
  • Logic ICs
  • Enable signals

The maximum current of a GPIO pin must always be respected.

Pull-Up Resistor

A pull-up resistor connects an input to the positive supply so that the input assumes a defined HIGH state when no external device is driving it LOW.

VCC
 │
 R
 │
 ├──── GPIO
 │
Switch
 │
GND

When the switch is open, the input is HIGH. When the switch is closed, the input becomes LOW.

Pull-Down Resistor

A pull-down resistor connects an input to ground so that it normally remains LOW.

VCC
 │
Switch
 │
 ├──── GPIO
 │
 R
 │
GND

When the switch closes, the input becomes HIGH.

Timers and Counters

Timers are hardware peripherals used for measuring time and generating precise timing events.

They can be used for:

  • Delays
  • Periodic interrupts
  • Pulse generation
  • PWM
  • Frequency measurement
  • Event counting

PWM

PWM stands for Pulse Width Modulation.

A PWM signal rapidly switches between HIGH and LOW while changing the percentage of time spent HIGH.

Low duty cycle:

───┐   ┌───┐   ┌───
   └───┘   └───┘

High duty cycle:

───┐─────┐─────┐───
   └─────┘     └───

PWM is commonly used for:

  • LED brightness control
  • Motor speed control
  • Servo control
  • Power control
  • Audio generation

ADC

ADC stands for Analog-to-Digital Converter.

An ADC allows the microcontroller to measure analog voltages.

Analog sensor
      │
      ▼
     ADC
      │
      ▼
Digital value
      │
      ▼
     CPU

ADC resolution determines how many digital levels can represent the input voltage.

ADC Resolution

For an ideal N-bit ADC, the number of digital levels is:

2^N

For example, a 10-bit ADC provides:

2^10 = 1024 levels

A 12-bit ADC provides:

2^12 = 4096 levels

The actual accuracy depends on many factors beyond resolution.

DAC

Some microcontrollers contain a DAC, or Digital-to-Analog Converter.

A DAC converts digital values into an analog voltage or current.

Applications include:

  • Audio generation
  • Analog control
  • Waveform generation
  • Reference generation

UART

UART stands for Universal Asynchronous Receiver-Transmitter.

It provides serial communication between the microcontroller and another device.

Microcontroller
      │
      │ TX
      ├────────────► RX
      │
      │ RX
      ◄────────────┤
                   │
                Other Device

UART is commonly used for:

  • Serial debugging
  • GPS modules
  • Bluetooth modules
  • Serial displays
  • Other microcontrollers

I2C

I2C is a two-wire serial communication interface.

The two main signals are:

  • SDA — Serial Data
  • SCL — Serial Clock

Multiple compatible devices can share the same bus using device addresses.

SPI

SPI is a high-speed synchronous serial interface.

Typical signals include:

  • SCLK
  • MOSI
  • MISO
  • CS

SPI is commonly used with:

  • Displays
  • SD cards
  • Flash memory
  • ADCs
  • DACs
  • Sensors

CAN

CAN stands for Controller Area Network.

It is widely used in automotive and industrial applications.

CAN allows multiple controllers and devices to communicate over a shared bus using message-based communication.

USB

Some modern microcontrollers include USB hardware.

This can allow the microcontroller to communicate directly with a computer or other USB device.

Possible applications include:

  • USB serial devices
  • USB keyboards
  • USB controllers
  • USB audio
  • Data acquisition

Interrupts

An interrupt allows a peripheral or external event to temporarily interrupt the normal execution of the program.

Examples include:

  • Timer overflow
  • GPIO change
  • UART reception
  • ADC completion
  • External interrupt

Interrupts allow the microcontroller to respond quickly to events without continuously polling every peripheral.

Watchdog Timer

A watchdog timer can reset the microcontroller if the firmware stops operating correctly.

The software periodically services the watchdog. If it fails to do so within the configured time, the watchdog can reset the device.

This is particularly useful in unattended embedded systems.

Clock System

The microcontroller requires a clock to execute instructions and operate many of its peripherals.

Clock sources may include:

  • Internal oscillator
  • External crystal
  • External oscillator
  • PLL-generated clock

The clock frequency affects processing speed and peripheral timing.

Crystal Oscillator

An external crystal can provide an accurate frequency reference.

The crystal is normally connected to dedicated oscillator pins through the recommended supporting components.

The exact circuit depends on the microcontroller.

Reset

Reset places the microcontroller into a known startup state.

Reset can occur because of:

  • Power-on reset
  • External reset pin
  • Watchdog timeout
  • Brownout condition
  • Software reset

Brownout Detection

Brownout detection monitors the supply voltage.

If the supply falls below a specified threshold, the microcontroller can reset or enter a protected state.

This helps prevent unpredictable operation when the supply voltage is too low.

Microcontroller Programming

The behavior of a microcontroller is determined by firmware stored in its program memory.

The firmware is commonly developed using languages such as:

  • C
  • C++
  • Assembly
  • Rust
  • MicroPython on suitable devices
  • Other supported languages

The exact development environment depends on the microcontroller family.

Firmware

Firmware is the software stored inside an embedded device that controls its operation.

A typical firmware program may:

  1. Initialize the hardware.
  2. Configure GPIO.
  3. Configure timers.
  4. Initialize communication interfaces.
  5. Read sensors.
  6. Process data.
  7. Control outputs.
  8. Repeat continuously.

Bootloader

A bootloader is a small program that runs before or alongside the main application firmware.

It can provide functions such as:

  • Firmware programming
  • Firmware updates
  • Recovery
  • Communication with a programming tool

Programming Interfaces

Microcontrollers can be programmed through different interfaces depending on the device.

Examples include:

  • USB
  • UART bootloader
  • SWD
  • JTAG
  • ISP
  • ICSP

Debugging

Debugging allows the developer to inspect the program while it is running.

Depending on the microcontroller, debugging may provide:

  • Breakpoints
  • Single stepping
  • Register inspection
  • Memory inspection
  • Variable inspection
  • Watchpoints

Microcontroller Development Board

A development board combines a microcontroller with supporting hardware that makes experimentation easier.

A board may include:

  • USB connector
  • Voltage regulator
  • Programming interface
  • LEDs
  • Buttons
  • Pin headers
  • Crystal or oscillator

Development boards are useful for learning and prototyping before designing a custom PCB.

Common Microcontroller Families

Many different microcontroller families are available.

  • Arduino-compatible AVR devices
  • STM32
  • ESP32
  • ESP8266
  • PIC
  • RP2040
  • MSP430
  • AVR
  • 8051-family devices
  • RISC-V microcontrollers

ESP32

ESP32 is a family of microcontrollers widely used in connected embedded applications.

Depending on the particular ESP32 variant, features can include:

  • Wi-Fi
  • Bluetooth
  • GPIO
  • Timers
  • ADC
  • UART
  • SPI
  • I2C
  • PWM

The exact features differ between ESP32 variants.

STM32

STM32 is a large family of ARM-based microcontrollers.

Different STM32 families target applications ranging from simple control systems to high-performance embedded processing.

Features vary significantly between models.

AVR

AVR microcontrollers are widely known from the Arduino ecosystem and other embedded applications.

They are available in many different configurations with varying amounts of memory and peripheral hardware.

PIC Microcontrollers

PIC is a family of microcontrollers developed by Microchip.

PIC devices range from relatively simple controllers to high-performance microcontrollers with advanced peripherals.

Microcontroller Power Consumption

Power consumption depends on:

  • Clock frequency
  • Supply voltage
  • CPU activity
  • Peripheral activity
  • GPIO loading
  • Wireless operation
  • Sleep mode

Low-power applications often use sleep modes and wake the microcontroller only when necessary.

Sleep Modes

Many microcontrollers provide low-power operating modes.

Depending on the device, these may disable some or most internal circuitry while preserving selected functions.

The microcontroller can then wake in response to an event such as a timer, GPIO signal or communication event.

Microcontroller Applications

  • Home appliances
  • Audio equipment
  • Internet radios
  • Remote controls
  • Industrial controllers
  • Motor controllers
  • Robotics
  • Security systems
  • Displays
  • Measurement instruments
  • Automotive electronics
  • IoT devices
  • Battery-powered equipment
  • Consumer electronics

Microcontroller and Sensors

A microcontroller can read information from sensors and use that information to make decisions.

Sensor
  │
  ▼
ADC / GPIO
  │
  ▼
Microcontroller
  │
  ▼
Decision
  │
  ▼
Output

For example, a temperature sensor can be monitored and a fan switched on when a selected temperature is reached.

Microcontroller and Displays

Microcontrollers can control many types of displays.

  • LED indicators
  • 7-segment displays
  • Character LCDs
  • OLED displays
  • TFT displays
  • Graphic displays

The communication interface may use GPIO, SPI, I2C, parallel interfaces or another protocol depending on the display.

Microcontroller and Motors

A microcontroller normally does not drive a large motor directly.

Instead, it generates control signals for a driver circuit.

Microcontroller
      │
      ▼
Motor Driver
      │
      ▼
Motor

The driver may use transistors, MOSFETs, H-bridges or dedicated motor-driver ICs.

Microcontroller and Relays

A GPIO pin normally should not directly drive a relay coil.

A transistor or dedicated driver is normally used between the MCU and relay.

GPIO
 │
 ▼
Transistor
 │
 ▼
Relay Coil
 │
 ▼
Load

A suitable flyback protection arrangement is required for a conventional DC relay coil.

Microcontroller Testing

When a microcontroller circuit does not work, start with the hardware before assuming the firmware is defective.

  1. Check the supply voltage.
  2. Check ground connections.
  3. Check reset circuitry.
  4. Check the clock source.
  5. Check programming connections.
  6. Check firmware programming.
  7. Check GPIO configuration.
  8. Check peripheral configuration.
  9. Check external components.
  10. Use an oscilloscope or logic analyzer when appropriate.

Microcontroller Not Starting

Possible causes include:

  • Missing power
  • Incorrect supply voltage
  • Reset held active
  • Incorrect clock configuration
  • Incorrect firmware
  • Corrupted program memory
  • Incorrect boot configuration
  • Hardware damage

GPIO Not Working

If a GPIO pin does not behave correctly, check:

  • Pin configuration
  • Input/output direction
  • Pull-up or pull-down configuration
  • Alternate-function selection
  • Output drive mode
  • External circuitry
  • Firmware logic

Communication Problems

When UART, SPI or I2C communication fails, check:

  • Supply voltage
  • Ground connection
  • Pin assignment
  • Baud rate
  • Clock frequency
  • Protocol configuration
  • Pull-up resistors where required
  • Signal integrity

Microcontroller Replacement

Replacing a microcontroller is more complicated than replacing a simple logic IC.

The replacement must be compatible in both hardware and firmware.

Important factors include:

  • Package
  • Pinout
  • CPU architecture
  • Flash memory
  • RAM
  • GPIO count
  • Peripheral availability
  • ADC characteristics
  • Communication interfaces
  • Supply voltage
  • Clock requirements
  • Firmware compatibility

How to Select a Microcontroller

  1. Determine the required number of GPIO pins.
  2. Determine the required supply voltage.
  3. Determine processing requirements.
  4. Determine Flash requirements.
  5. Determine RAM requirements.
  6. Determine ADC requirements.
  7. Determine communication interfaces.
  8. Determine timer and PWM requirements.
  9. Determine package requirements.
  10. Determine power-consumption requirements.
  11. Check development-tool availability.
  12. Check component availability and cost.

Microcontroller Datasheet

The datasheet is the most important technical reference when designing with a particular microcontroller.

It contains information such as:

  • Pin configuration
  • Electrical characteristics
  • Absolute maximum ratings
  • Supply voltage
  • Clock requirements
  • Memory size
  • Peripheral specifications
  • GPIO characteristics
  • ADC specifications
  • Package information

Absolute Maximum Ratings

Absolute maximum ratings specify limits that should not be exceeded.

They are not necessarily recommended operating conditions.

Examples include:

  • Maximum supply voltage
  • Maximum input voltage
  • Maximum GPIO current
  • Maximum junction temperature

Microcontroller Development Tools

Development normally requires a combination of software and hardware tools.

Common tools include:

  • Compiler
  • IDE
  • Programmer
  • Debugger
  • USB interface
  • Serial terminal
  • Oscilloscope
  • Logic analyzer

Microcontroller Debugging Tools

A multimeter is useful for checking power and DC voltages.

An oscilloscope can reveal:

  • Clock signals
  • PWM signals
  • UART data
  • SPI activity
  • Reset pulses
  • Analog signals

A logic analyzer is particularly useful for examining digital communication protocols.

Advantages of Microcontrollers

  • Programmable
  • Compact
  • Low component count
  • Low power consumption
  • Many integrated peripherals
  • Flexible control
  • Easy to update through firmware
  • Available in many performance levels

Limitations of Microcontrollers

  • Firmware is required
  • Development tools are required
  • Programming errors can cause system failures
  • GPIO current is limited
  • Memory is limited by the device
  • Peripheral capabilities vary considerably
  • Firmware compatibility must be considered during replacement

Common Microcontroller Design Mistakes

  • Incorrect supply voltage
  • Exceeding GPIO current limits
  • Leaving required inputs floating
  • Poor power-supply decoupling
  • Incorrect reset design
  • Incorrect clock configuration
  • Ignoring ADC reference requirements
  • Incorrect communication configuration
  • Insufficient PCB grounding
  • Ignoring thermal requirements

Key Points

  • A microcontroller is a programmable control IC.
  • It normally contains a CPU, memory and peripherals.
  • GPIO allows the MCU to interact with external hardware.
  • Timers provide accurate hardware timing.
  • PWM is useful for motor and LED control.
  • ADC allows the MCU to measure analog signals.
  • UART, I2C and SPI are common communication interfaces.
  • Interrupts allow rapid response to hardware events.
  • Watchdogs can recover systems when firmware stops operating correctly.
  • Flash stores firmware while RAM stores temporary working data.
  • Microcontrollers are used extensively in embedded systems.
  • The datasheet should always be consulted before designing with a device.
  • A replacement MCU must be compatible with both the hardware and firmware.

Continue Learning About Microcontrollers

The next pages can cover microcontroller types, architecture, GPIO, memory, timers, interrupts, ADC, PWM, UART, SPI, I2C, programming, debugging, development boards, testing, troubleshooting, replacement and selection.