Teach Remote lab lessons

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.

  • STM32 Nucleo (Arduino)
  • 55 min
  • Grade 10-12 / introductory vocational electronics
  • English
  • Embedded systems
Signal path from potentiometer pot1 on PC0, through analogRead values from 0 to 1023 and map values from 0 to 255, to RGB PWM outputs on PA8, PA9, and PA10.
STM32 Nucleo (Arduino)

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.

1

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.

Follow the value from input to output

Signal path from potentiometer pot1 on PC0, through analogRead values from 0 to 1023 and map values from 0 to 255, to RGB PWM outputs on PA8, PA9, and PA10.

The program will use this conversion:

int reading = analogRead(POT_1);
int brightness = map(reading, 0, 1023, 0, 255);

With the direct conversion shown, what should a potentiometer reading near the
maximum produce?

2

Control brightness with an analog reading

29 min

Serial.begin(115200) sets the communication rate to 115200 baud. After an
upload, open CodeIDE's Serial console and select 115200 if it asks for a rate.
Each line should show one ADC reading (0-1023 counts) and one PWM brightness
(0-255). map(value, inLow, inHigh, outLow, outHigh) places a value at the
corresponding point between the two output endpoints.

  1. Launch the STM32 Arduino lab and open the main.ino editor tab in CodeIDE.

  2. Paste the starter program. For TODO 1, select one declared RGB pin. For TODO

  3. 2 and 3, complete a direct PWM map so the minimum reading is dark and the

  4. maximum reading is fully bright.

  5. Search for TODO; replace all three before compiling. Save, compile, wait

  6. for success, then upload and wait for success.

  7. Open Serial at 115200 baud. If no lines appear after a successful upload,

  8. confirm the console rate and reopen the console before changing code.

  9. Initial test: move potentiometer 1 to low, middle, and high positions. At

  10. each position, wait for 3-5 Serial lines to settle, copy one approximate pair

  11. of values, and observe the selected RGB channel.

  12. Complete all three table rows and the explanation immediately below the table

  13. before modifying the map.

#include <Arduino.h>

const int POT_1 = PC0;
const int RGB_RED = PA8;
const int RGB_GREEN = PA9;
const int RGB_BLUE = PA10;
const int RGB_OUTPUT = TODO_RGB_OUTPUT;  // TODO 1: one declared RGB pin to control

void setup() {
  pinMode(RGB_OUTPUT, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int reading = analogRead(POT_1);
  int brightness = map(reading, 0, 1023,
                       TODO_PWM_AT_MIN, TODO_PWM_AT_MAX);  // TODO 2-3: direct-map endpoints

  analogWrite(RGB_OUTPUT, 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, name the RGB channel and describe its brightness.

Potentiometer position Serial reading (ADC counts) Calculated brightness (PWM 0-255) Observed output Does it make sense?

Use at least two rows to explain the relationship between the potentiometer reading and the observed brightness.

3

Invert only after recording the initial test

8 min

After the three initial rows are complete, swap the two PWM endpoints in
map so the conversion is inverted. Do not change RGB_OUTPUT. Save, compile,
upload, reopen Serial at 115200 baud, and retest only the low and high
positions. This creates a clear comparison without mixing a channel change with
a conversion change.

Answer in three lines using this format:

1. modified map: ...
2. low position: pot1=... brightness=... | observed LED: ...
3. high position: pot1=... brightness=... | observed LED: ...

Then add one sentence explaining how the two observations show that the
conversion was inverted.

4

Submit your code

10 min

Submit your final main.ino

Save main.ino before attaching it. The snapshot must show the tested inverted
map, the selected RGB output, Serial reporting, and no TODO token.

Explain why a value between 0 and 1023 cannot be used directly as PWM brightness if the expected brightness range is 0 to 255.