Please enable JavaScript to use CodeHS

Physical computing

What Is a Microcontroller?

The tiny computer inside everything

Your microwave has one. So does your TV remote, your car key, and your game controller. It is a whole computer on a chip the size of a postage stamp. Find them, open one up, and then program a virtual board to blink lights and read buttons.

Electronics + coding ~25 min Beginner friendly

Pins on the outside. A whole computer inside.

01 / MEET THE CHIP

A whole computer on one tiny chip

Look around the room. Count the things that have a button, a light, or a beep. Many of them have a computer hiding inside. It is not a laptop. It is much smaller and simpler. It is called a microcontroller.

A microcontroller is a tiny computer on a single chip. “Micro” means small. “Controller” means it controls things, like a light, a motor, or a speaker. One chip holds the brain, a little memory, and the metal legs that connect it to the real world. Industry estimates say tens of billions of them are made every year, so there are far more of them than people.

Before we go on, here are the words you will use again and again. Each one shows up in the lab later.

Microcontroller
A tiny computer on one chip, built to do one job, like running a microwave.
Pin
A metal leg on the chip. You connect a wire to a pin so the chip can talk to a part.
Input
Information coming into the chip through a pin, like a button press.
Output
A signal going out of a pin, like power that lights an LED or sounds a buzzer.
Sensor
A part that measures the world, like light, heat, or motion, and turns it into an electrical signal.
Digital
A signal with only two states: on (1) or off (0). A light switch is digital.
Analog
A signal that can be anywhere in a range, like a dimmer knob. It has many in-between values.
Program
A list of instructions for the chip. A program stored inside a device is called firmware.
Loop
A set of instructions that repeats again and again. Most microcontroller programs are one big loop.
02 / SPOT THE CHIPS

Find the hidden computers

This house is full of microcontrollers. Tap each glowing dot to look inside. Try to find all eight. For each one you will see what it senses, what it thinks, and what it does.

KITCHEN LIVING ROOM GARAGE 68°
0 of 8 found

Tap a glowing dot to look inside. You can also pick from the list below.

You found them all. Notice the pattern: every one of them senses something, thinks a little, and then acts.

Which chip is inside depends on the brand and the model. Some bigger gadgets use a mix of chips, but a microcontroller is a very common choice for jobs like these.

03 / TINY VS BIG

Microcontroller vs laptop

A laptop is also a computer. So why not put a laptop in a microwave? Because a microwave needs a computer that is cheap, small, starts instantly, and sips power. A laptop is built for something different. Drag the slider to make the job bigger and watch which kind of computer fits.

Microcontroller

Laptop

Side by side (typical numbers, they vary a lot)
MicrocontrollerLaptop computer
JobOne job, like running a washing machineMany jobs at once: games, homework, video calls
SoftwareUsually no operating system. One program runs straight on the chip.An operating system like Windows, macOS, or Linux runs many apps.
MemoryOften a few kilobytes to a few hundred kilobytes of RAMOften 8 to 16 gigabytes of RAM, sometimes much more
SpeedOften a few million to a couple hundred million steps per secondOften billions of steps per second, on several cores
PowerCan sleep and run for months on a small batteryNeeds a big battery or a wall plug
CostThe chip often costs from under a dollar to a few dollarsA laptop costs hundreds of dollars
Start-upAlmost instantMany seconds to boot
04 / OPEN IT UP

Inside the chip

Every microcontroller has the same few parts. Pick one to see what it does. The yellow part on the chip shows where it lives.

Power test
FLASHProgram: blink saved
RAMVariable: counter = 42

Power is on. The program runs and keeps a counter in RAM.

Real boards you can buy

A board is a small circuit board that holds a microcontroller chip and makes its pins easy to reach. Here are three popular ones. The numbers are for common versions, and newer versions exist.

Arduino Uno

Chip: ATmega328P (8-bit)

  • Speed16 MHz
  • Flash32 KB
  • RAM2 KB

A classic for learning. You program it in an Arduino style of C++, which is what our simulator copies.

micro:bit

Chip: nRF52833 (Arm Cortex-M4) in v2

  • Speed64 MHz
  • Flash512 KB
  • RAM128 KB

Has LEDs, buttons, and sensors built in, plus Bluetooth. You can code it with blocks or Python.

Raspberry Pi Pico

Chip: RP2040 (two Arm Cortex-M0+ cores)

  • Speedup to 133 MHz
  • Flash2 MB (on the board)
  • RAM264 KB

Small and cheap. You can program it in MicroPython or in C and C++.

Not the same thing: a full Raspberry Pi is a different kind of computer. It runs an operating system like Linux, has gigabytes of memory, and works like a tiny laptop. The Raspberry Pi Pico is a microcontroller board. The names are close, but the two do very different jobs.

