MakerSpace Amiens · Microcontrollers
Communicating and structuring
Lecture 3: serial buses, game loop and state machine
Alban Petit
On the agenda
Why buses
UART, I2C, SPI
Choosing your bus
The game loop
The state machine
1 h 30, last lecture of the series, just before the labs. Keep time for the Pong FSM exercise: an FSM is drawn on paper, not in front of a screen.
Open with the Lecture 2 quiz: high impedance and open-drain come back with I2C, millis() with the game loop.
2
What you will be able to do
By the end of this lecture, you will be able to:
say why a serial bus exists, and tell UART, I2C and SPI apart
choose the right bus for a peripheral, and wire it correctly
write a game loop that never blocks
recognise a situation where a state machine is needed
implement it with an enum and a switch
The first two make the chip talk to the world; the last three keep your code readable.
Communication buses Finite state machine
3
Quiz: what is left from Lecture 2
Between 0.8 V and 2.5 V, what does an ESP32-S3 input read?
Why does an unconnected input read anything at all?
With INPUT_PULLUP, what is read when the button is pressed?
Can an open-drain output impose a HIGH?
Why does a single press sometimes count twice?
Which pins to read the joystick while Wi-Fi is running?
How do you run a 12 V motor from a pin?
Same format as in Lecture 2: on a sheet of paper, no notes, 5 min, show-of-hands correction. Questions 2 and 4 (high impedance, open-drain) prepare I2C, question 5 (millis()) the game loop: insist on them if they were a struggle.
4
Quiz: answers
Answer
1
Nothing reliable: it is the undefined zone
2
It floats : high impedance, nothing fixes its voltage
3
LOW: the logic is inverted
4
No : it pulls to 0 V, a pull resistor provides the HIGH
5
Contact bounce ; filter it with millis()
6
The ADC1 pins (ADC2 is unusable with Wi-Fi)
7
A “logic level” MOSFET , and a flyback diode
High impedance, open-drain and millis() come back today, with I2C and the game loop.
GPIO and the digital world ADC and PWM
5
Why buses
Making components talk
The course’s display (240 × 320) receives 76,800 pixels per frame , i.e. more than 150,000 bytes in 16-bit colour. A sensor returns a temperature on 16 bits. An SD card, blocks of 512 bytes.
Wiring one wire per bit would be impractical: no pins left, an unreadable board, and interference between tracks.
A serial bus sends the bits one after the other , over two to four wires. Width is traded for time, and the chip is fast enough for it not to show.
Communication buses
7
Serial or parallel
In parallel, one wire per bit: fast, but bulky. In serial, a single data wire: the bits go one after the other.
Communication buses
8
Four questions to compare two links
Question
The two possible answers
How many parties?
Point-to-point (two) or multipoint (several)
A shared clock?
Synchronous (yes) or asynchronous (no)
Both directions at once?
Full-duplex (yes) or half-duplex (taking turns)
How is the recipient chosen?
An address in the message, or a select wire
These four answers are enough to describe UART, I2C and SPI.
Communication buses
9
Three buses, three trade-offs
UART : the simplest. Two wires, a single party, no shared clock
I2C : two wires, several components told apart by an address
SPI : four wires, a clock, the fastest of the three
None is better in absolute terms: each trades speed for wires, or wires for complexity. The choice is made by the need.
Communication buses
10
UART
Two wires, point-to-point
The simplest of the three
Point-to-point , full-duplex , asynchronous : without a clock, the speed (the baud rate , often 115,200 baud) is set in advance on both sides.
Communication buses
12
One device’s TX connects to the other’s RX, and vice versa. TX-to-TX wiring transmits nothing, and it is silent: no error message will warn you, you will just see a module that “does not answer”.
Communication buses
13
Anatomy of a frame
The start gives the signal, the receiver counts the bits at the agreed speed. 8 bits, no parity, 1 stop: 8N1 . Different format or speed: gibberish.
Communication buses
14
Where you already use it
Serial . begin ( 115200 ); // the serial monitor port
Serial . printf ( "x=%d y=%d \n " , x , y );
The serial monitor is handled like a UART. UART is also the bus of GPS, Bluetooth or GSM modules, and the simplest way of making two microcontrollers talk.
On many boards, a USB-serial chip (CP2102, CH340) translates UART into USB and requires a driver on Windows. The XIAO has none: Serial goes directly through the ESP32-S3’s native USB, and UART0 stays free on D6/D7.
The serial port in VSCode Checking the toolchain
16
I2C
Two wires, several components
One bus, addresses
Multipoint , synchronous (SCL), half-duplex . Each slave has a 7-bit address ; the master writes it at the head of each message.
Communication buses
18
An I2C exchange
Start , address + R/W , slave ACK , data (one ACK per byte), stop . No ACK after the address: nobody answers at that address.
Communication buses
19
Pull-ups are not optional
SDA and SCL are open-drain , as in Lecture 2: components can only pull the line LOW, never HIGH. This is precisely what allows several of them to share the wire without a short circuit.
Two pull-up resistors (~4.7 kΩ) to 3.3 V are therefore needed. Without them, the line never rises and nothing communicates .
Most commercial modules already include them. If you chain several, a single pair is enough for the whole bus: no need to stack them. ↓ for the details.
Communication buses GPIO and the digital world
20
Sharing a wire without a short circuit
Anyone can pull the line to 0 or release it, never impose a 1: two components cannot contradict each other.
GPIO and the digital world
20.1
Who is there? The I2C scanner
You do not always know the address. Sweep them all:
for ( byte addr = 1 ; addr < 127 ; addr ++ ) {
Wire . beginTransmission ( addr );
if ( Wire . endTransmission () == 0 ) {
Serial . printf ( "Found: 0x%02X \n " , addr );
}
}
Missing from the scan? The problem is the wiring or the pull-ups.
Communication buses
21
Standard speeds
Mode
Speed
Standard
100 kHz
Fast
400 kHz
Fast+
1 MHz
High Speed
3.4 MHz
More than enough for sensors or a small monochrome OLED display, over one or two metres at most. Far too slow for a colour TFT display: at 400 kHz, a 240 × 320 pixel frame would take more than three seconds.
Communication buses
22
SPI
Four wires, full speed
The display’s bus
Wire
Role
MOSI
Data from master to slave
MISO
Data from slave to master
SCK
Clock, generated by the master
CS
Chip Select : one wire per slave
Synchronous transmission (the clock paces each bit, so no baud rate to agree on) and full-duplex (both directions at once).
Communication buses
24
One master, several slaves
No address: the CS at 0 designates the slave. The others leave MISO in high impedance. From a few MHz up to 80 MHz on the ESP32-S3.
Communication buses
25
An SPI exchange
At each clock pulse, one bit leaves and one bit comes back: full-duplex . No address, no acknowledgement: that is what makes it so fast.
Communication buses
26
The display wiring
Signal
Pin
Role
SCK
D9 (GPIO8)
SPI clock
MOSI
D10 (GPIO9)
Data from CPU to display
DC
D8 (GPIO7)
Command / data switch
RESET
D7 (GPIO44)
Display hardware reset
CS
GND
Display always selected
LED
3V3
Backlight, always on
MISO is not wired (write only) and CS is at GND : a single slave, always selected.
ST7789 SPI TFT display
27
The display wiring, wire by wire
CS is connected to GND: a single slave, always selected.
The modules of the kit
27.1
In practice, you will not write the SPI
lib_deps =
adafruit/Adafruit ST7735 and ST7789 Library@^1.11.0
Adafruit_ST7789 tft ( & SPI , - 1 , D8 , D7 ); // CS (none), DC, RESET
SPI . begin ( D9 , - 1 , D10 , - 1 ); // SCK, MISO, MOSI, CS
tft . init ( 240 , 320 , SPI_MODE3 );
tft . fillScreen ( ST77XX_BLACK );
Wrong pins give a black screen, not a compilation error. If the screen stays off while everything compiles, first check the pins passed to SPI.begin() (SCK on D9) and SPI_MODE3, mandatory when CS is connected to GND.
The modules of the kit
28
Choosing your bus
The dashboard
Criterion
UART
I2C
SPI
Wires (excluding ground)
2
2
3 + 1 CS per slave
Link
point-to-point
multipoint
multipoint
Clock
asynchronous
synchronous
synchronous
Direction
full-duplex
half-duplex
full-duplex
Recipient
none to choose
7-bit address
CS wire
Acknowledgement
no
yes (ACK)
no
Speed
~1 Mbit/s
0.1 to 3.4 Mbit/s
up to 80 Mbit/s
Typical use
debugging, modules
sensors, OLED
displays, SD card
Communication buses
30
The decision tree
flowchart LR
B{"Need<br/>speed?"} -->|Yes| C["SPI<br/>display, SD card"]
B -->|No| D{"Several<br/>components?"}
D -->|Yes| E["I2C<br/>sensors, OLED"]
D -->|No| F["UART<br/>debug, GPS, BT"]
Our project: a serial port for the monitor, SPI for the display. ↓ for the page.
Communication buses
31
The concept online
31.1
The game loop
Never block
Read, compute, display
void loop() {
static uint32_t lastFrameMs = 0;
uint32_t nowMs = millis();
if (nowMs - lastFrameMs < FRAME_TIME_MS) { // 16 ms
return; // not time yet
}
lastFrameMs = nowMs;
float joystickX = normalizeJoystick(analogRead(PIN_JOYSTICK_X), joystickCenterX);
bool startPressed = justPressed(selectButton); // 1. read
gameUpdate(game, joystickX, startPressed, nowMs); // 2. compute
rendererDraw(game); // 3. display
}
This framework will not change any more: the Pong of Lab 4 and then the project’s Snake will enrich gameUpdate() and rendererDraw().
Pong on the console
33
Why 16 ms
16 ms
per frame , i.e. 60 frames per second. Below that, the eye sees stutter.
4 M
CPU cycles available in those 16 ms, at 240 MHz. Enough to do a lot.
If a frame takes more than 16 ms to compute, the game slows down instead of skipping frames. First suspect when things are slow: a forgotten delay(), or a full redraw of the screen instead of only the modified areas.
Pong on the console
34
The state machine
Getting out of the spaghetti
Code that gets tangled
A game has several stages in its life: a welcome menu, a game in progress, a game over screen. Handled by hand, they give this:
bool inGame = false ;
bool gameOver = false ;
bool menuActive = true ;
// and it gets worse with every new phase
What does inGame && gameOver mean? Nothing. And yet nothing prevents it.
Finite state machine
36
An FSM is three things
a finite set of states , transitions , a single active state
stateDiagram-v2
[*] --> MENU
MENU --> PLAYING : START button
PLAYING --> GAME_OVER : lives == 0
GAME_OVER --> MENU : RETRY button
Finite state machine
37
The same thing, as a table
State \ Event
START
lives == 0
RETRY
MENU
PLAYING
-
-
PLAYING
-
GAME_OVER
-
GAME_OVER
-
-
MENU
A “-“ cell means “event ignored in this state”. The table translates line by line into a switch/case, and reveals the forgotten cases.
Drawing it before coding takes five minutes, and saves two hours.
Finite state machine
38
enum and switch
enum GameState { MENU , PLAYING , GAME_OVER };
GameState state = MENU ; // the current state, only one
void loop () {
readInputs ();
switch ( state ) {
// one case per state
}
}
An enum names the states, a variable remembers the active one. The switch does the rest.
Finite state machine
39
One case per state
case MENU:
if (startPressed()) { drawGame(); state = PLAYING; }
break;
case PLAYING:
updateGame(); // and redraws what has moved
if (playerLives == 0) { drawGameOver(); state = GAME_OVER; }
break;
case GAME_OVER:
if (retryPressed()) { drawMenu(); state = MENU; }
break;
A full screen is drawn once, at the transition , never at every pass. And startPressed() is only true once per press (edge).
Finite state machine
40
In the same session, the word state refers to two things: the game data (ball position, score: these are variables) and the game phase (MENU, PLAYING, GAME_OVER: this is the FSM). Keep the distinction in mind so as not to mix them up.
Finite state machine
41
Doing something only once
Resetting the score or clearing the screen must not be repeated 60 times per second. It is run at the moment of the transition , not in the case:
void goTo ( GameState next ) {
state = next ;
stateEntry = millis (); // time of entry
if ( next == PLAYING ) initGame ();
if ( next == GAME_OVER ) drawGameOver ();
}
This is the entry action (onEnter ). All the transitions go through here.
Finite state machine
42
A state that switches on its own
The “GAME OVER” screen goes back to the menu after three seconds, without blocking anything:
case GAME_OVER :
if ( millis () - stateEntry > 3000 ) {
goTo ( MENU );
}
break ;
FSM and millis() combine naturally: this is the timed transition . The time of entry into the state, stored by goTo(), serves as the reference.
Finite state machine From hardware to software
43
The timed transition, as a diagram
stateDiagram-v2
MENU --> PLAYING : press
PLAYING --> GAME_OVER : no lives left
GAME_OVER --> MENU : 3 s elapsed
The bottom arrow depends on no input: only on the time spent in the state.
Finite state machine
43.1
The canonical example: the traffic light
stateDiagram-v2
[*] --> GREEN
GREEN --> AMBER : 5 s
AMBER --> RED : 2 s
RED --> GREEN : 5 s
Three states, purely timed transitions, no button.
Finite state machine
44
The traffic light, in full
void loop () {
unsigned long el = millis () - stateEntry ;
switch ( state ) {
case GREEN : if ( el > 5000 ) goTo ( AMBER ); break ;
case AMBER : if ( el > 2000 ) goTo ( RED ); break ;
case RED : if ( el > 5000 ) goTo ( GREEN ); break ;
}
}
No delay(): the program could also read a pedestrian button.
Finite state machine
45
What the FSM gains you
Approach
Readability
Extensibility
Typical bugs
Booleans
low
hard
contradictory states
FSM enum
high
one more case
none
In the project, this FSM will extend to the network states of the multiplayer Snake.
Finite state machine
46
Activity: your Pong’s FSM
Draw your game’s state machine
What are the states? (MENU, PLAYING, PAUSE?, GAME_OVER)
Which events trigger each transition?
What is the initial state?
Fill in the table: are there any forgotten cells?
Compare with the neighbouring pair: the differences are instructive.
Classic trap to point out during the debrief: forgetting to reset the score when entering PLAYING, or allowing a RETRY during the game. This is exactly what the transition table reveals.
Finite state machine
47
Key takeaways
A serial bus sends the bits one after the other
UART : 2 wires, TX/RX crossed, same baud rate on both sides
I2C : 2 wires, addressing, ~4.7 kΩ pull-ups mandatory
SPI : 4 wires, one CS per slave, the only fast one for a TFT
The game loop paces at 16 ms with millis(), never blocking
An FSM = states + transitions + a single active state
Entry action at the transition, never in the case
Communication buses Finite state machine
48
And now: the labs, then the project
Session
What you are going to do
Lab 1
Install, flash, read the serial port
Lab 2
Test the modules of the kit: GPIO, ADC, PWM, SPI
Lab 3
Design your board in KiCad
Lab 4
Solder the board, write a Pong on it: game loop and FSM
Project
A multiplayer Snake, the FSM extended to the network
The three lectures are over: everything you have just seen is used from Lab 2 onwards.
Course syllabus The modules of the kit
49