Teach Remote lab lessons

Teach lesson

STM32 Mbed CodeIDE (7/8): servo position control

Students control a servo with Mbed PWM pulse widths, test positions safely, and record approximate movement evidence from the remote STM32 setup.

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

Learning Outcomes

  • Control a servo output with Mbed PwmOut.

  • Map a potentiometer reading to an approximate angle.

  • Use serial evidence to support an observation that may be hard to judge visually.

Student activity preview

Activity Content

Preview only. In a class session, students can fill in responses and submit their work to the teacher.

1

Servo pulse width basics

10 min

PWM for an LED changes average brightness. A servo also uses repeated pulses, but the important value is the pulse width:

- about 500 microseconds: one end of travel
- about 1500 microseconds: middle
- about 2500 microseconds: other end of travel

In Mbed, the servo signal is produced with PwmOut. servo.period_ms(20) sets the usual 20 ms repetition period, and servo.pulsewidth_us(...) changes the high-pulse width that the servo interprets as position.

The exact mechanical angle can vary. Your job is to show a controlled relationship: low potentiometer value gives one position, high value gives a different position, and the serial output matches your mapping.

The camera may show the servo only as a small arm on the training board. Treat angle as an estimate calculated by your code. The strongest evidence is the combination of serial pulse_us, serial angle estimate, and a visible change in broad servo position.

Pin and evidence checklist for this lesson:

- Servo signal output: PA_15
- Potentiometer used later: Pot. pot2 -> PC_1
- Required serial evidence: pulse_us
- Camera evidence: broad servo position change, not a precise measured angle

Servo, Pot. pot2, and serial evidence

Labeled STM32WB lab screenshot showing the servo arm controlled from PA15, Pot. pot2 as the PC1 analog input, and console lines with percent, angle, and pulse_us evidence.

Use the camera for broad position evidence and the console for exact pulse-width evidence. Do not claim precise mechanical angles from the camera view alone.

If your code maps a low potentiometer value to 500 microseconds and a high value to 2500 microseconds, what should happen as you turn the potentiometer from low to high?

2

Run a three-position test

20 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. Watch the camera and console together.

  6. Watch for three broad servo positions.

  7. Record the serial pulse widths and what you observed.

#include "mbed.h"

PwmOut servo(PA_15);

void set_servo_us(int pulse_us) {
    servo.pulsewidth_us(pulse_us);
}

int main() {
    servo.period_ms(20);
    printf("Servo three-position test start\n");

    while (true) {
        set_servo_us(500);
        printf("pulse_us=500 position=low\n");
        ThisThread::sleep_for(2000ms);

        set_servo_us(1500);
        printf("pulse_us=1500 position=middle\n");
        ThisThread::sleep_for(2000ms);

        set_servo_us(2500);
        printf("pulse_us=2500 position=high\n");
        ThisThread::sleep_for(2000ms);
    }
}

Fill three table rows, one for each fixed pulse width: 500 us, 1500 us, and 2500 us. Copy the printed pulse width and printed position. In the Confidence column, write clear, partial, or unclear based on whether the camera showed a visible position change. If motion is unclear, write unclear and describe what you could see instead of inventing a precise angle.

Fixed servo pulse-width table

Pulse width Printed position Observed motion or position Confidence
3

Map potentiometer to servo angle

22 min

Now map AnalogIn PC_1 to an approximate angle and pulse width. Use the Pot. pot2 slider for PC_1.

  1. Replace main.cpp with the program below.

  2. Save, compile, and upload.

  3. Move the Pot. pot2 slider to low, middle, and high positions.

  4. Record the printed percent, angle, pulse width, and observed motion.

#include "mbed.h"

AnalogIn pot(PC_1);
PwmOut servo(PA_15);

int main() {
    servo.period_ms(20);
    printf("Potentiometer servo control start\n");

    while (true) {
        float reading = pot.read();
        int percent = (int)(reading * 100.0f);
        int angle = (int)(reading * 180.0f);
        int pulse_us = 500 + (int)(reading * 2000.0f);

        servo.pulsewidth_us(pulse_us);

        printf("percent=%d angle=%d pulse_us=%d\n",
               percent,
               angle,
               pulse_us);
        ThisThread::sleep_for(500ms);
    }
}

Fill three table rows for low, middle, and high potentiometer positions. Copy the printed percent, angle estimate, and pulse width before judging the motion. The serial pulse_us value is required evidence; the camera observation is only a broad position check.

Potentiometer-to-servo evidence table

Potentiometer position Serial percent Serial angle estimate Serial pulse_us Observed servo position

Use your low, middle, and high rows to explain whether the potentiometer controlled the servo as expected. Include the serial pulse_us trend and the broad camera observation.

4

Submit your code

8 min

Attach your saved final main.cpp

Click Check saved files, confirm that main.cpp contains the potentiometer-controlled servo version, then click Attach saved code. After the code is attached, click Submit submission at the bottom of the activity.