Try Embedded

Microcontrollers and your first program

What a microcontroller is, how code gets onto it, and the parts of a C program.

What a microcontroller is

A microcontroller is a complete small computer on a single chip. It sits inside washing machines, car keys, thermostats, drones and toothbrushes. It usually costs less than a cup of coffee and does one job for years without being switched off.

Inside the chip there are four things you will hear about constantly.

PartWhat it doesEveryday comparison
Processor (CPU)Carries out instructions, one after anotherThe person following a recipe
Flash memoryStores your program. Keeps it when the power is offThe recipe book
RAMHolds the values the program is working with. Emptied when the power goes offThe kitchen counter
PeripheralsCircuits that touch the outside world, such as pins, timers and serial portsThe oven, the tap, the timer

The pins are the metal legs of the chip. A program can switch a pin on to light an LED, or read a pin to see whether a button is pressed. Everything a product does comes down to reading some pins and driving others.

Embedded C is the C language used to write programs for these chips. It is ordinary C. What makes it "embedded" is what it is used for, namely small memory, no screen or keyboard, and direct control of hardware.

From text to a running program

You write a program as text. The chip cannot run text, so there are a few steps in between.

  1. Write the program in C.
  2. Compile it. A program called a compiler checks your text and translates it into the numeric instructions the processor understands. If it finds a mistake, it stops and tells you.
  3. Flash it. The translated program is copied into the chip's flash memory.
  4. Run it. When the chip gets power, it starts executing your program from the beginning.

On this site, steps 2 to 4 happen when you press Run. Your code runs on a simulated microcontroller in the browser, so you can experiment freely. Nothing can be damaged.

Your first program

Press Run on this example. Then change the text between the quotes and run it again.

#include <stdio.h>

int main(void) {
  printf("Hello, board!\n");
  return 0;
}

Every part has a job.

LineMeaning
#include <stdio.h>Brings in the standard input and output tools, which include printf
int main(void)Starts the function named main. Every C program begins running here
{ and }Mark where the body of the function starts and ends
printf("Hello, board!\n");Prints text. \n means "start a new line"
return 0;Ends main. Zero means "finished without a problem"

Each instruction is called a statement, and each statement ends with a semicolon. Statements run in order, from top to bottom.

#include <stdio.h>

int main(void) {
  printf("first\n");
  printf("second\n");
  printf("third\n");
  return 0;
}

Comments

Anything after // on a line is a comment. The compiler ignores it. Comments are notes for people, including you in three months.

// Switch the heater off above 80 degrees.
printf("heater off\n");   // a comment can also follow a statement

Good comments say why the code does something. The code itself already says what it does.

Printing values

printf can print numbers as well as fixed text. Inside the quotes, %d is a placeholder. It is replaced by the number you give after the text.

#include <stdio.h>

int main(void) {
  printf("The answer is %d\n", 42);
  printf("%d plus %d is %d\n", 2, 3, 2 + 3);
  return 0;
}

The placeholders are filled in from left to right, so there must be one value for each. On a real board printf usually sends its text over a serial cable to your computer, and it is the first tool everyone uses to see what their program is doing.

Functions

A function is a named block of code. main is one, and you can write your own. A function can take inputs and hand back a result.

#include <stdio.h>

int add(int a, int b) {
  return a + b;
}

int main(void) {
  printf("%d\n", add(2, 3));
  printf("%d\n", add(10, 20));
  return 0;
}

Read the first line of add piece by piece.

PieceMeaning
int (at the start)The function hands back a whole number
addIts name
(int a, int b)It takes two whole numbers, which it calls a and b
return a + b;It hands back their sum

Writing add(2, 3) is called calling the function. The call is replaced by the value that comes back, so printf receives 5.

Some functions only do something and hand nothing back. They are declared void, which means "nothing".

#include <stdio.h>

void greet(void) {
  printf("Hello!\n");
}

int main(void) {
  greet();
  greet();
  return 0;
}

Note: The word void inside the parentheses means the function takes no inputs.

When the compiler complains

Everyone makes typing mistakes, and the compiler catches many of them. Run this example. It is broken on purpose.

#include <stdio.h>

int main(void) {
  printf("almost right\n")
  return 0;
}

The message says what was expected and gives a line number. Add the missing semicolon and run it again.

