Teach Remote lab lessons

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.

  • STM32 Nucleo (Mbed)
  • 60 min
  • Upper secondary / introductory vocational electronics
  • English
  • Embedded systems
STM32 Nucleo (Mbed)
STM32 Nucleo (Mbed)

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.

1

From analog value to decision

9 min

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.

Analog warning pin and LED map

Lesson 5 map showing Pot. pot2 connected to PC1 and the green warning LEDs led1 PB13, led2 PB14, and led3 PB15.

Use this map for this lesson only: move the Pot. pot2 slider for PC_1, and watch the three green LEDs controlled by PB_13, PB_14, and PB_15.

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

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?

2

Run the warning monitor

30 min

  1. Open the STM32 Mbed CodeIDE lab.

  2. Open main.cpp.

  3. Replace the file with the program below.

  4. Save, compile, and upload.

  5. Move the Pot. pot2 slider slowly through low, middle, and high positions.

  6. Record at least four serial readings and LED states.

  7. 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);

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 < 1000) {
            overvoltage_flash = false;
            set_leds(1);
            printf("millivolts=%d level=LOW\n", millivolts);
        } else if (millivolts < 2000) {
            overvoltage_flash = false;
            set_leds(2);
            printf("millivolts=%d level=MEDIUM\n", millivolts);
        } else if (millivolts < 3000) {
            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. In the Reading column, write a short label such as low position, middle position, high position, or highest value. For each row, copy the printed millivolt value and level from the console, then write the LED state you saw in the camera.

Example serial lines:

millivolts=742 level=LOW
millivolts=1840 level=MEDIUM
millivolts=2510 level=HIGH
millivolts=3120 level=OVERVOLTAGE flash=1

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.

Example table row: middle position | 1840 | MEDIUM | two green LEDs on | yes.

Voltage warning evidence table

Reading Serial millivolts 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.

3

Tune the warning behavior

13 min

Change one threshold by 200 mV. For example, change the medium/high boundary from 2000 mV to 2200 mV by editing this line:

} else if (millivolts < 2000) {

to:

} else if (millivolts < 2200) {

This moves the MEDIUM/HIGH boundary upward. Values below 2200 mV should now print MEDIUM; values at 2200 mV or above should print HIGH until the overvoltage threshold.

Compile and upload again. Test at least two potentiometer positions near the changed boundary: one just below the new boundary and one just above it. Copy both serial lines as evidence. If you cannot get very close to the boundary, record the closest value below and the closest value above, then explain that limitation.

Which threshold did you change? Paste the two near-boundary serial readings you used, say which side of the new boundary each reading was on, and explain what difference the change made in the observed behavior.

4

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.