Skip to content

SanthoshKumar-BE/stm32-usb-cdc-led-control

Repository files navigation

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

  1. Hardware Connections

Connect LED to the designated GPIO pin (check LED_GPIO_Port and LED_Pin in code) Connect STM32 to PC via USB

  1. 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

  1. Build and Flash

Open project in STM32CubeIDE Build the project Flash to your STM32 board

๐ŸŽฎ Usage

  1. 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)

  1. Available Commands

ON - Turns the LED ON OFF - Turns the LED OFF

  1. 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

โš ๏ธ Troubleshooting Initial Message Not Appearing If "STM32 is Ready to Receive Data" doesn't show up:

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! ๐ŸŽ‰

About

Simple STM32 USB CDC project for controlling LED via serial commands - Turn LED ON/OFF using virtual COM port

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages