{"id":562,"date":"2021-11-11T04:53:41","date_gmt":"2021-11-11T04:53:41","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=562"},"modified":"2021-11-11T04:55:00","modified_gmt":"2021-11-11T04:55:00","slug":"working-with-stm32f7-and-adc-part-1-single-channel-single-conversion","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=562","title":{"rendered":"Working with STM32F7 and ADC part 1: Single channel Single Conversion"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"323\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1024x323.png\" alt=\"\" class=\"wp-image-202\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1024x323.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-300x95.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-768x243.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-1150x363.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-750x237.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-400x126.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_-250x79.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_.png 1200w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide, we took a look at the STM32F7 blinking LED (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_.png\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/1200px-ADC_Symbol.svg_.png\" target=\"_blank\">here<\/a>) and how to change the core frequency to push it to max 216MHz (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=531\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=531\" target=\"_blank\">here<\/a>). In this guide, we shall use the ADC module to read the voltage applied to pin PA3 of the MCU.<\/p>\n\n\n\n<p>In this guide, we shall learn how to use the ADC of the STM32F7 for single channel Single Conversion mode using only registers.<\/p>\n\n\n\n<p>The ADC is commonly used to measure the voltage from a sensor for example temperature sensor such as LM35 which can provide voltage proportional to the temperature.<\/p>\n\n\n\n<p>This guide will cover the following<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is the ADC<\/li><li>Required Parts<\/li><li>Schematic<\/li><li>Code<\/li><li>Results<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 What is the 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 STM32F411 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 href=\"https:\/\/en.wikipedia.org\/wiki\/Successive-approximation_ADC\" target=\"_blank\" rel=\"noreferrer noopener\">Link<\/a>)<\/p>\n\n\n\n<p>For more information about how the adc of STM32F4 works, refer to the user manual (RM0383).<\/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<h2 class=\"wp-block-heading\">2. Required Parts:<\/h2>\n\n\n\n<p>In this guide, we need the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>STM32F767ZI Nucloe-144<\/li><li>Breadboard<\/li><li>Potentiometer<\/li><li>connection wires<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Schematic<\/h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"948\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-1024x948.png\" alt=\"\" class=\"wp-image-563\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-1024x948.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-300x278.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-768x711.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-1536x1421.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-1150x1064.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-750x694.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-400x370.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM-250x231.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.21.34-AM.png 1662w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p>The connection is as following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Right leg of the pot to 3V3 <\/li><li>Middle leg to A0 of Arduino connection (PA3)<\/li><li>Left leg to GND<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code<\/h2>\n\n\n\n<p>Before we start, we need to get the actual pin of Arduino A0. From Nucleo-144 User Manual, Arduino A0 is connected to PA3.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"947\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-1024x947.png\" alt=\"\" class=\"wp-image-564\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-1024x947.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-300x277.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-768x710.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-1536x1420.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-1150x1063.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-750x694.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-400x370.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM-250x231.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.28.15-AM.png 1806w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, we need to know which channel of ADC is connected to PA3. From the datasheet, PA3 is connected to ADC1_CH3.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"105\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-1024x105.png\" alt=\"\" class=\"wp-image-565\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-1024x105.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-300x31.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-768x79.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-1536x157.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-2048x210.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-1150x118.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-750x77.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-400x41.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.31.39-AM-250x26.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>After that we can start configuring our pin and ADC.<\/p>\n\n\n\n<p>We start off by Enabling Clock access to GPIOA.<\/p>\n\n\n\n<p>Since the GPIOA is connected to AHB1 bus as shown in figure below<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"671\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-1024x671.png\" alt=\"\" class=\"wp-image-566\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-1024x671.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-300x196.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-768x503.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-1150x753.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-750x491.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-400x262.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM-250x164.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.36.43-AM.png 1292w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Hence we can enable clock access to GPIOA 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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;<\/pre><\/div>\n\n\n\n<p>Since ADC is connected to APB2, we can enable clock access to ADC1 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;}\">RCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN;<\/pre><\/div>\n\n\n\n<p>After that we can configure PA3 as analog. From the reference manual, GPIO section, mode register.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"625\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-1024x625.png\" alt=\"\" class=\"wp-image-567\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-1024x625.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-300x183.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-768x469.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-1536x937.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-1150x702.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-750x458.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-400x244.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM-250x153.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.40.04-AM.png 1980w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In order to set PA3 as analog, we need to set bit 6 and bit 7 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;}\">GPIOA-&gt;MODER|=GPIO_MODER_MODER3_0|GPIO_MODER_MODER3_1;<\/pre><\/div>\n\n\n\n<p>Now, we can start configure the ADC module.<\/p>\n\n\n\n<p>We start off by setting the rank of the channel. We need to set PA3 to be first in conversion sequence as following<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"219\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-1024x219.png\" alt=\"\" class=\"wp-image-214\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-1024x219.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-300x64.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-768x165.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-1536x329.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-2048x439.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-1150x246.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-750x161.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-400x86.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.51.28-AM-1-250x54.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">#define CH3 0x03\nADC1-&gt;SQR3|=(CH3&lt;&lt;0);<\/pre><\/div>\n\n\n\n<p>After that we enable the ADC:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"413\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-1024x413.png\" alt=\"\" class=\"wp-image-569\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-1024x413.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-300x121.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-768x310.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-1536x620.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-1150x464.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-750x303.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-400x161.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM-250x101.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.48.07-AM.png 2008w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">ADC1-&gt;CR2|=ADC_CR2_ADON;<\/pre><\/div>\n\n\n\n<p>In the main loop<\/p>\n\n\n\n<p>We start the conversion:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"413\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-1024x413.png\" alt=\"\" class=\"wp-image-570\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-1024x413.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-300x121.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-768x310.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-1536x620.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-1150x464.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-750x303.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-400x161.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM-250x101.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-11-at-7.49.05-AM.png 2008w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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;}\">ADC1-&gt;CR2|=ADC_CR2_SWSTART;<\/pre><\/div>\n\n\n\n<p>wait until the conversion is ended<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"2376\" height=\"692\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited.png\" alt=\"\" class=\"wp-image-211\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited.png 2376w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-300x87.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-1024x298.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-768x224.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-1536x447.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-2048x596.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-1150x335.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-750x218.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-400x116.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.43.02-AM-edited-250x73.png 250w\" sizes=\"(max-width: 2376px) 100vw, 2376px\" \/><\/figure>\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;}\">while(((ADC1-&gt;SR) &amp; ADC_SR_EOC) == 0);<\/pre><\/div>\n\n\n\n<p>store the adc data to a variable <\/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;}\">adc_data=ADC1-&gt;DR;<\/pre><\/div>\n\n\n\n<p>hence the entire code is 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;}\">#include &quot;stm32f7xx.h&quot;                  \/\/ Device header\n\n#define CH3 0x03\n\nuint16_t adc_data;\n\nint main(void)\n\t{\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\n\tRCC-&gt;APB2ENR|=RCC_APB2ENR_ADC1EN;\n\t\n\tGPIOA-&gt;MODER|=GPIO_MODER_MODER3_0|GPIO_MODER_MODER3_1;\n\tADC1-&gt;SQR1=0;\n\tADC1-&gt;SQR3|=(CH3&lt;&lt;0);\n\tADC1-&gt;CR2|=ADC_CR2_ADON;\n\twhile(1)\n\t\t{\n\t\tADC1-&gt;CR2|=ADC_CR2_SWSTART;\n\t\twhile(((ADC1-&gt;SR) &amp; ADC_SR_EOC) == 0);\n\t\tadc_data=ADC1-&gt;DR;\n\t\t}\n\t\n\t\n\t}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. Result<\/h2>\n\n\n\n<p>After compiling the code, uploading the code to mcu, opening a debug session in keil uVision\u00a0<\/p>\n\n\n\n<p>Rotate the potentiometer and observe the adc_data in watch window<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"242\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-1024x242.png\" alt=\"\" class=\"wp-image-215\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-1024x242.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-300x71.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-768x181.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-1536x363.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-2048x484.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-1150x272.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-750x177.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-400x94.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.36-AM-250x59.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Potentiometer rotate Counter clockwise&nbsp;<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"242\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1024x242.png\" alt=\"\" class=\"wp-image-217\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1024x242.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-300x71.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-768x181.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1536x363.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-2048x484.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-1150x272.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-750x177.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-400x94.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/07\/Screen-Shot-2021-07-26-at-10.57.42-AM-250x59.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Happy coding \ud83d\ude42 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide, we took a look at the STM32F7 blinking LED (here) and how to change the core frequency to push it to max 216MHz (here). In this guide, we shall use the ADC module to read the voltage applied to pin PA3 of the MCU. In this guide, we shall learn how [&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-562","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\/562"}],"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=562"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions"}],"predecessor-version":[{"id":573,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/562\/revisions\/573"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=562"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}