Teach Remote lab lessons

Teach lesson

STM32 Mbed CodeIDE (1/8): first blink

Students use STM32 Mbed CodeIDE to edit main.cpp, compile and upload a DigitalOut blink program, and record LED evidence from the real board.

  • STM32 Nucleo (Mbed)
  • 50 min
  • Upper secondary / introductory vocational electronics
  • English
  • Embedded systems
Workflow from the main.cpp file through a successful Compile and Upload to board to observing the remote STM32 with the camera.
STM32 Nucleo (Mbed)

Learning Outcomes

  • Recognize the STM32 Mbed CodeIDE workflow and the main.cpp entry file.

  • Write a minimal Mbed program with DigitalOut and ThisThread::sleep_for().

  • Collect evidence that code compiled, uploaded, and changed a real output.

Student activity preview

Activity Content

Preview only. In a class session, students can fill in responses and submit their work to the teacher.

1

Before opening the lab

8 min

Embedded programming usually starts with a simple visible proof: can your code make a physical device change on command? A first blink is small, but it verifies the whole chain - edit, compile, upload, and observe the real board.

Mbed OS gives your C++ program a set of ready-made APIs for microcontroller work: digital pins, analog inputs, PWM outputs, timing, threads, and serial messages. In this lab, CodeIDE provides that Mbed environment for the remote STM32 board. Your program lives in main.cpp, includes mbed.h, starts in int main(), and controls hardware by creating Mbed objects.

For example, DigitalOut status_led(PB_5, 0); creates an output object named status_led connected to STM32 pin PB_5. Later lessons use other Mbed objects such as DigitalIn, AnalogIn, and PwmOut.

You can complete the course with the explanations here. If you want the official reference while you work, keep these pages nearby:

- Mbed OS I/O APIs
- Mbed CE API reference

From main.cpp to visible hardware

Workflow from the main.cpp file through a successful Compile and Upload to board to observing the remote STM32 with the camera.

Use Open lab, open main.cpp in Files, replace and save the code, select
Compile, then Upload to board, and finally watch the live camera. For
this first lesson, ignore the switches, buttons, potentiometers, console input,
and power meter. Save before every compile; build messages are not blink
evidence, so confirm the result with the live camera.

The starter idea is a blink: turn an output on, wait, turn it off, wait, and repeat.

In Mbed, you first create an object connected to a pin:

DigitalOut status_led(PB_5, 0);

Read that line as: "make a digital output named status_led on pin PB_5, and start it at value 0."

The board you are programming is the STM32 Nucleo WB55RG.

STM32 Nucleo WB55RG board

Photo of the STM32 Nucleo WB55RG board used in the LabsLand remote lab.

This photo shows the microcontroller board itself. It is for orientation, not for identifying the exact LED from a still image. The live remote lab may also show separate controls or training hardware, but they are not shown in this photo. You do not plug anything into your own computer; CodeIDE sends the compiled program to the remote board.

Inside while (true), the program repeats forever:

status_led = !status_led.read();
ThisThread::sleep_for(500ms);

Read the first line as: "read the current value, change it to the opposite value, and store that new value back on the output." The ! operator means "not" or "opposite" for a 0/1 value.

One state change is one transition, such as on-to-off or off-to-on. A full on-off blink cycle needs two state changes: one to turn on and one to turn off again.

For a 500ms wait after every toggle, which timing description is correct?

2

Create and test the blink

26 min

Plan for two builds and about 18 minutes of active lab time: first 500ms, then 1000ms. If the remote-lab queue is busy, answer both prediction questions and prepare the headings for both observations before your turn.

Copy every line from #include "mbed.h" through the final }. The starter has exactly one compile-blocking placeholder. Replace TODO 1 before saving or compiling; TODO_WAIT_VALUE is not valid C++.

#include "mbed.h"

DigitalOut status_led(PB_5, 0);

int main() {
    // 1: replace the placeholder with 500ms for build 1.
    const auto wait_time = TODO_WAIT_VALUE;

    while (true) {
        status_led = !status_led.read();
        ThisThread::sleep_for(wait_time);
    }
}