05 / TALKING THROUGH PINS

Digital vs analog pins

Pins are how a microcontroller feels and acts. In your program you tell each pin what to do. An input pin listens. An output pin speaks. Pins talk in two ways.

  • Digital means two choices: on or off. We write that as 1 and 0, or HIGH and LOW. A push button is digital. It is either pressed or not.
  • Analog means a whole range of values. A knob, a light sensor, or a temperature sensor is analog. The chip measures how much, not just yes or no.

Try all three below. The first two are inputs. The last one is an output.

Digital inputA button on pin 2

Hold the button. The pin reads 1 while it is pressed and 0 when it is not. Nothing in between.

digitalRead(2)0

Analog inputA light sensor on pin A0

Slide to change how much light hits the sensor. The chip turns the voltage into a number from 0 to 1023.

analogRead(A0)512

That is about 2.5 volts on a 5 volt board.

Output with PWMDim an LED on pin 9

A pin can only be fully on or fully off. To dim a light it flicks on and off very fast. More on-time looks brighter.

analogWrite(9, 128)50%

Why 0 to 1023?

A part of the chip called the ADC (analog-to-digital converter) measures a voltage and turns it into a whole number. On many boards, including the Arduino Uno, it uses 10 bits. Ten bits can count 1,024 values, which is 0 to 1023. Some boards use more bits and give a bigger range.

PWM in plain words

PWM stands for pulse-width modulation. It is a trick. The pin switches on and off hundreds of times each second, much too fast for your eyes to see. The share of time it spends on is called the duty cycle. On half of the time looks half as bright. On all of the time is full brightness. On Arduino, analogWrite takes a number from 0 (always off) to 255 (always on). Only some pins can do this. On an Arduino Uno they are marked with a ~.

06 / THE LAB

Program a virtual board

Now you are the programmer. This virtual board has a red LED on pin 13, an amber LED on pin 9, a buzzer on pin 8, a button on pin 2, and a light sensor on pin A0. Write code, press Run, and watch it work. Four challenges check your answers.

