Teach lesson
STM32 Mbed CodeIDE (4/8): serial debugging with printf
Students use printf serial debugging in STM32 Mbed CodeIDE to compare expected and actual behavior, then fix a mismatch with evidence.
New to LabsLand? Create your teacher account
Learning Outcomes
Use
printf()to observe what a running Mbed program is doing.Print integer values instead of relying on floating point formatting.
Use serial output to debug one controlled change at a time.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
Why serial output matters
8 min
LEDs cannot tell you what values the code was using when something went wrong. A controller often needs an instrument panel for the programmer, and the serial console provides that panel so you can find a timing or logic mismatch instead of guessing from the camera alone.
When a controller does not behave as expected, guessing is slow. Serial output lets you print values and checkpoints:
printf("cycle=%d led=%d\n", cycle, led_state);
In these Mbed programs, printf() sends text from the running STM32 board to the serial console in CodeIDE. The %d markers are replaced by integer values, and \n starts a new line so each reading is easier to copy.
This course uses integer serial output. In this Mbed environment, floating-point printf("%f") output is not a good first debugging tool. Use integers such as milliseconds, millivolts, percentages, and degrees.
In the STM32 Mbed CodeIDE lab, serial output appears in the console panel near the bottom of the lab view. CodeIDE manages the USB serial connection and baud rate for you; just compile, upload, and read the printed lines.
How printf reaches the CodeIDE console
After upload, read the Console area near the bottom of CodeIDE. That is
where lines printed by the running printf() calls appear.
Which check best tests whether a printed timing value describes the physical program?
Print a running heartbeat
26 min
Plan for two builds and about 22 minutes of active lab time: one required buggy run and one corrected run. If queued, prepare the table and a five-interval timing method. Do not diagnose by changing the source before you have serial and camera evidence from build 1.
Open the STM32 Mbed CodeIDE lab.
Open
main.cpp.Replace the file with the program below exactly. This supplied version contains a deliberate timing/reporting fault; do not correct it yet.
Save, compile build 1, and upload the buggy version.
Open or watch the serial console area in CodeIDE.
Observe the serial lines and confirm that the cycle number increases while the LED changes.
Copy at least three serial lines before changing the program.
With the camera, time five consecutive LED state-change intervals in seconds. Divide by 5 for one observed interval.
Keep build 1 running until the table and timing evidence are complete.
#include "mbed.h"
DigitalOut status_led(PB_5, 0);
int main() {
int cycle = 0;
const int interval_ms = 500;
const auto interval = 1000ms;
printf("STM32 Mbed serial debug start\n");
while (true) {
status_led = !status_led.read();
cycle = cycle + 1;
printf("cycle=%d led=%d interval_ms=%d\n", cycle, status_led.read(), interval_ms);
ThisThread::sleep_for(interval);
}
}
The first lines should look similar to this:
STM32 Mbed serial debug start
cycle=1 led=1 interval_ms=500
cycle=2 led=0 interval_ms=500
cycle=3 led=1 interval_ms=500
Fill three table rows using different cycle=... lines, not the startup line. Paste each full line. For Camera behavior at that line, record the LED state or change visible at about the same time; do not assume that raw 0/1 universally means on/off. Write a short observation such as "cycle increased by 1" or "the printed LED value alternated." Judge timing only from the five-interval comparison below.
Serial evidence table
| Console line copied | Camera behavior at that line | What this tells me |
|---|---|---|
For buggy build 1, answer with these three lines:
1. Printed interval: ... ms
2. Measured time for 5 intervals: ... s
3. One-interval calculation: (... s / 5) × 1000 = ... ms
Do not propose a fix yet.
Find a serial mismatch
13 min
Use only the serial line, your camera timing, and the two timing constants in the code to diagnose build 1.
Paste one misleading build-1 serial line. State the claimed interval and camera-measured interval, each with units, then identify which two code expressions disagree. Do not report corrected evidence in this answer.
After answering, replace the two timing declarations with this correction template. It contains exactly two compile-blocking placeholders. Choose either a 500 ms or 1000 ms interval, but both TODO replacements must represent the same duration. Search for TODO before compiling.
// 1: integer milliseconds printed in the serial line.
const int interval_ms = TODO_REPORTED_MS;
// 2: the same duration as an Mbed chrono literal, including ms.
const auto interval = TODO_WAIT_MS;
Save, compile build 2, upload, and repeat the same five-interval camera measurement. Only after recording the corrected evidence, click Mark practice as done.
For corrected build 2, answer in this format:
1. Serial line: ...
2. Printed interval: ... ms
3. Measured time for 5 intervals: ... s
4. One-interval calculation: (... s / 5) × 1000 = ... ms
5. Conclusion: they agree/do not agree within camera timing uncertainty because...
Submit your code
8 min
Attach your saved final main.cpp
Click Check saved files, confirm that main.cpp contains the corrected, uploaded version and no TODO. Then attach and submit.