Teach lesson
STM32 Mbed CodeIDE (5/8): analog voltage warning
Students read a potentiometer with Mbed AnalogIn, convert the value to a voltage warning, and verify threshold behavior on the real board.
First time activating Teach? You can run one class session at no cost. Students join with a class code.
New to LabsLand? Create your teacher account
Learning Outcomes
Read an analog input with Mbed
AnalogIn.Convert a normalized reading into approximate millivolts.
Build a threshold warning using LEDs and serial evidence.
Student activity preview
Activity Content
Preview only. In a class session, students can fill in responses and submit their work to the teacher.
From analog value to decision
9 min
Many real controllers watch values that change gradually, such as a battery voltage, sensor level, or adjustment knob, and then decide when the value has crossed a warning threshold. A potentiometer gives a safe changing analog value for practicing that pattern.
This lesson uses PC_1, one of the analog input pins in the STM32 Mbed CodeIDE guide. In the remote lab UI, this is the Pot. pot2 slider. Move that slider slowly and watch the console value change.
Pin checklist for this lesson:
- Analog input: Pot. pot2 -> PC_1
- Green LED 1: PB_13
- Green LED 2: PB_14
- Green LED 3: PB_15
How one analog reading becomes a warning
Mbed code:
AnalogIn pot(PC_1);
float reading = pot.read();
int millivolts = (int)(reading * 3300.0f);
AnalogIn pot(PC_1) creates a Mbed input object for a measured voltage, similar to how DigitalOut created output objects for LEDs. pot.read() takes one sample and returns a normalized value from about 0.0 to 1.0. Multiplying by 3300.0f converts that fraction into approximate millivolts for a 3.3 V input range. The program prints integer millivolts, not floating-point text.
Approximate thresholds:
- below 1000 mV: low
- 1000-1999 mV: medium
- 2000-2999 mV: high
- 3000 mV and above: simulated overvoltage/full-scale warning, shown by blinking all three green LEDs
The low state still turns one green LED on. In this lesson, that means "LOW warning level", not "no output."
Expected warning behavior:
- < 1000 mV: printed level LOW; one green LED on.
- 1000-1999 mV: printed level MEDIUM; two green LEDs on.
- 2000-2999 mV: printed level HIGH; three green LEDs on.
- >= 3000 mV: printed level OVERVOLTAGE; all three green LEDs blink on and off together.
The helper function set_led() writes 0 for on and 1 for off because these green LED outputs are active-low in the lab examples.
If the serial console prints millivolts=2450, which threshold range is it in and which warning level should the program show?
Run the warning monitor
30 min
Open the STM32 Mbed CodeIDE lab.
Open
main.cpp.Replace the file with the program below.
Complete TODO 1, TODO 2, and TODO 3 with the three boundary values stated above. Do not change any other starter line yet.
Save, compile, and upload. This is planned build 1 of 2.
Move the
Pot. pot2slider slowly through low, middle, and high positions.Record at least four serial readings and LED states.
If a threshold is hard to reach, record the closest value and explain it.
#include "mbed.h"
AnalogIn pot(PC_1);
DigitalOut led1(PB_13, 1);
DigitalOut led2(PB_14, 1);
DigitalOut led3(PB_15, 1);
// TODO 1: replace 0 with the LOW/MEDIUM boundary in mV.
constexpr int low_medium_boundary_mv = 0;
// TODO 2: replace 0 with the MEDIUM/HIGH boundary in mV.
constexpr int medium_high_boundary_mv = 0;
// TODO 3: replace 0 with the HIGH/OVERVOLTAGE boundary in mV.
constexpr int overvoltage_boundary_mv = 0;
void set_led(DigitalOut &led, bool on) {
led = on ? 0 : 1;
}
void set_leds(int count) {
set_led(led1, count >= 1);
set_led(led2, count >= 2);
set_led(led3, count >= 3);
}
int main() {
printf("Analog voltage warning start\n");
bool overvoltage_flash = false;
while (true) {
float reading = pot.read();
int millivolts = (int)(reading * 3300.0f);
if (millivolts < low_medium_boundary_mv) {
overvoltage_flash = false;
set_leds(1);
printf("millivolts=%d level=LOW\n", millivolts);
} else if (millivolts < medium_high_boundary_mv) {
overvoltage_flash = false;
set_leds(2);
printf("millivolts=%d level=MEDIUM\n", millivolts);
} else if (millivolts < overvoltage_boundary_mv) {
overvoltage_flash = false;
set_leds(3);
printf("millivolts=%d level=HIGH\n", millivolts);
} else {
overvoltage_flash = !overvoltage_flash;
set_leds(overvoltage_flash ? 3 : 0);
printf("millivolts=%d level=OVERVOLTAGE flash=%d\n",
millivolts,
overvoltage_flash);
}
ThisThread::sleep_for(1000ms);
}
}
The program above is one continuous main.cpp file. If your PDF view splits the code block across two pages, continue copying with the next line on the following page.
Fill at least four table rows while you move the potentiometer. Use different positions when possible: low, medium, high, and the highest value you can reach. Test case identifies the intended range; Serial millivolts is the integer console reading in mV; Printed level is the exact console label; and Observed LED state is what the camera showed. Choose the match judgment from the listed options. The table may show extra rows: leave every unused row completely blank.
Copy values from your own current console. A valid line begins with
millivolts= and includes the level selected by your completed threshold code.
For level=OVERVOLTAGE, observe for at least two seconds before filling the row. flash=1 means all three green LEDs are on for that sample; flash=0 means all three are off for that sample. Record the overall behavior as "all three blinking", not as a single still image.
Voltage warning evidence table
Complete four rows for different ranges. Leave extra unused rows blank.
| Test case | Serial millivolts mV | Printed level | Observed LED state | Matched threshold? |
|---|---|---|---|---|
Choose one row from your table. Quote the serial millivolt value, name the threshold range it belongs to, and explain whether the LED state matched that range.
Tune the warning behavior
13 min
Independently choose one of the three named boundary constants and change it by exactly +200 mV or -200 mV. Keep the boundaries in increasing order, so every warning range remains reachable. Do not copy a classmate's choice: select the boundary and direction that make sense for the warning behavior you want.
Before editing, state the constant name, its original value, whether you will add or subtract 200 mV, and the resulting value.
Edit only that constant, then save, compile, and upload. This is planned build 2 of 2. Test two positions around the new boundary: the closest reading below it and the closest reading at or above it. Copy both complete serial lines. A reading equal to the boundary belongs to the higher branch because the code uses <. If the slider jumps past the exact value, use the closest reading on each side and say so.
Paste the two near-boundary serial lines. Label one below and the other at or above, then state the printed branch for each.
Submit your code
8 min
Attach your saved final main.cpp
Click Check saved files, confirm that main.cpp contains your tuned threshold version, then click Attach saved code. After the code is attached, click Submit submission at the bottom of the activity.
Turn this preview into a live class session
Activate LabsLand Teach to use this lesson with your students. The first time you activate Teach, you can run one class session at no cost.
New to LabsLand? Create your teacher account