A program has two main parts. setup() runs once when the board starts. loop() runs again and again forever. You do not need a while loop to make it repeat.

    
                    
                
    MCU VIRTUAL BOARD PIN 13 ~PIN 9 PIN 8 PIN 2 PIN A0
    • 13OUTLOW
    • ~9OUT0
    • 8OUTLOW
    • 2IN0
    • A0IN600
    Button (pin 2)
    darkbright
    Ready. Press Run to start the board.
      Command cheat sheet
      void setup() { ... }Runs once, when the board starts.
      void loop() { ... }Runs again and again.
      pinMode(13, OUTPUT);Make a pin an output. Use INPUT to listen.
      digitalWrite(13, HIGH);Turn a pin on. LOW turns it off.
      digitalRead(2)Is the pin on? Gives HIGH (1) or LOW (0).
      analogRead(A0)Read a number from 0 to 1023.
      analogWrite(9, 128);Dim a pin from 0 to 255. Only pin 9 here.
      delay(500);Wait 500 milliseconds (half a second).
      if (x < 300) { ... } else { ... }Choose what to do. Compare with < > == !=.
      int x = 5;Make a variable. Whole-number division drops the remainder.
      Serial.println(x);Print a value in the console.
      tone(8, 440); noTone(8);Start and stop the buzzer.

      Good to know: capital letters matter, so digitalWrite works but digitalwrite does not. Every command ends with a semicolon. This is a small, forgiving version of the Arduino language, and it runs safely in your browser, so you cannot hurt anything.

      07 / THE BIG LOOP

      The sense-think-act loop

      Look back at your lab programs. Each one did the same three things, over and over:

      1. Sense. Read an input, like the button or the light sensor.
      2. Think. Decide what to do, usually with an if.
      3. Act. Change an output, like the LED or the buzzer.

      That is the heartbeat of every microcontroller, from a night light to a robot. Robots use the same loop, just with more sensors and motors. If you want to see it at robot scale, read What Is a Robot?. And every LED you wire up on a real board needs a resistor to stay safe. Ohm’s Law and Simple Circuits shows you why.

      Step through one trip around the loop for three real devices. Press Next step.

      1. 1. Sense

      2. 2. Think

      3. 3. Act

      Trip 1
      
          
      08 / BUILD SOMETHING

      Projects to try next

      Ready for real hardware? Each project below uses the same sense-think-act loop you just practiced. Pick a difficulty to filter them. The parts are ideas, not a shopping list, and many starter kits include most of them.

      • Burglar alarm

        Easy

        Sound a buzzer when a door opens, unless you press the secret button first.

        Parts

        • Board and USB cable
        • Magnetic door switch (reed switch)
        • Buzzer and an LED with a resistor
        • Push button to arm it

        You practice: digital inputs, if, and buzzers.

      • Plant waterer

        Medium

        Check the soil and run a small pump for a few seconds when it is too dry.

        Parts

        • Board with a USB or battery pack
        • Soil moisture sensor
        • Small low-voltage pump, tubing, and a cup of water
        • Relay or transistor that suits the pump

        You practice: analog inputs, thresholds, and a safety rule that limits pump time.

      • Game controller

        Medium

        Build a controller with buttons and a thumbstick that works as a USB gamepad or keyboard.

        Parts

        • A board that can act as a USB keyboard or gamepad (some can, some cannot)
        • 4 to 6 push buttons
        • Thumbstick module
        • Breadboard and wires

        You practice: digital and analog inputs together, and ignoring button bounce.

      • Weather station

        Medium

        Show the temperature and humidity on a tiny screen and keep a log of how they change.

        Parts

        • Board and USB power
        • Temperature and humidity sensor
        • Small display (or just the serial monitor)

        You practice: using a library, reading a sensor, and showing data.

      • Smart bird feeder

        Hard

        Count the birds that visit and send the total to your phone or a web page.

        Parts

        • Board with Wi-Fi
        • Light-beam or weight sensor
        • Weatherproof box for the electronics
        • Battery pack or USB power

        You practice: counting events, saving power, and keeping a connected device secure.

      Safety first: power your projects with USB or small battery packs only. Never connect anything you build to a wall outlet. Wall power can seriously hurt you. Ask an adult before using pumps, heaters, or rechargeable lithium batteries, because those can get hot if they are damaged or shorted. Keep water away from the electronics, and put a resistor in line with every LED.

      09 / WORK WITH TINY COMPUTERS

      Careers, and why tiny devices need updates

      Someone has to write the program inside every microwave, car, and medical device. Here are four jobs that work with microcontrollers.

      Embedded engineer

      Writes the firmware that runs inside cars, appliances, and medical devices. It has to be fast, small, and reliable.

      Start with: Arduino or Pico projects, math, and physics.

      Robotics engineer

      Combines sensors, motors, and microcontrollers so a machine can move and react. Motor control often runs on a tiny chip.

      Start with: a robotics club and What Is a Robot?

      IoT developer

      IoT means the “Internet of Things.” These engineers connect small devices, like smart plugs and farm sensors, to the internet and make them last on a battery.

      Start with: networking basics and low-power design.

      Hardware security researcher

      Tests how devices can be broken so makers can fix the problems first. They only test devices they own or have permission to test.

      Start with: how firmware works, then ethical hacking.

      10 / QUICK ANSWERS

      Quick answers

      What is a microcontroller in simple words?

      A microcontroller is a tiny computer on a single chip. It has a processor, a small amount of memory, and pins that connect to buttons, sensors, lights, and motors. It usually runs one program that repeats, such as the program inside a microwave or a TV remote.

      What is the difference between a microcontroller and a microprocessor?

      A microprocessor is mostly the processor alone, so a computer built around it needs separate memory, storage, and other chips. A microcontroller puts the processor, memory, and input and output pins on one chip, which makes it small, cheap, and low power.

      Is an Arduino a microcontroller?

      An Arduino is a circuit board that has a microcontroller chip on it, plus power parts and header sockets that make the pins easy to reach. The Arduino Uno, for example, is built around an ATmega328P microcontroller.

      Is a Raspberry Pi a microcontroller?

      A full Raspberry Pi is not. It is a small computer that runs an operating system like Linux, much like a tiny laptop. The Raspberry Pi Pico is different: it is a microcontroller board that runs one program at a time.

      What programming languages do microcontrollers use?

      Many boards use C or C++, such as the Arduino language. Others run MicroPython or CircuitPython, and beginner boards like the micro:bit also offer block coding. All of them follow the same idea: a setup step that runs once and a loop that repeats.

      Do I need to buy a board to start learning?

      No. You can practice the core ideas in a simulator like the one in this tutorial. When you are ready for real hardware, a starter kit with a board, LEDs, buttons, and wires often costs somewhere around twenty to fifty dollars.

      11 / CHECK YOURSELF

      Quick quiz

      1. Which job is the best fit for a microcontroller?

      2. Which of these is an analog input?

      3. You unplug a board and plug it back in. What is still there?

      Keep learning about physical computing

      You now know what a microcontroller is, what is inside it, and how to program the sense-think-act loop. Next, see how a whole robot puts those pieces together, or go deeper into the electronics that power them.

      Related: Binary Explorer and What Is CAD?