ENع
←Embedded systems
Lesson 01 · Getting started with a microcontroller

Where embedded systems live and what limits them

10 min10 steps5 questionsBeginner
About this lesson

An embedded system is a small computer hidden inside a device to do one job: a thermostat, a washing machine, a car, a watch. Open a room thermostat built on an Arduino Uno R3 and follow its firmware as it senses the temperature, decides, and switches a heater, once a second, forever. Then meet the limits every embedded designer works inside: 32 KB of flash, 2 KB of SRAM, a 16 MHz clock and a battery. The Hardware lesson “Microcontrollers: a whole computer on one chip” explains what is inside the chip; here we stay with what it does in a device.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 1 · Interactive
01 / 10

Computers hidden inside things

An embedded system is a computer built into a device to do one job. Thermostats, washing machines, cars and watches all have one. You never see it or install apps on it: it just runs the device.

embedded system  a computer hidden inside a device,
                 built for one dedicated job
  thermostat
  washer
  car
  watch
no screen of apps, no keyboard: it just runs the device
Loading figure…
or ← →
Read the lesson step by step
  1. Computers hidden inside things

    An embedded system is a computer built into a device to do one job. Thermostats, washing machines, cars and watches all have one. You never see it or install apps on it: it just runs the device.

    embedded system  a computer hidden inside a device,
                     built for one dedicated job
      thermostat
      washer
      car
      watch
    no screen of apps, no keyboard: it just runs the device
  2. A computer on one chip

    Open our thermostat and you find a microcontroller, or MCU: processor, memory and input/output pins on a single chip. Ours is the ATmega328P on an Arduino Uno R3 board, a common one to learn on.

    MCU      ATmega328P  (board: Arduino Uno R3)
      processor   carries out instructions
      flash       32 KB, keeps the program with the power off
      SRAM        2 KB, the working values, lost at power off
      pins        wires to sensors and switches
    all of it on one chip
  3. One program, forever

    The chip runs just one program, called firmware. It is copied into flash memory, which keeps it with the power off. At power on the chip starts it, and loop() repeats until the power goes.

    firmware  the one program on the chip
    flash     32 KB, survives power off
    power on  -> setup() once -> loop() again and again
    
    // Arduino Uno R3: TMP36 sensor on A0, heater relay on pin 8
    const int HEAT = 8;
    
    int readTemp() {                  // degrees C from a TMP36
      int raw = analogRead(A0);       // 0..1023 for 0..5 V
      return raw * 500L / 1024 - 50;  // 10 mV per degree, 0.5 V at 0 C
    }
    
    void setup() {
      pinMode(HEAT, OUTPUT);
    }
    
    void loop() {
      int t = readTemp();
      digitalWrite(HEAT, t < 20);
      delay(1000);
    }
  4. Sense: read the temperature

    The sensor puts out about 0.69 volts. analogRead turns that into a number from 0 to 1023: 141. readTemp turns 141 into 18 degrees and keeps it in t, a variable in SRAM.

    line 2    int t = readTemp();
    sensor    TMP36 says about 688 mV  (18.8 C)
    ADC       688 mV of 5000 mV, 10 bits  ->  raw 141
    readTemp  141 * 500 / 1024 - 50 = 18
    SRAM      t = 18
  5. Decide: is 18 below 20?

    Next line: compare t with 20 degrees. 18 < 20 is true, so the room is too cold. In Arduino code true counts as 1, and 1 is HIGH: the pin should turn on.

    line 3    digitalWrite(HEAT, t < 20);
    compare   18 < 20  ->  true
    true = 1 = HIGH   false = 0 = LOW
  6. Act: switch the heater on

    digitalWrite sets pin 8 HIGH: 5 volts. That small signal closes a relay, a switch for bigger currents, and the heater turns on. Sense, decide, act: that is the whole job.

    line 3    digitalWrite(HEAT, 1)
    pin 8     HIGH = 5 V
    relay     closes  ->  heater on
    sense -> decide -> act
  7. Wait, then do it again

    delay(1000) waits one second, then loop() runs again. Now it reads 21, not below 20, so the heater turns off. Some answers must also come on time: an airbag that fires late fails. Systems with deadlines are called real-time.

    line 4    delay(1000);      one second
    loop      again: raw 146  ->  t = 21
    compare   21 < 20  ->  false   ->  pin 8 LOW, heater off
    real-time: a late answer is a wrong answer
  8. Limit: 32 KB and 2 KB

    The whole program must fit in 32 KB of flash, and every variable in 2 KB of SRAM. An int takes 2 bytes, so 2,048 bytes hold at most 1,024 of them. Bigger chips have more: an ESP32 has 520 KB of SRAM.

    flash  32 KB    the program
    SRAM   2 KB  = 2,048 bytes, the variables
    int    2 bytes on the Uno  ->  at most 1,024 ints
    ESP32  520 KB SRAM, for comparison
  9. Limit: 16 MHz

    A clock ticks 16 million times a second, and most instructions take one tick: up to 16 million simple steps a second. Plenty for a thermostat, but heavy jobs, like video, need a much faster chip.

    clock    16 MHz = 16 million ticks per second
    most instructions: 1 tick
    so up to 16 MIPS (million instructions per second)
    ESP32    up to 240 MHz, for comparison
  10. Limit: the battery

    At 1 MHz and 1.8 V the chip draws about 0.2 mA awake and 0.1 µA asleep: 2,000 times less. Battery devices sleep between readings. Our delay() keeps the chip awake, so a later lesson replaces it.

    awake   0.2 mA   (1.8 V, 1 MHz)
    asleep  0.1 µA   (power-down, 1.8 V)
    ratio   2,000 times less while asleep
    delay() waits awake: no energy saved

Quiz Unlocks after the last step.