Teach lesson
STM32 with Arduino API (3/4): potentiometer and RGB
Students read a potentiometer with analogRead(), map the value to RGB output, and test real-board color and threshold behavior.
Learning Outcomes
Read an analog input and convert it to a useful range.
Use PWM to control the brightness of an RGB output.
Use the serial console as debugging evidence.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
Two different ranges
8 min
analogRead(PC0) returns a reading that we will treat as a value between 0
and 1023. analogWrite(PA10, brightness) expects brightness between 0 and
255. That is why we use map: it converts one range into another.
The program will use this conversion:
int reading = analogRead(POT_1);
int brightness = map(reading, 0, 1023, 0, 255);According to that fragment, if the potentiometer is close to the maximum, do you expect low, medium, or high brightness?
Control brightness with an analog reading
29 min
Open the STM32 Arduino lab and
main.ino.Copy the reference program.
Compile and upload.
Move potentiometer 1 to three positions: low, middle, and high.
Observe the blue channel brightness and the serial console.
Change the output channel from
RGB_BLUEtoRGB_GREEN, or invert the map.Compile, upload, and check that the change is visible.
#include <Arduino.h>
const int POT_1 = PC0;
const int RGB_RED = PA8;
const int RGB_GREEN = PA9;
const int RGB_BLUE = PA10;
void setup() {
pinMode(RGB_BLUE, OUTPUT);
Serial.begin(115200);
}
void loop() {
int reading = analogRead(POT_1);
int brightness = map(reading, 0, 1023, 0, 255);
analogWrite(RGB_BLUE, brightness);
Serial.print("pot1=");
Serial.print(reading);
Serial.print(" brightness=");
Serial.println(brightness);
delay(200);
}
Fill in the table after moving the potentiometer and checking Serial. Use only
three rows and leave the others blank if Teach shows more:
- Row 1: potentiometer in a low position.
- Row 2: potentiometer in a middle position.
- Row 3: potentiometer in a high position.
- In Serial reading and Calculated brightness, copy approximate values;
they do not need to be exact.
- In Observed output, describe the brightness or color you saw on the board.
| Potentiometer position | Serial reading | Calculated brightness | Observed output | Does it make sense? |
|---|---|---|---|---|
Use at least two rows to explain the relationship between the potentiometer reading and the observed brightness.
Change the conversion
8 min
Choose one of these modifications:
- Change the output to RGB_GREEN.
- Invert the map: map(reading, 0, 1023, 255, 0).
- Turn on two channels at once with different brightness values.
Which modification did you choose? Copy the main line you changed and describe what evidence shows that it worked.
Submit your code
10 min
Submit your final main.ino
Save main.ino before attaching it. The snapshot must show the final version that reads the potentiometer, calculates brightness, and writes values through Serial.
Explain why a value between 0 and 1023 cannot be used directly as PWM brightness if the expected brightness range is 0 to 255.