Microcontrollers, embedded systems and FPGAs · Part 34 of 36

Microcontrollers and firmware

Expert 19 min

What is inside a microcontroller, GPIO, ADC, timers and PWM, interrupts versus polling, low-power design and the rise of RISC-V.

What you will learn

  • You can describe the main peripherals of a microcontroller.
  • You can configure GPIO, PWM and ADC conceptually and choose interrupts or polling.
  • You can estimate battery life from sleep and active currents.

Before this: Flip-flops, counters and clocks

A computer on a chip

A microcontroller (MCU) combines a processor core, flash memory for the program, RAM, and peripherals on one chip, often for less than a euro. Common cores: Arm Cortex-M (STM32, nRF52, RP2040/RP2350), AVR (classic Arduino), Xtensa and RISC-V (ESP32-C3/C6, many newer parts). RISC-V is an open instruction set; its ratified RVA23 profile (October 2024) set a common baseline for application processors, and RISC-V microcontrollers became mainstream in 2023–2026. The Raspberry Pi RP2350 (2024) even contains both Arm and RISC-V cores.

A microcontroller board on a breadboard wired to a sensor, a button and a glowing LED

Talking to the world

  • GPIO (general-purpose I/O): pins set high/low or read as inputs. Inputs need defined levels: enable internal pull-up/pull-down resistors for buttons. Outputs typically source or sink only a few to ~20 mA — use transistors for anything bigger (lessons on BJTs and MOSFETs). Mind the voltage: a 3.3 V MCU is not always 5 V tolerant.
  • ADC: converts analogue voltages to numbers (lesson "Sampling, aliasing and digital filters"): typically 10–12 bits, with a reference voltage that sets the range. A divider scales higher voltages down.
  • Timers and PWM: hardware timers measure time, generate interrupts and produce pulse-width modulation: a square wave whose duty cycle sets the average power — LED dimming, motor speed, and with an RC low-pass, a simple DAC.
  • Communication peripherals: UART, I²C, SPI, USB, CAN, often radio (next lesson).
  • Watchdog: a timer that resets the chip if the firmware stops "petting" it — a safety net against hangs.

Polling versus interrupts

Polling means checking a flag in a loop — simple, but wasteful and slow to react if the loop does other work. Interrupts let hardware events (a pin change, a timer, received data) pause the main program and run a short interrupt service routine (ISR). Rules: keep ISRs tiny, set a flag or push to a queue and do the work in the main loop; mark shared variables volatile (C/C++) and protect multi-byte data from being half-updated — the concurrency lessons apply on a single core too. DMA controllers move data between peripherals and memory without the CPU at all.

Firmware structure

  • Super loop: setup() then while (1) { read; decide; act; } — fine for simple devices.
  • RTOS (FreeRTOS, Zephyr): tasks with priorities, queues and timers for more complex firmware.
  • Languages: mostly C and C++; Rust on embedded matured strongly (embedded-hal 1.0 in January 2024), and MicroPython/CircuitPython make prototyping fast.
A small microcontroller board connected to a temperature sensor and a tiny fan

Low power

Battery devices spend most of their life asleep. Example: 5 mA for 50 ms every 10 s, 5 µA asleep the rest: average ≈ 5 mA × 0.005 + 5 µA ≈ 25 µA + 5 µA = 30 µA. A 1,000 mAh cell would last about 33,000 hours — nearly four years, before self-discharge and temperature effects, which often dominate in practice. Wake up rarely, do the work fast, turn off radios and sensors, and measure real consumption with a suitable meter.

Security

Connected devices need secure boot, signed firmware updates, no default passwords and a vulnerability-handling process — legally required for many products under the EU RED cybersecurity rules (since August 2025) and the Cyber Resilience Act (reporting from September 2026, full obligations from December 2027).

Key points

  • An MCU = core + flash + RAM + peripherals; Arm Cortex-M dominates, RISC-V is mainstream since ~2023 (RVA23 profile 2024).
  • GPIO with pull-ups, ADC, timers/PWM, communication peripherals and a watchdog.
  • Interrupts react quickly; keep ISRs short and protect shared data; DMA moves data without the CPU.
  • Super loop, RTOS, C/C++, Rust (embedded-hal 1.0, 2024), MicroPython.
  • Average current = duty-weighted sum; sleep most of the time; secure boot and updates are now legal duties in the EU.

Flashcards

Question: What is PWM used for?
Answer: Setting average power or an analogue level by varying the duty cycle of a square wave.
Question: What should an ISR do?
Answer: Very little — set a flag or queue data; the main loop does the work.
Question: What does a watchdog do?
Answer: Resets the MCU if the firmware stops responding.
Question: What is RISC-V?
Answer: An open, royalty-free instruction set architecture.

Check yourself

Think about your answer, then open it. In the app the quiz is interactive, earns points and brings back what you missed.

  1. An MCU draws 10 mA for 20 ms every second and 2 µA otherwise. Average current (in µA, approx.)?

    Show the answer

    202 µA (± 2 µA)

    10 mA × 0.02 = 200 µA, plus ≈ 2 µA sleep ≈ 202 µA.

    Source: Making Embedded Systems, ch. 13

  2. A 12-bit ADC with a 3.3 V reference reads 2048. Input voltage (in V)?

    Show the answer

    1.65 V (± 0.01 V)

    2048 / 4096 × 3.3 = 1.65 V.

    Source: Harris & Harris / MCU reference manuals

  3. A button connects a pin to ground when pressed. What keeps the input defined when released?

    • An internal or external pull-up resistor
    • A pull-down resistor
    • A series capacitor
    • Nothing is needed
    Show the answer

    An internal or external pull-up resistor

    The pull-up holds the pin high; pressing pulls it low.

    Source: Making Embedded Systems, ch. 4

  4. Which are good interrupt practices?

    • Keep the ISR short
    • Set a flag and process in the main loop
    • Use long delays inside the ISR
    • Protect variables shared with the main loop
    Show the answer

    Keep the ISR short; Set a flag and process in the main loop; Protect variables shared with the main loop

    Delays in ISRs block other interrupts and the main program.

    Source: Making Embedded Systems, ch. 5

Sources

Facts checked on 2026-11-30. Skillgrove lessons are free learning material — no certificate and no accreditation.