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.
New to LabsLand? Create your teacher account
Learning Outcomes
Use Mbed
PwmOutto 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.
Duty cycle
8 min
Many devices need levels between off and on: dimmer lights, color indicators, motors, buzzers, and other outputs that should feel adjustable. In this lesson, PWM lets you control brightness and color intensity, then connect that visible output to a live analog control.
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.
From Pot. pot1 to the RGB PWM channels
The path uses only Pot. pot1 on PC_0 and the three RGB outputs. The diagram
shows the interfaces; your TODOs still determine the actual duty value and write.
Which should look brighter on the same LED channel: duty cycle 0.20 or duty cycle 0.80? Explain in one sentence.
Test fixed RGB levels
22 min
Open the STM32 Mbed CodeIDE lab.
Open
main.cpp.Replace the file with the program below.
Save, compile, and upload.
Watch the console and camera together.
Observe the three brightness/color states.
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 rows, one per printed RGB state. Printed percentages means the three duty-cycle percentages shown after r=, g=, and b=; Observed output is the camera color/brightness. The table may display extra rows: leave unused rows completely blank. If a color is subtle, choose partly or no instead of guessing.
Fixed RGB evidence table
Complete the three printed states. Leave extra unused rows blank.
| Printed state | Printed red % | Printed green % | Printed blue % | Observed output | Matched expectation? |
|---|---|---|---|---|---|
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.
Replace
main.cppwith the program below.Complete TODO 1 so
dutymaps the 0.0-1.0 analog reading to the same 0.0-1.0 PWM range.Complete TODO 2 so the mapped value is written to the red channel.
Save, compile, and upload. This is planned build 2 of 2.
Move the
Pot. pot1slider slowly.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 reading = pot.read();
// TODO 1: replace 0.0f with the analog-to-duty mapping.
float duty = 0.0f;
int percent = (int)(duty * 100.0f);
// TODO 2: replace 0.0f with the mapped duty variable.
red.write(0.0f);
printf("red_percent=%d\n", percent);
ThisThread::sleep_for(500ms);
}
}
Fill exactly the three required cases: low, middle, and high potentiometer positions. Serial percent is the integer after red_percent= and has unit %; Observed brightness is a broad camera judgment, not a numeric measurement; Visibility records whether the camera evidence was clear. Leave any extra unused rows completely blank.
Potentiometer PWM evidence table
Complete low, middle, and high rows after the TODO program runs. Leave extra unused rows blank.
| Potentiometer position | Serial percent % | Observed brightness | Camera visibility |
|---|---|---|---|
Compare your low, middle, and high rows. State the three printed percentages in order and whether the visible red brightness followed the same low-to-high trend.
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.