STM32 Blue Pill Serial over USB (CDC): Wiring, Code and Simulator
To build this STM32 Blue Pill Serial over USB (CDC) project you need an STM32 Blue Pill (STM32F103C8) and nothing but the board itself. With the board’s Serial setting on USB, Serial runs over the Blue Pill’s own USB port (USB CDC): the lab plugs it into a virtual PC, which enumerates it as a serial port. Make the 0 connections in the wiring table, upload the Arduino C++ code, and type 1 or 0 in the serial monitor to switch the PC13 LED. Then set Serial back to USART1 in the Inspector: the same code prints on PA9/PA10 instead.
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 |
Wiring
No wiring: this project uses only the STM32 Blue Pill (STM32F103C8).
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 nothing but the board itself from the parts palette.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Type 1 or 0 in the serial monitor to switch the PC13 LED. Then set Serial back to USART1 in the Inspector: the same code prints on PA9/PA10 instead.
Arduino C++ code
// Serial over the Blue Pill's own USB port (USB CDC): the board shows up on the
// computer as a serial port, with no USB-serial adapter. Set the board's Serial
// option to USB. Type 1 or 0 in the serial monitor to switch the PC13 LED.
void setup() {
Serial.begin(115200); // over USB the baud rate is ignored
pinMode(PC13, OUTPUT);
digitalWrite(PC13, HIGH); // LED off
}
int n = 0;
void loop() {
while (Serial.available()) {
char c = Serial.read();
if (c == '1') { digitalWrite(PC13, LOW); Serial.println("LED on"); }
if (c == '0') { digitalWrite(PC13, HIGH); Serial.println("LED off"); }
}
Serial.print("Hello over USB ");
Serial.println(n++);
delay(500);
}
What to try
Type 1 or 0 in the serial monitor to switch the PC13 LED. Then set Serial back to USART1 in the Inspector: the same code prints on PA9/PA10 instead.
Frequently asked questions
How do I wire the Serial over USB (CDC) project on an STM32 Blue Pill (STM32F103C8)?
No extra wiring is needed: it uses only the STM32 Blue Pill (STM32F103C8) itself.
What parts do I need for this STM32 Blue Pill project?
An STM32 Blue Pill (STM32F103C8) and nothing but the board itself.
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?
With the board’s Serial setting on USB, Serial runs over the Blue Pill’s own USB port (USB CDC): the lab plugs it into a virtual PC, which enumerates it as a serial port.
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