In this article, let’s make a USB device using an STM32 microcontroller. USB is a very popular communication method and we almost every day, use such device that works with USB. Now, let’s make our own device.

Disclaimer: Electricity is always dangerous. Proper skill is required to work with electricity. Do work at your own risk. The author will not be responsible for any misuse or harmful act or any mistake you make. The contents of this website are unique and copyright protected. Kindly don’t do any nonsensical act copying and claiming it as yours. Most of the articles published here are kept open-source to help you. Take the knowledge for free and use it, but if you are interested you can buy the ready resources offered here. If you need any help or guidance feel free to comment below, the author will try to help you. Also, there can be affiliation links in the article. Which will not affect you anyway, but allows the author with some commission. So please don’t take it otherwise. Thanks.

Table of Contents


USB communication:

USB is the most popular and daily life communication protocol among all the protocols. At the same time, communication cables are easy to find. That is why this protocol is mostly practiced. There are different types of USB ports.

STM32 as USB Device

To know more about USB, please visit this Wiki link.

Anyway, let’s move on. Here for STM, we’ll use STM32CubeMx compiler. Collect a ST_link stick to upload the firmware to the MCU.

Configuration part:

Open STM32CubeMx and create a new project. Select the IC you want to use. Then configure the IC.

First: Configure the RCC (crystal Oscillator). Select Crystal/Ceramic Resonator

STM32 as USB Device

Configure SYS. Select serial wire debug if you want to. (Optional).

STM32 as USB Device

Configure USB from connectivity: Select Device (FS).

STM32 as USB Device

In the last, configure the USB_Device from the middleware. Select Communication Device Class.

STM32 as USB Device

Now, go to the clock settings and configure the clock. You can click the resolve clock issue to do some automated selection.

STM32 as USB Device

After this stage, save the console giving a name to generate the code for you by STM32cubeMx. Then. open the project.

Coding part:

First, you can put a simple toggle in the while loop. Keep in mind to put that inside the user code section.

STM32 as USB Device

Then, go to usbd_cdc_if.c and put the buffers like this. Don’t forget to declare the buffer on the top inside the user function.

STM32 as USB Device

And now, inside the CDC_Receive_FS(), just edit the following image. And it’s done.

STM32 as USB Device

You are all set. Now build the project. Then upload to your target MCU.

You may check my other articles:


Before testing:

In my case, I was suffering from the USB driver in the device manager of my PC. Try installing drivers if your one doesn’t work at the first attempt.

In my case, the device worked excellently. I hope, this guide will help you to make your own USB device using STM32, Thanks.

For Professional Designs or Help:


Also let me know what you want to get as the next article, comment below!


Mithun K. Das; B. Sc. in EEE from KUET; Head of R&D @ M's Lab Engineering Solution. "This is my personal blog. I post articles on different subjects related to electronics in the easiest way so that everything becomes easy for all, especially for beginners. If you have any questions, feel free to ask through the contact us page." Thanks.


Djalltra · 08/07/2022 at 9:25 pm

Can you do a tutorial on AC current measurement using current transformer.
Thank you

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

%d bloggers like this: