STM32 USB CDC LED Control A simple STM32 project that demonstrates USB CDC (Communication Device Class) functionality for controlling an LED via serial commands. ๐ Features
USB Virtual COM Port: STM32 appears as a serial device on your computer LED Control: Turn LED ON/OFF using simple text commands Real-time Feedback: Get confirmation messages for each command Plug & Play: No additional drivers needed on most modern systems
๐ง Hardware Requirements
STM32F103 development board (or compatible) LED connected to GPIO pin (configurable) USB cable for connection to PC
๐ Software Requirements
STM32CubeIDE or compatible development environment STM32 HAL Library USB Device Library (CDC class) Serial terminal software (PuTTY, Tera Term, Arduino IDE Serial Monitor, etc.)
๐ ๏ธ Setup & Configuration
- Hardware Connections
Connect LED to the designated GPIO pin (check LED_GPIO_Port and LED_Pin in code) Connect STM32 to PC via USB
- STM32CubeMX Configuration
Enable USB Device functionality Configure USB as CDC (Communication Device Class) Set up GPIO pin for LED output Configure system clock for USB operation
- Build and Flash
Open project in STM32CubeIDE Build the project Flash to your STM32 board
๐ฎ Usage
- Connect to Serial Port
Connect STM32 to your computer via USB Open Device Manager (Windows) to find the COM port number Open your preferred serial terminal application Connect to the COM port with any baud rate (USB CDC ignores baud rate)
- Available Commands
ON - Turns the LED ON OFF - Turns the LED OFF
- Expected Behavior STM32 is Ready to Receive Data
ON Turning ON LED OFF Turning OFF LED ๐ Project Structure โโโ Core/ โ โโโ Src/ โ โ โโโ main.c # Main application logic โ โ โโโ usbd_cdc_if.c # USB CDC interface implementation โ โ โโโ ... โ โโโ Inc/ โ โโโ main.h โ โโโ usbd_cdc_if.h โ โโโ ... โโโ USB_DEVICE/ โ โโโ ... # USB device stack files โโโ README.md ๐ Key Functions main.c
toggleLED(): Processes ON/OFF commands and controls LED main(): Initializes hardware and handles command processing loop
usbd_cdc_if.c
CDC_Receive_FS(): Handles incoming USB data CDC_Transmit_FS(): Sends data back to PC via USB
Try increasing the initial delay in main.c Ensure your serial terminal is connected before the message is sent Send the first command (ON/OFF) to verify connection
LED Not Responding
Check GPIO pin configuration in STM32CubeMX Verify LED wiring and polarity Ensure commands are sent as plain text without extra characters
USB Not Recognized
Install STM32 VCP drivers if needed Try a different USB cable (ensure it's data-capable) Check USB device configuration in STM32CubeMX
๐ก๏ธ Code Safety Features
Buffer overflow protection Null termination safety for string operations USB busy state checking before transmission Proper memory management for received data
๐ Extending the Project This basic framework can be extended to:
Control multiple LEDs Add more complex commands Implement sensor data logging Create a simple command-line interface Add PWM control for LED brightness
๐ License This project is provided as-is for educational and development purposes. Feel free to modify and use in your own projects. ๐ค Contributing Feel free to submit issues, fork the repository, and create pull requests for any improvements. ๐ Support If you encounter any issues or have questions about this project, please open an issue in the GitHub repository.
Happy coding! ๐