Three habits make errors easy to deal with.

  • Read the message. It usually says exactly what is wrong.
  • Go to the line it names. If that line looks fine, look at the line above it.
  • Fix the first error only, then run again. One mistake often causes several messages.

The mistakes beginners make most often are a missing semicolon, a missing closing brace, a misspelled name and a quote that was opened and never closed.

How practice works here

Each practice problem gives you a function to complete. You do not write main. The tests supply their own main, call your function with different inputs and compare what comes out.

  • Run tries your code on the examples shown in the problem. It happens instantly, in your browser.
  • Submit also runs hidden tests, which check the unusual cases.
  • Hints are there when you are stuck. Open them one at a time.

Getting a problem wrong is normal and it is how the learning happens. Read what the test expected, compare it with what your code produced and adjust.

Common mistakes

  • Forgetting the semicolon at the end of a statement.
  • Forgetting \n, so that everything prints on one line.
  • Using %d without giving a value for it.
  • Mixing up printing a value with returning it. Tests that check a result need it returned.
  • Changing a function's name or inputs in a practice problem, so the tests can no longer call it.

Interview question: What is the difference between flash memory and RAM in a microcontroller?

Answer: Flash holds the program and keeps its contents when the power is removed. RAM holds the values the program is working on while it runs, and is lost at power-off. A microcontroller has far more flash than RAM, often 64 KB of flash and only 8 KB of RAM, so firmware is careful about how much data it keeps.

In the product: a smart doorbell

A doorbell's firmware is the same shape as every program in this lesson: set things up once, then react. Here the reaction is a function that decides what the doorbell does when the button is pressed, depending on whether the owner set it to silent.

#include <stdio.h>

// What one press does: ring and notify, or only notify at night.
void on_press(int silent) {
  if (silent) {
    printf("notify phone (silent mode)\n");
  } else {
    printf("ring chime\n");
    printf("notify phone\n");
  }
}

int main(void) {
  printf("doorbell ready\n");
  on_press(0);  // a visitor at noon
  on_press(1);  // a visitor at midnight
  return 0;
}

Build it: the kitchen scale's display

This is step 1 of the module project, a digital kitchen scale, built one small piece per lesson. Every product starts by showing something to its user. Here that is the line on the scale's display.

Lab: Print the weight on the display

The display shows one line. Write the function show_weight(grams), which prints Weight: 125 g with the number given, followed by a newline (\n). Use printf with %d for the number.

Then write show_welcome(), which prints the two lines the scale shows when it is switched on: Kitchen Scale on the first line and Ready on the second.

Going deeper

Interview question: Walk through what happens in a smart doorbell from the button press to the phone notification.

Answer: The button pulls a pin low and wakes the MCU from sleep (an edge interrupt), the firmware debounces it, rings the chime locally at once so the visitor gets feedback, captures a snapshot, then brings up Wi-Fi, which takes the longest, and sends the event to the cloud, which pushes a notification to the phone. A good answer orders the work by what the user feels: local feedback first, network last, and back to sleep as soon as possible because the battery runs the whole design.

Interview question: What is the difference between a microcontroller and a microprocessor?

Answer: A microcontroller puts the CPU, flash for the program, RAM and peripherals (GPIO, timers, UART, ADC) on one chip, so it can run a product on its own with very little external circuitry, low cost and low power. A microprocessor is mainly a CPU, often much faster and with a memory management unit, that needs external RAM, storage and support chips and usually runs a full operating system such as Linux.

Interview question: Why does firmware's main function usually never return?

Answer: On a microcontroller there is no operating system to return to: main is called by the startup code, and if it returned, the startup code would typically loop forever or reset. Firmware is designed to run as long as the device has power, so main ends in an infinite loop (or starts an RTOS scheduler that never returns) that keeps responding to inputs and events.

Explain it: In your own words, describe what happens between pressing build and the program running on the microcontroller.

Answer: The compiler turns the C source into machine instructions for the microcontroller's CPU, and the linker places them at the addresses where the chip's flash memory is, producing a binary image. A programmer or debug probe writes that image into the flash. When the chip powers up or resets, it starts executing from flash: a little startup code prepares memory and then calls main, which runs the program's loop for as long as the device is on.