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

QtyPartWhat it does
1STM32 Blue Pill (STM32F103C8)the microcontroller board
1servo motorPWM to a pin, V+ to 5V, GND to GND. The Servo library turns write(angle) into a pulse width.

Wiring

#FromTo
1STM32 Blue Pill (STM32F103C8) PA8servo motor PWM
2STM32 Blue Pill (STM32F103C8) 5Vservo motor V+
3STM32 Blue Pill (STM32F103C8) GNDservo motor GND

Step by step

  1. 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.
  2. Add the parts. Add a servo motor from the parts palette.
  3. Wire 1. Connect STM32 Blue Pill (STM32F103C8) PA8 to servo motor PWM.
  4. Wire 2. Connect STM32 Blue Pill (STM32F103C8) 5V to servo motor V+.
  5. Wire 3. Connect STM32 Blue Pill (STM32F103C8) GND to servo motor GND.
  6. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  7. 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