Skip to content

What a microcontroller is

The chip that runs your program, and where the program and its variables are stored.

Explanation

An embedded system is a computer built into a product to do one job: a washing machine controller, a bike light, a thermostat, a drone. It has no screen or keyboard of its own. It starts as soon as power arrives and runs the same program for years.

The computer inside is usually a microcontroller, often written MCU. One chip holds the processor core, the memory and the peripherals. Peripherals are the parts that connect to the outside world: pins, timers, serial ports and analog inputs. Popular families for learning are the ARM Cortex-M chips (STM32, nRF52, RP2040), the AVR chips on the original Arduino boards such as the Uno, and the ESP32.

You need to know 2 kinds of memory from the start. Flash stores your program and your constants, and keeps them when the power is off. RAM holds the variables your program changes while it runs. At power up, RAM holds nothing useful until the startup code fills it in. Flash is larger and RAM is faster, but both are small: a beginner board may have 256 KB of flash and 32 KB of RAM, hundreds of thousands of times less than a laptop.

On Cortex-M chips, ARM fixes the main memory regions, and you will see them in every debugger: the low addresses are for code, RAM starts at 0x20000000 and the peripherals start at 0x40000000. The chip maker decides where the flash sits inside the code region: 0x08000000 on STM32, 0x10000000 on the RP2040 and 0x00000000 on nRF52. The first digits of a pointer value tell you what kind of memory it points to.

When the chip starts, a short piece of startup code copies the initial values of your initialised global variables from flash into RAM. It sets the globals that have no initial value to zero, and then calls main(). The rest of RAM is left as it is. The local variables of a function live on the stack, a region of RAM that grows and shrinks as functions are called and return. Your main() then runs a loop that never ends. There is no operating system to return to.

Key points

  • An MCU is a processor, memory and peripherals on one chip.
  • Flash keeps the program when the power is off. RAM holds the variables and holds nothing useful at power up.
  • On Cortex-M, RAM starts at 0x20000000 and peripherals at 0x40000000.
  • Globals with an initial value live in RAM and get their value copied from flash at startup. Locals live on the stack.
  • main() runs a loop forever. There is nothing to return to.

Check your understanding

3 questions on this lesson. An explanation follows each answer.

Sign in to answer the questions. Your answers count towards your level.

Sign in

You can read the lesson and the project without an account.

Project

Blink the on-board LED

What you need

  • A development board. Good first choices are the Raspberry Pi Pico, an STM32 Nucleo-64, an Arduino Uno or an ESP32 development kit. The Pico, a Nucleo-64 and an ESP32 kit each cost under USD 20. An official Uno costs about USD 27; compatible clones cost under USD 10.
  • A USB cable that carries data, not only power.
  • The free tools for your board. The Arduino IDE works with all 4 boards. You can also use STM32CubeIDE for the Nucleo or the Pico SDK for the Pico.

Steps

  1. 1Install the tools and connect the board. Check that the computer detects it.
  2. 2Open the blink example that comes with the tools. Build it and load it onto the board.
  3. 3Change the on and off times to 100 ms and load the program again. Then change them to 1000 ms.
  4. 4Find the line of code that turns the LED on. Comment it out, load the program again and observe the result.

Done when

The on-board LED blinks at a speed you chose, and you have changed and loaded the program at least 3 times.