{"id":3363,"date":"2025-02-18T15:46:41","date_gmt":"2025-02-18T15:46:41","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=3363"},"modified":"2025-02-18T16:20:05","modified_gmt":"2025-02-18T16:20:05","slug":"wireless-bluetooth-communication","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=3363","title":{"rendered":"Wireless : Bluetooth Communication"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-6ff2343e76739e70fb7cd67d5c4dc200 wp-block-paragraph\" style=\"color:#5c5c5c\">Bluetooth is a wireless technology used for short-range communication between devices. It operates using radio waves in the 2.4 GHz frequency range and is commonly used for connecting peripherals like headphones, speakers, keyboards, mice, and even smart home devices. There are different versions of Bluetooth, with newer versions (like Bluetooth 5.0, 5.1, and 5.2) offering better range, speed, and power efficiency compared to older versions. There are many devices available for Bluetooth communication, HC05\/06 is the most popular one. In this article we use HC05 IC for Bluetooth communication. Let\u2019s start our journey with simple basic commands. Every HC05 module has pin configuration shown in the back side, both Rx &amp; Tx pin can operate in 3.3V, but power supply need 3.6V~6V. Connect Rx pin of HC05 to Tx pin &amp; Tx pin of HC05 to Rx pin of the microcontroller. EN pin is used to select for command or data mode. EN -> 5V enters into command mode &amp; EN->0V enters into normal data mode. By default the device has name HC-05, pin is either 1234 or 0000 &amp; baud rate 38400bits\/s.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-700bc59ccb6a471e03e3d7bff93f661d wp-block-paragraph\" style=\"color:#5c5c5c\">If any problem entering into AT command mode- Press the push button and re-power the BT module, changes it to continue glow for long time, means we successfully entered at AT command mode. &nbsp; Let\u2019s first see some necessary command &amp; Change some parameter-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1024\" height=\"317\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Command-Format-1024x317.jpg\" alt=\"\" class=\"wp-image-3368\" style=\"width:743px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Command-Format-1024x317.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Command-Format-300x93.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Command-Format-768x238.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Command-Format.jpg 1268w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-6a2015035bb1ea3026e0296acf61f2d4 wp-block-paragraph\" style=\"color:#5c5c5c\">Use any serial terminal software for command, here I use \ud83d\udcbb Realterm software for command. Only 3 parameters which will we set-<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"114\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Set-Command-1024x114.jpg\" alt=\"\" class=\"wp-image-3371\" style=\"width:735px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Set-Command-1024x114.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Set-Command-300x33.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Set-Command-768x86.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/HC05-Set-Command.jpg 1201w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-9aee6a1018cfe78d07ae80209c373f4b wp-block-paragraph\" style=\"color:#5c5c5c\">After device configuration, now it is for setting the Bluetooth terminal of mobile. Pair the HC05 with your phone then install \ud83d\udcf1 Serial Bluetooth app. The Serial Bluetooth Terminal app can help you to transfer data via Bluetooth.\u00a0 In Serial Bluetooth Terminal -> Devices -> Bluetooth Classic you will find your device. Here we use 2 load with GPIO &amp; 1 load with PWM. 2 GPIO pin will use for simple ON\/OFF load &amp; 1 PWM pin will use for pulse control. In this article we use system clock as 48MHz internal clock. For PWM the formula is-<\/p>\n\n\n\n<p class=\"has-text-align-center has-text-color has-link-color wp-elements-cddbba49c1b72554ff9951c1dfee52aa wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp; PWM frequency f<sub>PWM<\/sub> = f<sub>cpu<\/sub>\/((1+Prescaler)*(1+ARR))<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-914f7c9edc96db7c425f7cf121c3ad80 wp-block-paragraph\" style=\"color:#5c5c5c\">Let ARR=19999 &amp;Prescaler 47, will generate a frequency of 50Hz. For 50% Duty cycle the CCRx register value will be 9999. Here I use Timer 15 channel 1 for PWM, for 50% Duty cycle-<\/p>\n\n\n\n<p class=\"has-text-align-center has-text-color has-link-color has-medium-font-size wp-elements-4c6a394ae85b54da9bf9fe25176a5988 wp-block-paragraph\" style=\"color:#252525\">TIM15-&gt;CCR1=9999<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-514410dc29cbb74908abd84184824fbe wp-block-paragraph\" style=\"color:#5c5c5c\">Initialize UART with 9600 baud rate, 1 stop bit &amp; None parity. Here I use UART1 for communicating with Interrupt function. Use &#8216;\\n&#8217; or &#8216;\\r&#8217; as the last received character. The received function-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nchar display&#x5B;20];\nuint8_t rx_char,i;\nextern UART_HandleTypeDef huart1;\nvoid HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)\n{\n  if(huart-&gt;Instance==USART1 )\n  {\n    if(rx_char==&#039;\\r&#039; || rx_char==&#039;\\n&#039;)\n    i=0;\n    else\n    display&#x5B;i++]=rx_char;\n    HAL_UART_Receive_IT(&amp;huart1,&amp;rx_char,1);\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-4652bdcaedc87f18a6a64bf012e9f9c2 wp-block-paragraph\" style=\"color:#5c5c5c\">For GPIO control same logic will works on LOAD1 &amp; LOAD2.<\/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\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nif(strstr(display,&quot;load1&quot;))\n    {\n      HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_0);\n      if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0))\n      OLCD_write_string(5,1,&quot;LD1 ON &quot;);\n      else OLCD_write_string(5,1,&quot;LD1 OFF&quot;);\n    }\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nif(strstr(display,&quot;load2&quot;))\n    {\n      HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_1);\n      if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_1))\n      OLCD_write_string(5,9,&quot;LD2 ON &quot;);\n      else OLCD_write_string(5,9,&quot;LD2 OFF&quot;);\n    }\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-57f93edfdbd9ca75c1f8873b3f39a9d1 wp-block-paragraph\" style=\"color:#5c5c5c\">For PWM on\/off we need some modification as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nuint8_t b_fan,duty=50;\nchar fan_display&#x5B;16];\nif(strstr(display,&quot;fan&quot;))\n    {\n      b_fan=b_fan^0x01;\n      if(b_fan)\n      {\n        HAL_TIM_PWM_Start(&amp;htim15,TIM_CHANNEL_1);\n        sprintf(fan_display,&quot;FAN Sp:%i    &quot;,duty);\n        OLCD_write_string(6,1,fan_display);\n      }else{\n        HAL_TIM_PWM_Stop(&amp;htim15,TIM_CHANNEL_1);\n        OLCD_write_string(6,1,&quot;FAN OFF        &quot;);\n      }\n    }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-1f1a38d95fd1d302d9d811dcbfbbb9a0 wp-block-paragraph\" style=\"color:#5c5c5c\">For changing duty cycle we need some modification, here we use different duty cycle.<\/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\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nif(strstr(display,&quot;increment&quot;))\n    {\n      if(b_fan)\n      {\n       speed++;\n       if(speed&gt;=5)speed=4;\n       switch(speed)\n       {\n       case 0: TIM15-&gt;CCR1=4999; \/\/for 25% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:25% &quot;);\n       duty=25;\n       break;\n       case 1: TIM15-&gt;CCR1=9999; \/\/for 50% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:50% &quot;);\n       duty=50;\n       break;\n       case 2: TIM15-&gt;CCR1=14999;\/\/for 75% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:75% &quot;);\n       duty=75;\n       break;\n       case 3: TIM15-&gt;CCR1=17999;\/\/for 90% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:90%  &quot;);\n       duty=90;\n       break;\n\t   case 4: TIM15-&gt;CCR1=19998;\/\/for 100% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:100%  &quot;);\n       duty=100;\n       break;\n       }\n      }\n    }\n<\/pre><\/div><\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\"><div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nif(strstr(display,&quot;decrement&quot;))\n    {\n      if(b_fan)\n      {\n       speed--;\n       if(speed&lt;=0)speed=0;\n       switch(speed)\n       {\n       case 0: TIM15-&gt;CCR1=4999; \/\/for 25% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:25% &quot;);\n       duty=25;\n       break;\n       case 1: TIM15-&gt;CCR1=9999; \/\/for 50% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:50% &quot;);\n       duty=50;\n       break;\n       case 2: TIM15-&gt;CCR1=14999;\/\/for 75% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:75% &quot;);\n       duty=75;\n       break;\n       case 3: TIM15-&gt;CCR1=17999;\/\/for 90% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:90% &quot;);\n       duty=90;\n       break;\n\t   case 4: TIM15-&gt;CCR1=19998;\/\/for 100% duty cycle\n       OLCD_write_string(6,1,&quot;FAN ON SP:100%  &quot;);\n       duty=100;\n       break;\n       }\n      }\n\t}\n<\/pre><\/div><\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c1face4bc3e2e5a39e32f8f058906f9b wp-block-paragraph\" style=\"color:#5c5c5c\">Combine those function will be our full program. Now for mobile site change some setting-<\/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 decoding=\"async\" width=\"671\" height=\"269\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Function-5.jpg\" alt=\"\" class=\"wp-image-3386\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Function-5.jpg 671w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Function-5-300x120.jpg 300w\" sizes=\"(max-width: 671px) 100vw, 671px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">For setting please see my Youtube video. Here I use OLED for display purpose. In the video part I use RTC timer to ON\/OFF any load at any time&amp;date, but I don\u2019t describe it in this article. Since I only describe the basic part I omitted that. Leave the EN &amp; STATE pin unconnected, since the microcontroller just only read the sting from the mobile terminal.<\/p>\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 aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"478\" height=\"1024\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Connection-478x1024.jpg\" alt=\"\" class=\"wp-image-3387\" style=\"width:175px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Connection-478x1024.jpg 478w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Connection-140x300.jpg 140w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Connection-768x1644.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Connection-718x1536.jpg 718w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Connection-957x2048.jpg 957w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/Connection.jpg 1080w\" sizes=\"(max-width: 478px) 100vw, 478px\" \/><\/figure>\n<\/div>\n<\/div>\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=\"Wireless &amp; IoT : Bluetooth Communication\" width=\"640\" height=\"360\" src=\"https:\/\/www.youtube.com\/embed\/i4GoQBdhSTc?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<p class=\"wp-block-paragraph\"><\/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<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\/02\/BLUETOOTH.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--50);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--50)\">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-b305cd0235ec7f297db01194d3c7c027 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2025\/02\/BLUETOOTH.rar\">BLUETOOTH.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bluetooth is a wireless technology used for short-range communication between devices. It operates using radio waves in the 2.4 GHz frequency range and is commonly used for connecting peripherals like headphones, speakers, keyboards, mice, and even smart home devices. There are different versions of Bluetooth, with newer versions (like Bluetooth 5.0, 5.1, and 5.2) offering [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-3363","post","type-post","status-publish","format-standard","hentry","category-wireless-iot"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3363","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=3363"}],"version-history":[{"count":21,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3363\/revisions"}],"predecessor-version":[{"id":3397,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/3363\/revisions\/3397"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}