Teach lesson
STM32 with Arduino API (4/4): mini-controller
Students combine potentiometer input, decisions, RGB/LED output, and serial evidence to build a compact STM32 Arduino mini-controller.
Learning Outcomes
Design a mini-controller with input, decision, output, and manual mode.
Choose and justify a threshold using real readings.
Test two branches of a decision and communicate the result.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
Understand the challenge and design tests
10 min
Your mini-controller will have three states:
- Potentiometer below the threshold: green color.
- Potentiometer above the threshold: red color.
- Button pressed: blue color, even if the potentiometer says something else.
The challenge is not just copying the program. By the end, you should have a
tested mini-controller: it reads an input, decides a state, changes the RGB LED,
writes evidence through Serial, and keeps one small improvement that you can
defend.
Before programming, think as if you were testing a small product. A good test
should say what you will touch, what color you expect to see, and what Serial
data would confirm that the controller made the correct decision.
Write two tests that should prove the mini-controller works. In each test, include: the input you will use, the expected RGB output, and the Serial data that should confirm it. One test must use button 1 pressed, and another must change the potentiometer relative to the threshold.
Build and check the mini-controller
27 min
The setRgb function avoids repeating three lines every time you want to change
color. The initial threshold is 600, but you will need to check whether it
makes sense with your real readings.
You will copy a base program, test it, and adjust it. The final result should be
a small but complete controller: it reads POT_1, decides with THRESHOLD,
allows manual mode with BUTTON_1, changes the RGB LED, and writes through
Serial which state it chose.
Readable schematic detail for this challenge: POT_1 reaches PC0, B1
reaches PC5, and the RGB LED uses PA8, PA9, and PA10. You do not need
to modify the setup; use it to understand that the program connects existing
inputs, decisions, and outputs.
Full STM32 Nucleo WB55RG schematic
Download the complete schematic as a PDF if you need to zoom in on a connection
that is not shown in the crop.
Open the STM32 Arduino lab and
main.ino.Copy the reference program.
Compile and upload.
Test the potentiometer below and above the threshold.
Press button 1 and check whether manual mode overrides the potentiometer.
Adjust
THRESHOLDto a value that makes sense with the readings you see in Serial.Compile, upload again, and repeat your two main tests.
#include <Arduino.h>
const int POT_1 = PC0;
const int BUTTON_1 = PC5; // Active LOW
const int RGB_RED = PA8;
const int RGB_GREEN = PA9;
const int RGB_BLUE = PA10;
const int THRESHOLD = 600;
void setRgb(int red, int green, int blue) {
analogWrite(RGB_RED, red);
analogWrite(RGB_GREEN, green);
analogWrite(RGB_BLUE, blue);
}
void setup() {
pinMode(BUTTON_1, INPUT);
pinMode(RGB_RED, OUTPUT);
pinMode(RGB_GREEN, OUTPUT);
pinMode(RGB_BLUE, OUTPUT);
Serial.begin(115200);
}
void loop() {
int reading = analogRead(POT_1);
bool buttonPressed = digitalRead(BUTTON_1) == LOW;
const char* state;
if (buttonPressed) {
setRgb(0, 0, 255);
state = "manual";
} else if (reading >= THRESHOLD) {
setRgb(255, 0, 0);
state = "red";
} else {
setRgb(0, 180, 0);
state = "green";
}
Serial.print("state=");
Serial.print(state);
Serial.print(" pot1=");
Serial.print(reading);
Serial.print(" button=");
Serial.println(buttonPressed ? "pressed" : "released");
delay(100);
}
Fill in the table after testing the controller. Each row must connect an input
with a decision and a piece of evidence. Use at least three rows and leave the
others blank if Teach shows more:
- Row 1: potentiometer below the threshold.
- Row 2: potentiometer above the threshold.
- Row 3: button 1 pressed to check manual mode.
- In Serial values seen, copy state, pot1, and button approximately.
- If you modify THRESHOLD, write the final value you used.
| Test | Serial values seen | Threshold used | Expected output | Observed output | Verdict |
|---|---|---|---|---|---|
Would you keep the threshold 600 or change it? Justify the decision with at least one low reading and one high reading seen in Serial, and explain why your threshold separates the two cases well.
Improve one thing
10 min
Choose one small improvement:
- Change the threshold.
- Change the colors or brightness.
- Make manual mode blink in blue.
- Add a Serial line that explains the state (green, red, or manual).
What improvement did you apply to the mini-controller? Write what you changed in the code and what evidence shows that you did not break the two main tests: potentiometer relative to the threshold and button 1 in manual mode.
Present the controller
13 min
Submit your final main.ino
Save main.ino before attaching it. The snapshot must show the final mini-controller, with the threshold, Serial output, and the improvement you tested.
Present your mini-controller in 6-8 lines. It must be clear what problem it solves, what input it reads, what decision it makes, what observable output it produces, what tests you ran, what improvement you applied, and what limitation it would still have in a real situation.