Teach Remote lab lessons

Teach lesson

STM32 Mbed CodeIDE (8/8): integrated controller capstone

Students combine analog input, button acknowledgement, RGB/status LED output, and serial evidence to build a small integrated STM32 controller.

  • STM32 Nucleo (Mbed)
  • 65 min
  • Upper secondary / introductory vocational electronics
  • English
  • Embedded systems
STM32 Nucleo (Mbed)
STM32 Nucleo (Mbed)

Learning Outcomes

  • Combine analog input, button input, RGB PWM output, a status LED, and serial evidence in one controller.

  • Use a test matrix to prove controller behavior one state at a time.

  • Explain how code, serial output, and visible lab evidence support a controller claim.

Student activity preview

Activity Content

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

1

Controller requirements

12 min

The controller uses one potentiometer as a measured level and one button as an acknowledge control.

Hardware map used by the starter controller:

- PC_1 is the analog level input, controlled by the Pot. pot2 slider.
- PC_5 is the acknowledge button input, controlled by the B1 button.
- PA_8, PA_9, and PA_10 are the RGB PWM channels.
- PB_13 is the active-low green status LED.

Read this map as a set of Mbed objects working together: AnalogIn reads the level, DigitalIn reads the acknowledge button, PwmOut drives the RGB channels, and DigitalOut drives the status LED. The capstone is mainly about connecting those API pieces into one readable controller.

Capstone controller map

Diagram mapping Pot. pot2 to PC1, B1 to PC5, RGB LED channels to PA8 PA9 PA10, and the green status LED to PB13.

This capstone only uses features you practiced in the previous lessons.

The required behavior is:

- Level below 40%: RGB output green, status LED off, serial evidence like
percent=25 level=SAFE ack=0.
- Level from 40% to 69%: RGB output yellow, status LED off, serial evidence like
percent=55 level=WARNING ack=0.
- Level 70% or higher with the button released: RGB output red, status LED
blinking, serial evidence like percent=80 level=CRITICAL ack=0. Watch
for at least two seconds before recording this state; count it as blinking
only if the status LED turns on and off repeatedly.
- Level 70% or higher with the button pressed: RGB output blue, status LED off,
serial evidence like percent=80 level=CRITICAL ack=1.

The acknowledge button does not make the critical level safe. It only changes the visible warning so the operator can show that the alarm has been noticed.

If the potentiometer reads about 80% and the acknowledge button is pressed, what should the RGB output, status LED, and serial line show?

2

Run the starter controller

30 min

  1. Open the STM32 Mbed CodeIDE lab.

  2. Open main.cpp.

  3. Replace the file with the controller below.

  4. Save, compile, and upload.

  5. Use Pot. pot2 to set safe, warning, and critical levels.

  6. In the critical level, test the button released and then pressed.

  7. Record one row per state in the test table, including the full serial line.

  8. For the unacknowledged critical state, observe the status LED for at least two seconds before writing the visible response.

#include "mbed.h"

AnalogIn level_control(PC_1);
DigitalIn acknowledge_button(PC_5, PullUp);

PwmOut rgb_red(PA_8);
PwmOut rgb_green(PA_9);
PwmOut rgb_blue(PA_10);

DigitalOut status_led(PB_13, 1);

void status_on() {
    status_led = 0;
}

void status_off() {
    status_led = 1;
}

void set_rgb(float red, float green, float blue) {
    rgb_red.write(red);
    rgb_green.write(green);
    rgb_blue.write(blue);
}

void show_safe() {
    set_rgb(0.0f, 0.7f, 0.0f);
    status_off();
}

void show_warning() {
    set_rgb(0.7f, 0.5f, 0.0f);
    status_off();
}

void show_critical(bool acknowledged) {
    if (acknowledged) {
        set_rgb(0.0f, 0.0f, 0.8f);
        status_off();
    } else {
        set_rgb(0.9f, 0.0f, 0.0f);
        status_led = !status_led.read();
    }
}

int main() {
    rgb_red.period_ms(2);
    rgb_green.period_ms(2);
    rgb_blue.period_ms(2);

    printf("Integrated controller start\n");

    while (true) {
        float reading = level_control.read();
        int percent = (int)(reading * 100.0f);
        bool acknowledged = (acknowledge_button.read() == 0);

        if (percent < 40) {
            show_safe();
            printf("percent=%d level=SAFE ack=%d\n",
                   percent,
                   acknowledged);
        } else if (percent < 70) {
            show_warning();
            printf("percent=%d level=WARNING ack=%d\n",
                   percent,
                   acknowledged);
        } else {
            show_critical(acknowledged);
            printf("percent=%d level=CRITICAL ack=%d\n",
                   percent,
                   acknowledged);
        }

        ThisThread::sleep_for(500ms);
    }
}

Fill the table while you test. Use one row for each required state: safe, warning, critical with the button released, and critical with the button pressed. In Inputs used, write both the approximate potentiometer percent and the button state. In Serial evidence, copy the full printed line, such as percent=80 level=CRITICAL ack=1.
For the critical-released row, write blinking only after watching the status LED turn on and off repeatedly for at least two seconds.

Four-state controller test table

Test case Inputs used Expected response Observed visible response Serial evidence

Choose your strongest test row. Quote the input state and serial line from that row, then explain how the RGB output and status LED matched the requirement.

3

Improve the controller

15 min

The controller above is a starting point. Improve one part and test it again.

Choose one improvement:

- Change the warning or critical threshold and justify the new value.
- Add a slower or faster blink for the critical status LED.
- Add a serial line that prints mode=ACKNOWLEDGED when the button is pressed
in the critical state.
- Add a second severe-critical pattern for values above 90%.

Describe the improvement you made and how you tested it. Which requirement does it improve? If you could not implement the improvement, describe the blocker and the exact test you would run next.

4

Submit your code

8 min

Attach your saved final main.cpp

Click Check saved files, confirm that main.cpp contains the final controller you tested, then click Attach saved code. After the code is attached, click Submit submission at the bottom of the activity.