{"id":3224,"date":"2025-01-28T15:59:12","date_gmt":"2025-01-28T15:59:12","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=3224"},"modified":"2025-01-28T17:25:17","modified_gmt":"2025-01-28T17:25:17","slug":"usb-real-time-sensor-data","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=3224","title":{"rendered":"USB : Real Time Sensor Data"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-18ed81c18584174b4713606295540d04 wp-block-paragraph\" style=\"color:#5c5c5c\">The Universal Serial Bus (USB) is technology that allows a person to connect an electronic device to a computer. It is a fast serial bus type. The STM32 performance line embeds a USB device peripheral compatible with the USB full-speed 12 Mb\/s. The USB interface implements a full-speed (12 Mbit\/s) function interface. It has software-configurable endpoint setting and suspend\/resume support. The USB peripheral implements an interface between a full-speed USB 2.0 bus and the APB bus. The clock source must use a HSE crystal oscillator.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"780\" height=\"779\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Internal-Structure.jpg\" alt=\"\" class=\"wp-image-3228\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Internal-Structure.jpg 780w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Internal-Structure-300x300.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Internal-Structure-150x150.jpg 150w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Internal-Structure-768x767.jpg 768w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"775\" height=\"397\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Interface.jpg\" alt=\"\" class=\"wp-image-3239\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Interface.jpg 775w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Interface-300x154.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Interface-768x393.jpg 768w\" sizes=\"(max-width: 775px) 100vw, 775px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center has-text-color has-link-color has-medium-font-size wp-elements-bd795bc57dca4cc23665acd2c4267f0b wp-block-paragraph\" style=\"color:#252525\">Connection Diagram<\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-5f0506aba57324d6290a5a457784feab wp-block-paragraph\" style=\"color:#5c5c5c\">Not all STM32 has this USB feature. USB 2.0 version support 3 different speed i.e. low speed 1.5Mb\/s, full speed 12Mb\/s &amp; high speed 480Mb\/s. Let\u2019s look at the different class of USB-<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<ul style=\"color:#5c5c5c\" class=\"wp-block-list has-text-color has-link-color wp-elements-a7c39fca3f42622d8f2fbd02bad14602\">\n<li>Audio Device: This class allows the implementation of audio devices such as headphones and microphones.<\/li>\n\n\n\n<li>Communication Device: This class allows the implementation of virtual COM ports and modems.<\/li>\n\n\n\n<li>Download Firmware Update (DFU): Device Firmware Upgrade (DFU) is a process of updating or replacing the firmware of a hardware device. Firmware is the low-level software embedded in hardware components that controls their functionality.<\/li>\n\n\n\n<li>Human Interface Devices (HID): This class allows the implementation of Human Interface devices allowing the interaction between a human and a machine such as game controllers, mouse, keyboards.<\/li>\n\n\n\n<li>Custom HID: A Custom HID (Human Interface Device) refers to a device that uses the USB HID protocol for communication but is designed for specific, custom purposes not covered by standard HID device types like keyboards, mice, or game controllers.<\/li>\n\n\n\n<li>Mass Storage Class: This class allows the implementation of mass storage devices ensuring data storage and exchange via USB.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-00f73cb7f356b9850742fcd81142cd19 wp-block-paragraph\" style=\"color:#5c5c5c\">In this article we use USB Communication Device class for display real time sensor data to PC. Here we use simple sensor AHT20. AHT20, as a new generation of temperature and humidity sensors, has established a new standard in size and intelligence. Its uses communication protocol as I<sup>2<\/sup>C &amp; is very simple to operate.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"506\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/AHT20-Pinout-1024x506.jpg\" alt=\"\" class=\"wp-image-3246\" style=\"width:603px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/AHT20-Pinout-1024x506.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/AHT20-Pinout-300x148.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/AHT20-Pinout-768x380.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/AHT20-Pinout.jpg 1335w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c3dc30b602811df13e4cf5abe8fcd9cc wp-block-paragraph\" style=\"color:#5c5c5c\">The power supply range of AHT20 is 2.0-5.5V, and the recommended voltage is 3.3V. Please read the data sheet first. According to the data sheet the device has I<sup>2<\/sup>C address of 0x38. In order to read temperature &amp; humidity only three step will need-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"141\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-1-1024x141.jpg\" alt=\"\" class=\"wp-image-3251\" style=\"width:668px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-1-1024x141.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-1-300x41.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-1-768x106.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-1.jpg 1256w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c790dca1abb5c88f15d254d66da55eea wp-block-paragraph\" style=\"color:#5c5c5c\">In is tutorial we use STM32F103RCT6 microcontroller. I use I2C1 for OLED display purpose &amp; I2C2 for AHT20 temperature &amp; humidity measurement. You can use only 1 I2C protocol. Let\u2019s look at the code for interfacing which is very simple.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#define AHT20_ADD    0x38\n#define AHT20_INIT   0xBE\n#define AHT20_RESET  0xBA\n#define AHT20_TRIG   0xAC\nuint8_t Tx_Soft&#x5B;1]={AHT20_RESET};\nuint8_t Tx_INIT&#x5B;3]={AHT20_INIT,0x08,0x00};\nuint8_t Tx_Trig&#x5B;3]={AHT20_TRIG,0x33,0x00};\nuint8_t Rx_Data&#x5B;6];\nHAL_Delay(1000);\nHAL_I2C_Master_Transmit(&amp;hi2c2,AHT20_ADD&lt;&lt;1,Tx_INIT,3,200);\nHAL_I2C_Master_Transmit(&amp;hi2c2,AHT20_ADD&lt;&lt;1,Tx_Trig,3,200);\nHAL_Delay(100);\nHAL_I2C_Master_Receive(&amp;hi2c2,(AHT20_ADD&lt;&lt;1|0x01),Rx_Data,6,200);\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"336\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-2-1024x336.jpg\" alt=\"\" class=\"wp-image-3256\" style=\"width:771px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-2-1024x336.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-2-300x98.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-2-768x252.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-2-1536x504.jpg 1536w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/Function-2.jpg 1552w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-0906c6136e8187b08542d723bcdf4507 wp-block-paragraph\" style=\"color:#5c5c5c\">Here Rx_Data[6] represents the temperature &amp; humidity data. Rx_Data[0] -&gt; bit 7 represent measurement flag &amp; bit 3 represent calibration flag. &nbsp;Rx_Data[1], Rx_Data[2] &amp; Rx_Data[3] -&gt; upper 4 bits represent humidity. Rx_Data[4] -&gt; lower 4 bits, Rx_Data[5] &amp; Rx_Data[6] represents temperature. For calculation of<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-7c1b08726387f793ff9b97b52eb53920 wp-block-paragraph\" style=\"color:#5c5c5c\">Temperature = (S<sub>T <\/sub>\/2<sup>20<\/sup> )*200-50 Centigrade &amp; Humidity = (S<sub>RH <\/sub>\/2<sup>20<\/sup> )*100 % , 2<sup>20 <\/sup>= 1048576<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint32_t Temperature,humidity;\nfloat tem,hum;\nhumidity=(Rx_Data&#x5B;1]&lt;&lt;12)+(Rx_Data&#x5B;2]&lt;&lt;4)+(Rx_Data&#x5B;3]&gt;&gt;4);\nhum=(float)humidity\/1048576*100;\nTemperature=((Rx_Data&#x5B;3]&amp;0x0f)&lt;&lt;16)+(Rx_Data&#x5B;4]&lt;&lt;8)+(Rx_Data&#x5B;5]);\ntem=((float)Temperature\/1048576)*200-50;\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-80ef4d377ede8b5978c249481541285f wp-block-paragraph\" style=\"color:#5c5c5c\">Here I use internal RTC feature for Real time weather forecasting. Here I use OLED for display purpose, if you don\u2019t want to display ignore OLED section. Let\u2019s initialize the RTC &amp; use some string function for data transmitting to PC\/Laptop.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nRTC_TimeTypeDef sTime;\nRTC_DateTypeDef sDate;  \nchar display_time&#x5B;16],display_date&#x5B;16], display_AHT20&#x5B;16];\nchar Time_Zone&#x5B;32],Weather_Data&#x5B;32], PC_Monitor&#x5B;64];\nchar *day_name&#x5B;8]={&quot;Sun&quot;,&quot;Mon&quot;, &quot;Tue&quot;, &quot;Wed&quot;, &quot;Thu&quot;, &quot;Fri&quot;, &quot;Sat&quot;};\nHAL_RTC_GetTime(&amp;hrtc,&amp;sTime,RTC_FORMAT_BCD);\nHAL_RTC_GetDate(&amp;hrtc,&amp;sDate,RTC_FORMAT_BCD);\nsprintf(display_time,&quot; %02X:%02X:%02X &quot;,sTime.Hours,sTime.Minutes,sTime.Seconds);\nsprintf(display_date,&quot; %02X\/%02X\/%02X %s&quot;,sDate.Date,sDate.Month,sDate.Year,day_name&#x5B;sDate.WeekDay]);\nsprintf(Time_Zone,&quot;%s Time %s&quot;,display_date,display_time);\nsprintf(Weather_Data,&quot;Weather %02.2f C %02.2f %c HR&quot;,tem,hum,37);\nsprintf(PC_Monitor,&quot;%s%s\\r&quot;,Time_Zone,Weather_Data);\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-a2f5e8e4f1fac617871cfa906c679b16 wp-block-paragraph\" style=\"color:#5c5c5c\">Our final step is to transfer the string to PC\/Laptop using STM32 USB Communication Device class. The code for USB Communication Device class-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n#include &quot;usbd_cdc_if.h&quot;\nCDC_Transmit_FS(PC_Monitor, strlen(PC_Monitor));\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-7c21caee1e3a721a18269f7f4617dd70 wp-block-paragraph\" style=\"color:#5c5c5c\">Data will send to PC without any external driver. Just Connect to PC\/laptop according to the block diagram below. In PC-> Device manager see the Virtual Port number &amp; in Realterm connect according to the port number with any baud rate. For setting all parameter please follow the youtube tutorial. \u00a0<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe title=\"STM32 USB \ud83d\udc68\ud83c\udfff\u200d\ud83d\udcbbAHT20 Sensor Data Real time Monitoring\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/oZvja02nHcU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"553\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Connection-1024x553.jpg\" alt=\"\" class=\"wp-image-3273\" style=\"width:767px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Connection-1024x553.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Connection-300x162.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Connection-768x415.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Connection.jpg 1414w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Transfer-STM32F103RCT6.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">Download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p class=\"has-text-color has-link-color has-medium-font-size wp-elements-03391b4a1050e02b962addbf458fa669 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/01\/USB-Transfer-STM32F103RCT6.rar\">USB : Real Time Sensor Data<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Universal Serial Bus (USB) is technology that allows a person to connect an electronic device to a computer. It is a fast serial bus type. The STM32 performance line embeds a USB device peripheral compatible with the USB full-speed 12 Mb\/s. The USB interface implements a full-speed (12 Mbit\/s) function interface. It has software-configurable [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-3224","post","type-post","status-publish","format-standard","hentry","category-stm-arm-tutorials"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3224"}],"version-history":[{"count":33,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3224\/revisions"}],"predecessor-version":[{"id":3279,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3224\/revisions\/3279"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}