{"id":2450,"date":"2024-04-10T13:30:45","date_gmt":"2024-04-10T13:30:45","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2450"},"modified":"2024-04-10T13:30:48","modified_gmt":"2024-04-10T13:30:48","slug":"getting-started-with-stm32wb55-adc-continuous-conversion","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2450","title":{"rendered":"Getting Started with STM32WB55: ADC Continuous Conversion"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"1015\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-1024x1015.png\" alt=\"\" class=\"wp-image-2451\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-1024x1015.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-300x297.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-150x150.png 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-768x761.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-750x743.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-400x396.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1-250x248.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/Screenshot-2024-03-22-at-7.07.10\u202fAM-1-1.png 1130w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall configure the ADC of STM32WB55 to acquire analog data from analog sensor. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is ADC.<\/li><li>Driver Development.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is ADC:<\/h2>\n\n\n\n<p>In&nbsp;electronics, an&nbsp;<strong>analog-to-digital converter<\/strong>&nbsp;(<strong>ADC<\/strong>,&nbsp;<strong>A\/D<\/strong>, or&nbsp;<strong>A-to-D<\/strong>) is a system that converts an&nbsp;analog signal, such as a sound picked up by a&nbsp;microphone&nbsp;or light entering a&nbsp;digital camera, into a&nbsp;digital signal that can be read by STM32.&nbsp;<\/p>\n\n\n\n<p>ADCs can vary greatly between microcontroller. The ADC on the STM32F103 is a 12-bit ADC meaning it has the ability to detect 4096(2^12) discrete analog levels (which is also called&nbsp;<strong><em>Resolution<\/em><\/strong>). Some microcontrollers have 8-bit ADCs (2^8 = 256 discrete levels) and some have 16-bit ADCs (2^16 = 65,536 discrete levels).<\/p>\n\n\n\n<p>The way an ADC works is fairly complex. There are a few different ways to achieve this feat (see Wikipedia&nbsp;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Analog-to-digital_converter#ADC_types\">for a list<\/a>), but one of the most common technique uses the analog voltage to charge up an internal capacitor and then measure the time it takes to discharge across an internal resistor. The microcontroller monitors the number of clock cycles that pass before the capacitor is discharged. This number of cycles is the number that is returned once the ADC is complete. Other methods used called Successive-approximation ADC which you can read about it in details from this wikipedia page (<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Successive-approximation_ADC\" target=\"_blank\">Link<\/a>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"relating-adc-value-to-voltage\">1.2 Relating ADC Value to Voltage<\/h2>\n\n\n\n<p>The ADC reports a&nbsp;<em>ratio metric value<\/em>. This means that the ADC assumes 3.3V is 4095 and anything less than 3.3V will be a ratio between 3.3V and 4095.<\/p>\n\n\n\n<p>For example if the sensor has output voltage of 1.66V hence the ADC output will be (4095\/3.3)*1.66=2059.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Driver Development:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>We start off by creating new project with name of ADC_Cont. Refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2403\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2403\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a> for how to create new project and clock setup.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>From the schematics of STM32WB55, the arduino connectors, the analog section (A0 to A5):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"523\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26-1024x523.jpg\" alt=\"\" class=\"wp-image-2452\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26-1024x523.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26-300x153.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26-768x393.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26-750x383.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26-400x204.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26-250x128.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-08-26.jpg 1072w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>We can find that A0 is connected to PC0, hence enable PC0 as analog mode as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-1024x570.jpg\" alt=\"\" class=\"wp-image-2453\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-1536x854.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-2048x1139.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-400x222.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-53-25-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Enable IN1 as single ended as following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"570\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-1024x570.jpg\" alt=\"\" class=\"wp-image-2454\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-1024x570.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-300x167.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-768x427.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-1536x854.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-2048x1139.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-1150x640.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-750x417.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-400x222.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-54-250x139.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Configure the ADC parameters as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"884\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26-884x1024.jpg\" alt=\"\" class=\"wp-image-2455\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26-884x1024.jpg 884w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26-259x300.jpg 259w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26-768x889.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26-750x868.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26-400x463.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26-250x289.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_15-57-26.jpg 1038w\" sizes=\"(max-width: 884px) 100vw, 884px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Set the ADC parameters as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Set the resolution to be 8-bit.<\/li><li>Enable Continuous Conversion Mode.<\/li><li>Overrun behaviour to be overwritten.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, enable UART to print to terminal. To enable the UART, please refer to <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2435\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2435\" target=\"_blank\" rel=\"noreferrer noopener\">this guide<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>That all for the configuration. Save the project and this will generate the code.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In user begin includes, include the stdio for printf:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/* USER CODE BEGIN Includes *\/\n#include &quot;stdio.h&quot;\n\/* USER CODE END Includes *\/<\/pre><\/div>\n\n\n\n<p>In user  code begin PV, declare a variable to hold the ADC results:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\/* USER CODE BEGIN PV *\/\nuint8_t adc_data;\n\n\/* USER CODE END PV *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 0:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">int __io_putchar(int ch)\n{\n\tHAL_UART_Transmit(&amp;huart1, &amp;ch, 1, 10);\n\treturn ch;\n}\n\/* USER CODE END 0 *\/<\/pre><\/div>\n\n\n\n<p>This will retarget printf to print data over serial port.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 2:<\/p>\n\n\n\n<p>Enable the ADC as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">  \/* USER CODE BEGIN 2 *\/\n  HAL_ADC_Start(&amp;hadc1);\n  \/* USER CODE END 2 *\/<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 3:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">\t  adc_data=HAL_ADC_GetValue(&amp;hadc1);\n\t  printf(&quot;ADC Value = %d \\r\\n&quot;, adc_data);\n\t  HAL_Delay(500);<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>Get the ADC by calling HAL_ADC_GetValue.<\/li><li>Printf the ADC data to terminal.<\/li><li>Delay by a half second. <\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>Save, build and run the project on your Nucleo WB55.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Open your terminal, set the baud rate to 115200 and you should get something similar to this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"996\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-00-38.jpg\" alt=\"\" class=\"wp-image-2456\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-00-38.jpg 1010w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-00-38-300x296.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-00-38-768x757.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-00-38-750x740.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-00-38-400x394.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/04\/2024-04-10_16-00-38-250x247.jpg 250w\" sizes=\"(max-width: 1010px) 100vw, 1010px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we shall start developing the BLE Application.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall configure the ADC of STM32WB55 to acquire analog data from analog sensor. In this guide, we shall cover the following: What is ADC. Driver Development. Results. 1. What is ADC: In&nbsp;electronics, an&nbsp;analog-to-digital converter&nbsp;(ADC,&nbsp;A\/D, or&nbsp;A-to-D) is a system that converts an&nbsp;analog signal, such as a sound picked up by a&nbsp;microphone&nbsp;or light [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-2450","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2450"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2450"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2450\/revisions"}],"predecessor-version":[{"id":2457,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2450\/revisions\/2457"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}