Micro Controllers lessons
What is a microcontroller?
Opener: a sketch reads a knob on A0, works out a PWM duty and dims an LED, line by line, with the square wave at its real duty.
A microcontroller is a small computer on one chip. It holds a processor, memory, and pins that connect to the outside world.
It runs one program, called a sketch, in a loop that never ends. The one above reads a knob and sets an LED's brightness, slowed down so you can follow it.
A real board runs that loop thousands of times a second. Microcontrollers hide inside washing machines, cars, remote controls and toys.
One chip or a whole computer?
A microprocessor, like the one in a laptop, is only the processor. It needs separate chips for memory and storage, and an operating system to run programs.
A Raspberry Pi is a whole computer on one board, running Linux. A microcontroller keeps its sketch in flash memory and its working values in RAM, both inside the chip, with no operating system at all.
Example: a microcontroller, a laptop processor and a Raspberry Pi compared fact by fact, with each part lit where it sits.
That makes a microcontroller tiny, cheap and instant to start. It sips power, so it can run for months on batteries.
Three popular boards
A chip on its own is hard to use, so most people start with a development board. It adds a USB port, a power regulator and rows of pins.
The Arduino Uno uses an ATmega328P and works at 5 volts. The ESP32 DevKit adds Wi-Fi and Bluetooth, and the Raspberry Pi Pico uses the RP2040; both of those work at 3.3 volts.
3D example: the Uno, the ESP32 DevKit and the Pico, each toured from processor to memory, radio and pins.
Newer versions keep the same layouts. The Uno R4 and the Pico 2 have faster chips, and the Pico W adds Wi-Fi.
Practice: Choose a board
Pick the Uno, ESP32, Pico or Raspberry Pi for each project, with a spec table to read (ChoiceQuiz).
A tour of the Uno
Most of an Uno is there to help its chip. A USB chip lets your computer talk to it, and a voltage regulator turns the barrel jack's supply into a steady 5 V.
3D tour of the Uno: the chip, USB, power, the L LED on pin 13, and the digital and analog headers.
The pins sit in headers along two edges. Digital pins 0 to 13 run along one edge, and the power pins and analog inputs A0 to A5 along the other.
Pins marked with a ~ can do PWM, which comes later. Pin 13 also lights the small L LED on the board.
Practice: Find the pin
Tap the right pin on a 2D chart of the Uno's headers, found by number, job or label.
Writing and uploading a sketch
You write sketches in Arduino IDE 2, a free editor. Every sketch has a setup() function that runs once at power-on, and a loop() function that repeats forever.
Upload compiles the sketch into machine code and sends it over USB into the chip's flash. The board then restarts and runs it.
Example: Blink is compiled and uploaded in the IDE, the TX and RX lights flicker, and the L LED starts blinking.
The ESP32 and the Pico can also run MicroPython. You load it onto the board once, then write Python in an editor such as Thonny, with while True: in place of loop().
Digital output: blink
A digital pin is either HIGH or LOW. On an Uno, HIGH is 5 V and LOW is 0 V.
pinMode(13, OUTPUT) lets the sketch drive pin 13, and digitalWrite(13, HIGH) switches it on. delay(500) waits 500 milliseconds before the next line runs.
Example: three blink timings run in real time, with the line running, the L LED and a trace of pin 13.
One blink takes the on time plus the off time. With delay(250) twice, the LED blinks twice a second.
Practice: Match the blink
Pick the trace of pin 13 that matches a blink sketch's delays (ChoiceQuiz, picture tiles), random timings.
Buttons, pull-ups and bounce
A button joins a pin to ground while you press it. pinMode(2, INPUT_PULLUP) turns on a pull-up resistor inside the chip, which holds the pin HIGH the rest of the time.
So digitalRead(2) gives LOW while the button is pressed. That flip catches everyone out at first.
Example: a button with the internal pull-up is pressed; the pin drops to 0 V, and a close-up of the first milliseconds shows the contacts bouncing.
Metal contacts also bounce, joining and parting a few times in the first milliseconds. A simple fix is to ignore the pin for about 50 milliseconds after it changes.
Practice: Code the button
Type the pinMode line or the if condition for a button with the internal pull-up (TypedCode), random pins.
Analog input
An analog input measures a voltage, not just HIGH or LOW. Inside the chip, an ADC (analog to digital converter) turns the voltage into a whole number.
The Uno's ADC is 10-bit, so analogRead(A0) gives 0 to 1023 for 0 to 5 V. The ESP32 and the Pico have 12-bit converters, giving 0 to 4095 for about 0 to 3.3 V.
Example: an input voltage mapped to a reading on the Uno and on a 3.3 V board, with a close-up of the steps.
Uno: reading to volts: V = \frac{\text{reading}}{1023} \times 5\ \text{V}
The serial monitor
A microcontroller has no screen, so it sends text back over the USB cable. Serial.begin(9600) starts the link at 9600 bits a second, and Serial.println() sends one line.
The serial monitor in Arduino IDE 2 shows those lines. Its speed setting must match the sketch, or you see nonsense.
Example: a knob sketch prints each reading and its voltage, and the lines arrive in the serial monitor.
Practice: Convert a reading
Type the voltage for a random reading on an Uno or an ESP32 (MathAnswerInput).
PWM: dimming with pulses
An Uno pin cannot give out 2.5 V. Instead it uses PWM (pulse width modulation), switching the pin on and off about 490 times a second.
The share of time it is on is the duty cycle. analogWrite(9, 128) keeps pin 9 on about half the time, so it averages about 2.5 V and an LED looks dimmer.
Example: a fade loop steps analogWrite through 0 to 255; the wave widens, and a slowed-down view shows the LED really flashing.
Average voltage: V_{\text{avg}} = \frac{\text{value}}{255} \times 5\ \text{V}
Only the pins marked ~ can do this: 3, 5, 6, 9, 10 and 11 on an Uno. The value runs from 0, always off, to 255, always on.
Practice: Set the brightness
Type an analogWrite value for a duty, or the average voltage for a value (TypedAnswer), random rounds.
5 V and 3.3 V logic
An Uno uses 5 V logic: it reads anything above 3 V as HIGH and below 1.5 V as LOW. The ESP32 and the Pico use 3.3 V logic, so their thresholds are lower.
Never feed 5 V into a pin on a 3.3 V board. It can damage the chip for good.
Example: real signals placed on the Uno's and the ESP32's input scales: LOW, unsure, HIGH or damaging.
To join them safely, use a level shifter, or a voltage divider that brings 5 V down to about 3.3 V. A 3.3 V signal into an Uno usually works, because it is just above the 3 V line.
Practice: Safe to connect?
Sort connections between boards and modules into safe and damaging (SortGroups).
The ESP32 and Wi-Fi
The ESP32 has a 2.4 GHz Wi-Fi radio and Bluetooth built in. In Arduino IDE 2, install the esp32 package in the Boards Manager, then pick your board.
#include <WiFi.h> adds the Wi-Fi library. WiFi.begin() joins your network, and WiFi.localIP() gives the address the router handed out.
Example: the Wi-Fi sketch runs line by line while the serial monitor fills with dots, then the board's address.
Once connected, the board can send readings to a website or serve a small web page of its own. Many smart home sensors work this way.
Practice: Complete the Wi-Fi sketch
Drag the missing pieces into an ESP32 Wi-Fi sketch (FillBlanks).
UART, I2C and SPI
Boards talk to modules over a few standard protocols. UART joins two devices with a TX and an RX wire, crossed over, and both sides agree on a speed.
I2C shares two wires, SDA for data and SCL for the clock, between many devices, each with its own address. SPI is faster: every device shares a clock and two data wires, and gets its own chip select wire.
Example: a byte crossing a UART link, an I2C call to one address, and SPI choosing a device with its chip select wire.
Practice: Wire the module
Match each module pin to the board pin it wires to: I2C, UART and SPI modules (MatchPairs).
Power and sleep
An Uno runs on 5 V from USB, or on 7 to 12 V through its barrel jack or VIN pin. Its regulator turns that into a steady 5 V, and a smaller one makes 3.3 V.
In deep sleep, an ESP32 chip draws about 10 µA instead of around 100 mA with Wi-Fi on. A timer or a pin wakes it, and the sketch starts again from setup().
Example: an ESP32 sensor wakes for part of each minute; average current and battery life follow the awake time.
Battery life: \text{hours} = \frac{\text{capacity in mAh}}{\text{average current in mA}}
Practice: Battery life
Type a board's average current or its battery life in hours (MathAnswerInput), random clean values.
Build a night light
A complete project joins an input, a decision and an output. A night light reads a light sensor, compares it with a threshold, and switches an LED.
Build it one part at a time. Print the sensor's readings on the serial monitor first, then pick a threshold between your day and night readings.
Example: the night light runs from day to night, with the reading, the threshold, the branch taken and the LED.
Practice: Final review
Mixed questions from the whole course (ChoiceQuiz).