Teach Remote lab lessons

Teach lesson

STM32 Mbed CodeIDE (6/8): PWM brightness and RGB control

Students use Mbed PwmOut to control LED brightness and RGB color levels, then record how duty cycle changes real-board output.

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

Learning Outcomes

  • Use Mbed PwmOut to control output brightness.

  • Relate duty cycle to visible brightness or color balance.

  • Use analog input to control PWM 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

Duty cycle

8 min

Mbed uses PwmOut for PWM pins:

PwmOut red(PA_8);
red.period_ms(2);
red.write(0.25f);

PwmOut red(PA_8) creates a Mbed PWM output object. period_ms(2) sets how often the pulse pattern repeats, and write(0.25f) sets how much of each repeat is on. For an RGB LED, using the same period on red, green, and blue makes the channels behave as one coordinated color output.

write() takes a duty cycle from 0.0 to 1.0:

- 0.0f: always off
- 0.25f: on for 25 percent of each cycle
- 1.0f: always on

This lesson uses the RGB PWM pins PA_8, PA_9, and PA_10.

It also uses PC_0 as the analog control pin in the second half of the lesson. In the lab UI, PC_0 is the Pot. pot1 slider. This is different from lesson 5, which used PC_1 / Pot. pot2.

PWM and potentiometer pin map

Schematic detail showing POT1 on PC0, POT2 on PC1, and RGB PWM outputs on PA8, PA9, and PA10.

The same schematic detail is useful here: POT1 maps to PC_0, and the RGB LED channels map to PA_8, PA_9, and PA_10.

Which should look brighter on the same LED channel: duty cycle 0.20 or duty cycle 0.80? Explain in one sentence.

2

Test fixed RGB levels

22 min

  1. Open the STM32 Mbed CodeIDE lab.

  2. Open main.cpp.

  3. Replace the file with the program below.

  4. Save, compile, and upload.

  5. Watch the console and camera together.

  6. Observe the three brightness/color states.

  7. Record what changed between low red, mixed color, and high blue.

#include "mbed.h"

PwmOut red(PA_8);
PwmOut green(PA_9);
PwmOut blue(PA_10);

void set_rgb(float r, float g, float b) {
    red.write(r);
    green.write(g);
    blue.write(b);
}

int main() {
    red.period_ms(2);
    green.period_ms(2);
    blue.period_ms(2);

    printf("PWM RGB fixed levels start\n");

    while (true) {
        set_rgb(0.20f, 0.00f, 0.00f);
        printf("state=low_red r=20 g=0 b=0\n");
        ThisThread::sleep_for(2000ms);

        set_rgb(0.20f, 0.50f, 0.10f);
        printf("state=mixed r=20 g=50 b=10\n");
        ThisThread::sleep_for(2000ms);

        set_rgb(0.00f, 0.00f, 0.80f);
        printf("state=high_blue r=0 g=0 b=80\n");
        ThisThread::sleep_for(2000ms);
    }
}

Fill three table rows, one for each printed RGB state. Copy the printed state label and percentages, then describe the visible color or brightness you observed. If the camera makes a color subtle, write that in Matched expectation? instead of guessing.

Fixed RGB evidence table

Printed state Printed percentages Observed output Matched expectation?
3

Use the potentiometer as a brightness control

22 min

Now combine analog input and PWM output. The potentiometer controls the red channel duty cycle. The serial percentage is your strongest evidence if camera exposure makes brightness subtle.

  1. Replace main.cpp with the program below.

  2. Save, compile, and upload.

  3. Move the Pot. pot1 slider slowly.

  4. Record low, middle, and high readings with the visible output.

#include "mbed.h"

AnalogIn pot(PC_0);
PwmOut red(PA_8);
PwmOut green(PA_9);
PwmOut blue(PA_10);

int main() {
    red.period_ms(2);
    green.period_ms(2);
    blue.period_ms(2);

    green.write(0.0f);
    blue.write(0.0f);

    printf("Potentiometer controlled PWM start\n");

    while (true) {
        float duty = pot.read();
        int percent = (int)(duty * 100.0f);

        red.write(duty);
        printf("red_percent=%d\n", percent);

        ThisThread::sleep_for(500ms);
    }
}

Fill three table rows for low, middle, and high potentiometer positions. Copy the printed percentage and describe how the red brightness changed. If the camera exposure makes the brightness hard to judge, still copy the percentage and write "camera unclear" in Note.

Potentiometer PWM evidence table

Potentiometer position Serial percent Observed brightness Note

Use one row from your table to explain how analog input controlled PWM output. Include the potentiometer position, printed percentage, and observed brightness.

4

Submit your code

8 min

Attach your saved final main.cpp

Click Check saved files, confirm that main.cpp contains the potentiometer-controlled PWM version, then click Attach saved code. After the code is attached, click Submit submission at the bottom of the activity.