STM32 Blue Pill Servo Motor Sweep: Wiring, Code and Simulator
To build this STM32 Blue Pill Servo Motor Sweep project you need an STM32 Blue Pill (STM32F103C8) and a servo motor. The Servo library on PA8 times its 544–2400 µs pulses with a hardware timer’s interrupts, every 20 ms. Make the 3 connections in the wiring table, upload the Arduino C++ code, and watch the horn step through 0°, 90° and 180°. Try 45° steps.
Open this circuit in the free STM32 Blue Pill (STM32F103C8) simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | STM32 Blue Pill (STM32F103C8) | the microcontroller board |
| 1 | servo motor | PWM to a pin, V+ to 5V, GND to GND. The Servo library turns write(angle) into a pulse width. |
Wiring
| # | From | To |
|---|---|---|
| 1 | STM32 Blue Pill (STM32F103C8) PA8 | servo motor PWM |
| 2 | STM32 Blue Pill (STM32F103C8) 5V | servo motor V+ |
| 3 | STM32 Blue Pill (STM32F103C8) GND | servo motor GND |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the STM32 Blue Pill (STM32F103C8). No install or account is needed.
- Add the parts. Add a servo motor from the parts palette.
- Wire 1. Connect STM32 Blue Pill (STM32F103C8) PA8 to servo motor PWM.
- Wire 2. Connect STM32 Blue Pill (STM32F103C8) 5V to servo motor V+.
- Wire 3. Connect STM32 Blue Pill (STM32F103C8) GND to servo motor GND.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Watch the horn step through 0°, 90° and 180°. Try 45° steps.
Arduino C++ code
// Sweep a servo on PA8 between 0 and 180 degrees with the Servo library,
// which times its pulses with a hardware timer's interrupts.
#include <Servo.h>
Servo arm;
void setup() {
Serial.begin(115200);
arm.attach(PA8);
}
void loop() {
for (int a = 0; a <= 180; a += 90) {
arm.write(a);
Serial.print("Angle: ");
Serial.println(a);
delay(800);
}
}
What to try
Watch the horn step through 0°, 90° and 180°. Try 45° steps.
Frequently asked questions
How do I wire the Servo Motor Sweep project on an STM32 Blue Pill (STM32F103C8)?
Make 3 connections: STM32 Blue Pill (STM32F103C8) PA8 to servo motor PWM; STM32 Blue Pill (STM32F103C8) 5V to servo motor V+; STM32 Blue Pill (STM32F103C8) GND to servo motor GND.
What parts do I need for this STM32 Blue Pill project?
An STM32 Blue Pill (STM32F103C8) and a servo motor.
Can I simulate this STM32 Blue Pill project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is built with the STM32duino core and runs on an STM32F103 emulated instruction by instruction: its ARM Cortex-M3 core, NVIC and peripherals (GPIO, EXTI, USART, timers, ADC and I²C).
What does the code do?
The Servo library on PA8 times its 544–2400 µs pulses with a hardware timer’s interrupts, every 20 ms.
More STM32 Blue Pill (STM32F103C8) projects
- STM32 Blue Pill HC-05 Bluetooth on USART2: Wiring, Code and Simulator
- STM32 Blue Pill LED Blink and Serial: Wiring, Code and Simulator
- STM32 Blue Pill Button Interrupt (EXTI): Wiring, Code and Simulator
- STM32 Blue Pill ADC Potentiometer and PWM LED: Wiring, Code and Simulator
- STM32 Blue Pill I2C BMP280 with Wire: Wiring, Code and Simulator
- STM32 Blue Pill Serial UART Echo: Wiring, Code and Simulator