Before changing the wait time, predict what will happen. Use your answer about state-change timing to decide whether 1000ms should make the LED change faster, slower, or about the same.

Before testing, what should changing wait_time from 500ms to 1000ms do?

How to find the changing LED: after upload, compare the live camera view before and after the program starts. Ignore LEDs that stay steadily on or steadily off, and ignore brief upload/status flashes. Use the LED that keeps changing at the timing you programmed; when you switch from 500ms to 1000ms, the same LED should keep changing at the new timing.

The lab card has a Mark practice as done button. Do not click it yet; use it only after you complete all numbered steps, including the 1000ms retest.

  1. Open the STM32 Mbed CodeIDE lab from this activity's Open lab button.

  2. In CodeIDE, open main.cpp.

  3. Replace the file with the program above and replace TODO_WAIT_VALUE with 500ms.

  4. Search main.cpp for TODO. When the search has no matches, save. Compile only after saving.

  5. Compile build 1. If there is an error, check the TODO replacement, spelling, braces {}, parentheses (), and semicolons.

  6. Before clicking Upload, look at the live camera once and choose the board area you will watch.

  7. Upload the program to the board. Wait for the console/status area to show that upload/programming finished.

  8. Watch the same camera area and look for the LED that repeatedly changes at the timing you programmed. Ignore any always-on power/status LEDs and brief upload/status flashes. Use a stopwatch or phone timer: start timing when the repeating LED changes state, count five more changes, and stop at the sixth visible change. Off-to-on and on-to-off each count as one change.

  9. Change wait_time from 500ms to 1000ms, save main.cpp, compile build 2, upload, and observe the same LED area again.

  10. Only after step 9 is complete, click Mark practice as done.

Record the two observations in the boxes below. The reason for timing five intervals is practical: one LED change is short and hard to time accurately from a live camera, but five changes give a more stable estimate.

Use this method for each version:

1. Start the timer when the LED changes state. This starting change is the beginning of interval 1.
2. Count the next visible changes aloud or on paper: 1, 2, 3, 4, 5.
3. Stop the timer on the change you counted as 5. Your timed duration now covers five state-change intervals.
4. Divide the timed duration by 5 to estimate one state-change interval.
5. A full blink cycle takes two state changes, so multiply your state-change interval by 2.

Example: if five intervals take about 2.6 s, one state-change interval is about 0.52 s, and a full blink cycle is about 1.04 s.

For 500ms, write your observation using this format:

- I counted 5 state-change intervals from the repeating LED.
- Time for 5 intervals (s):
- Time for 1 state-change interval, approximately (s):
- Time for 1 full blink cycle, approximately (s):
- Did this match my prediction? yes/no, because...

For 1000ms, write your observation using this format:

- I counted 5 state-change intervals from the same repeating LED.
- Time for 5 intervals (s):
- Time for 1 state-change interval, approximately (s):
- Compared with 500ms, the LED changed faster/slower/about the same:
- Did this match my prediction? yes/no, because...
- Troubleshooting needed? yes/no. If yes, what changed?

Write a real-board evidence note. Include four details: whether compile/upload finished, where the changing LED appeared in the live camera view, what changed in the camera, and how the 1000 ms version looked different from the 500 ms version. Example: "Compile finished, upload/program finished, and the LED near the left side of the board area changed state more slowly after I changed 500ms to 1000ms."

3

Understand the Mbed structure

8 min

Most programs in this course follow the same Mbed OS structure: include mbed.h, create hardware objects such as DigitalOut, start in int main(), and put repeated controller behavior inside while (true).

In your own words, what does while (true) do in this blink program? Why is it needed?

4

Submit your code

8 min

Before submitting, leave the final 1000ms version saved in main.cpp. Search once more for TODO; the submitted file must contain no TODO placeholders.

Attach your saved final main.cpp

Click Check saved files, confirm that main.cpp contains the final 1000ms blink program, then click Attach saved code. After the code is attached, click Submit submission at the bottom of the activity. The required attachment for this lesson is the saved main.cpp code snapshot.