Servo sweep with an STM32 timer
Build it step by step
0 of 8 doneWire the programmer
SWDIO, SWCLK, GND and 3.3V to the Blue Pill.
Set both boot jumpers
To 0 so the board runs from flash.
Wire the knob
Outer legs to 3.3 and GND, wiper to PA0.
Wire the servo
Signal to PA1, power from the separate supply, grounds joined.
Install the STM32 core
Choose the Blue Pill settings.
Upload the sketch
The servo goes to the middle.
Turn the knob
The servo follows.
Change the pulse limits
Find the safe ends for your servo.
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
Wiring
| From | To | Why |
|---|---|---|
| ST-Link GND | Blue Pill GND | Ground |
| ST-Link SWDIO | Blue Pill DIO | Debug data |
| ST-Link SWCLK | Blue Pill DCLK | Debug clock |
| ST-Link 3.3V | Blue Pill 3.3 pin | Power from the programmer (do not also plug in USB) |
| Pot outer legs | Blue Pill 3.3 and GND | Voltage divider |
| Pot wiper | Blue Pill PA0 | Analog input |
| Servo signal (orange or yellow) | Blue Pill PA1 | Pulse output |
| Servo power (red) | Separate 5 V supply, not the board | A servo can pull hundreds of milliamps |
| Servo ground (brown or black) | Supply ground and Blue Pill GND | Common 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.
| Check | Expected |
|---|---|
| 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 |
| Knob moved slowly | Smooth movement with no jitter |
| Servo on its own supply | Board 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 →
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.
