Projects / Servo sweep with an STM32 timer

Servo sweep with an STM32 timer

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 8 done
  1. STM32ST

    Wire the programmer

    SWDIO, SWCLK, GND and 3.3V to the Blue Pill.

  2. STM32

    Set both boot jumpers

    To 0 so the board runs from flash.

  3. STM32

    Wire the knob

    Outer legs to 3.3 and GND, wiper to PA0.

  4. STM3259°

    Wire the servo

    Signal to PA1, power from the separate supply, grounds joined.

  5. STM32

    Install the STM32 core

    Choose the Blue Pill settings.

  6. STM3259°

    Upload the sketch

    The servo goes to the middle.

  7. STM3259°

    Turn the knob

    The servo follows.

  8. STM3259°

    Change the pulse limits

    Find the safe ends for your servo.

In short

How do you build a servo sweep with an STM32 timer?

A Blue Pill turns a knob position into a servo angle using a hardware timer to make the 50 Hz pulses, with the servo on its own power. You need STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), SG90 Micro Servo, Potentiometer 10 kΩ and Breadboard 830 Tie Points. It takes 8 steps and about 140 minutes.

Level
Intermediate
Time
About 140 min
Parts
5

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
ST-Link GNDBlue Pill GNDGround
ST-Link SWDIOBlue Pill DIODebug data
ST-Link SWCLKBlue Pill DCLKDebug clock
ST-Link 3.3VBlue Pill 3.3 pinPower from the programmer (do not also plug in USB)
Pot outer legsBlue Pill 3.3 and GNDVoltage divider
Pot wiperBlue Pill PA0Analog input
Servo signal (orange or yellow)Blue Pill PA1Pulse output
Servo power (red)Separate 5 V supply, not the boardA servo can pull hundreds of milliamps
Servo ground (brown or black)Supply ground and Blue Pill GNDCommon ground

Code

A hobby servo expects a pulse every 20 ms, which is 50 Hz, and the width of that pulse sets the angle. About 1 ms points one way, 1.5 ms is the middle and 2 ms points the other way. On an Uno the Servo library makes these pulses with a timer and interrupts. The STM32 does the same job with one of its hardware timers, which count clock ticks on their own while the processor does other work. The core's Servo library sets up a timer so each pulse ends at exactly the right tick, and that is why the arm does not jitter when your code is busy. Here the knob is read with the 12-bit ADC and mapped to a pulse between 1 ms and 2 ms. The board pins run at 3.3 V, which most small servos accept as a signal. The servo itself must have its own 5 V supply because a moving servo can draw far more current than the USB port or the programmer pin gives, and the grounds must be joined or the pulse has no reference. If the servo buzzes at the ends, narrow the pulse limits a little.

Runs on a virtual STM32 Blue Pill in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <Servo.h>

const int POT_PIN = PA0;
const int SERVO_PIN = PA1;

Servo arm;

void setup() {
  analogReadResolution(12);                    // 0 to 4095 on the STM32 ADC
  arm.attach(SERVO_PIN, 1000, 2000);           // pulse limits in microseconds
  arm.writeMicroseconds(1500);                 // start in the middle
  delay(500);
}

void loop() {
  int raw = analogRead(POT_PIN);
  int pulse = map(raw, 0, 4095, 1000, 2000);   // 1 ms to 2 ms
  arm.writeMicroseconds(pulse);
  delay(15);                                   // the pulse repeats every 20 ms
}

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Knob in the middleServo near the centre
Knob to one endServo moves to one limit
Knob to the other endServo moves to the other limit
Knob moved slowlySmooth movement with no jitter
Servo on its own supplyBoard does not reset when the servo moves

If it does not work

Servo twitches or the board resets

Power the servo from its own 5 V supply and join the grounds.

Servo does not move

Check PA1 and the signal wire colour.

Upload fails

Check the ST-Link wires and the boot jumpers.

Servo hums at the ends

Reduce the pulse range slightly.

Reads are noisy

Add a 100 nanofarad capacitor from PA0 to GND.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the servo sweep with an STM32 timer?

STM32 Blue Pill (STM32F103C8T6), ST-Link V2 programmer (clone), SG90 Micro Servo, Potentiometer 10 kΩ and Breadboard 830 Tie Points.

How long does the servo sweep with an STM32 timer take to build?

About 140 minutes for a intermediate maker, in 8 steps.

How do I know the servo sweep with an STM32 timer works?

Knob in the middle: Servo near the centre. Knob to one end: Servo moves to one limit. Knob to the other end: Servo moves to the other limit.

Servo twitches or the board resets: what should I check?

Power the servo from its own 5 V supply and join the grounds.

Servo does not move: what should I check?

Check PA1 and the signal wire colour.

Upload fails: what should I check?

Check the ST-Link wires and the boot jumpers.

Servo hums at the ends: what should I check?

Reduce the pulse range slightly.