{"id":3547,"date":"2025-06-19T15:09:03","date_gmt":"2025-06-19T15:09:03","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3547"},"modified":"2025-06-19T15:09:06","modified_gmt":"2025-06-19T15:09:06","slug":"stm32-adc-application-part-5-sample-time-calculation","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3547","title":{"rendered":"STM32 ADC Application Part 5: Sample Time Calculation"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"921\" height=\"425\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1.jpg\" alt=\"\" class=\"wp-image-3553\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1.jpg 921w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1-300x138.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1-768x354.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1-750x346.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1-400x185.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/Untitled-1-250x115.jpg 250w\" sizes=\"(max-width: 921px) 100vw, 921px\" \/><\/figure>\n\n\n\n<p>In this part of the STM32 ADC series, we delve into the concept of\u00a0<strong>sample time<\/strong>\u00a0and its critical role in determining ADC accuracy and performance. Sample time directly affects how long the ADC samples the input voltage before conversion, influencing both conversion speed and the ability to accurately capture signals\u2014especially those with high source impedance.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Introduction.<\/li>\n\n\n\n<li>How to calculate sample time for each channel.<\/li>\n\n\n\n<li>Firmware development.<\/li>\n\n\n\n<li>Sample time calculation and results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Understanding ADC Sample Time and Its Importance in STM32<\/h3>\n\n\n\n<p>The&nbsp;<strong>Analog-to-Digital Converter (ADC)<\/strong>&nbsp;in STM32 microcontrollers plays a vital role in bridging the analog and digital worlds by digitizing real-world signals\u2014like temperature, voltage, or sensor readings\u2014into binary values the microcontroller can process. Among the various configurable parameters of the ADC, one of the most fundamental and sometimes overlooked is the&nbsp;<strong>sample time<\/strong>.<\/p>\n\n\n\n<p>Sample time defines the&nbsp;<strong>duration during which the ADC\u2019s internal capacitor is connected to the external analog signal<\/strong>&nbsp;before the actual digital conversion begins. This phase allows the ADC&#8217;s internal&nbsp;<strong>sample-and-hold circuit<\/strong>&nbsp;to charge to the analog input voltage. It is expressed in&nbsp;<strong>ADC clock cycles<\/strong>&nbsp;and can range from very short (e.g. 1.5 cycles) to significantly longer durations (e.g. 601.5 cycles), depending on the STM32 family and configuration.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Why Sample Time Matters<\/h3>\n\n\n\n<p>Sample time has a direct influence on both the&nbsp;<strong>accuracy<\/strong>&nbsp;and&nbsp;<strong>speed<\/strong>&nbsp;of ADC conversions. The analog input signal must be sampled long enough to allow the internal capacitor to&nbsp;<strong>settle to the correct voltage<\/strong>&nbsp;with sufficient precision. If this time is too short, especially for high-impedance sources or slowly changing signals, the voltage captured may not accurately reflect the true input, leading to conversion errors or noisy readings.<\/p>\n\n\n\n<p>Conversely, using a sample time longer than necessary slows down the conversion rate and reduces the maximum number of samples per second, which is critical in high-speed applications like audio or waveform analysis.<\/p>\n\n\n\n<p>Thus,&nbsp;<strong>choosing the right sample time is a trade-off<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Too short: inaccurate results due to insufficient charge time or voltage settling.<\/li>\n\n\n\n<li>Too long: reduced throughput and slower system response.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">What Influences the Required Sample Time?<\/h3>\n\n\n\n<p>Several factors influence how much sample time is needed for accurate ADC operation:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Source Impedance (R&lt;sub&gt;source&lt;\/sub&gt;)<\/strong>:<br>If the analog signal comes from a high-impedance source\u2014such as a resistive divider, a sensor with weak output, or through filtering resistors\u2014it takes longer for the ADC input to charge to the correct voltage. Higher source impedance \u2192 longer required sample time.<\/li>\n\n\n\n<li><strong>ADC Input Capacitance and Internal Resistance<\/strong>:<br>The STM32 ADC has an internal sample-and-hold circuit with a small capacitor (a few pF) and internal switch resistance. The time constant formed by the source resistance and ADC input affects how quickly the voltage settles.<\/li>\n\n\n\n<li><strong>Accuracy Requirements<\/strong>:<br>For 12-bit resolution or higher, the sampled voltage needs to settle within a small error margin (e.g., 1\/4096 of full scale for 12-bit), which translates into a longer settling time for accurate readings.<\/li>\n\n\n\n<li><strong>ADC Clock Frequency<\/strong>:<br>Since the sample time is measured in\u00a0<strong>ADC clock cycles<\/strong>, a faster ADC clock shortens the actual time represented by each cycle. This means that for a given settling requirement, you may need to increase the number of cycles if the ADC clock is fast.<\/li>\n\n\n\n<li><strong>Presence of Analog Buffers or Op-Amps<\/strong>:<br>Buffering the input signal with a low-output-impedance op-amp allows faster charging of the ADC capacitor and permits shorter sample times even for slow or high-impedance sources.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">Consequences of Incorrect Sample Time<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Undersampling<\/strong>: If the sample time is too short, the ADC may not capture the correct voltage, leading to\u00a0<strong>conversion errors<\/strong>,\u00a0<strong>voltage droop<\/strong>, or\u00a0<strong>oscillating results<\/strong>.<\/li>\n\n\n\n<li><strong>Increased Noise<\/strong>: Fast sampling on high-impedance sources can amplify analog noise, making the digital values unstable or incorrect.<\/li>\n\n\n\n<li><strong>Throughput Bottleneck<\/strong>: Overestimating the required sample time reduces the ADC&#8217;s maximum sampling rate, limiting the performance of real-time data acquisition.<\/li>\n\n\n\n<li><strong>Power Trade-offs<\/strong>: Longer sample times may mean the system stays active longer during each sampling cycle, slightly increasing power consumption\u2014though this is usually minimal.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h3 class=\"wp-block-heading\">The Balancing Act<\/h3>\n\n\n\n<p>Choosing the optimal sample time is an exercise in balancing:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Signal integrity<\/strong>\u00a0(accuracy, stability),<\/li>\n\n\n\n<li><strong>System requirements<\/strong>\u00a0(speed, responsiveness),<\/li>\n\n\n\n<li><strong>Analog characteristics<\/strong>\u00a0(source impedance, signal frequency),<\/li>\n\n\n\n<li>and\u00a0<strong>microcontroller limits<\/strong>\u00a0(clock configuration, resolution, performance).<\/li>\n<\/ul>\n\n\n\n<p>STM32\u2019s flexibility in offering a range of selectable sample times gives developers control over this trade-off. However, proper understanding is necessary to make informed decisions\u2014especially in precision-sensitive applications like instrumentation, sensor interfaces, or motor control.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Sample Time Calculation:<\/h2>\n\n\n\n<p>In STM32F4, the&nbsp;<strong>ADC conversion time<\/strong>&nbsp;consists of two phases:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Sampling Time (Ts)<\/strong>:<br>The time during which the ADC samples the input voltage via its sample-and-hold capacitor. You can configure this in STM32CubeMX or directly in the\u00a0<code>ADC_SAMPLETIME<\/code>\u00a0register.<\/li>\n\n\n\n<li><strong>Conversion Time (Tc)<\/strong>:<br>The time it takes the ADC to convert the sampled analog signal to a digital value. For\u00a0<strong>12-bit resolution<\/strong>, this is a fixed\u00a0<strong>12.5 ADC clock cycles<\/strong>.<\/li>\n<\/ol>\n\n\n\n<p>The function to calculate the sampling time as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"607\" height=\"148\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/formula_1.avif\" alt=\"\" class=\"wp-image-3548\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/formula_1.avif 607w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/formula_1-300x73.avif 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/formula_1-400x98.avif 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/formula_1-250x61.avif 250w\" sizes=\"(max-width: 607px) 100vw, 607px\" \/><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above formula has 3 parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Sampling Cycles<\/strong>\u00a0is the time in terms of ADC CYCLES, taken by a particular channel to sample the data. This parameter is configurable in the cubeMX and it can be configured separately for each channel.<\/li>\n\n\n\n<li><strong>@Conversion Cycles<\/strong>\u00a0is the time in terms of ADC CYCLES, taken by the ADC to convert the sampled data. This parameter is not configurable, but it depends on the ADC resolution we set in the cubeMX. The\u00a0<strong>Higher<\/strong>\u00a0the resolution, the\u00a0<strong>higher<\/strong>\u00a0will be this parameter. This parameter also varies across different MCUs, but we can find the value in the reference manual of the MCU itself.<\/li>\n\n\n\n<li><strong>@ADC Clock<\/strong>\u00a0is the clock at which the ADC is running.<\/li>\n<\/ul>\n\n\n\n<p>For example the ADC in 12-bit mode in STM32F4 needs 12 cycles and number of cycles for the channel is 3 and ADC clock is 10MHz.<\/p>\n\n\n\n<p>From the equation:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"558\" height=\"238\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-22-56.jpg\" alt=\"\" class=\"wp-image-3550\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-22-56.jpg 558w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-22-56-300x128.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-22-56-400x171.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-22-56-250x107.jpg 250w\" sizes=\"(max-width: 558px) 100vw, 558px\" \/><\/figure><\/div>\n\n\n<p>Hence, the ADC shall take 1.5uS to finish the conversion of the channel.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Firmware Development:<\/h2>\n\n\n\n<p>Once you started a new project, enable ADC CH1 as follows:<\/p>\n\n\n\n<p>STM32F4xx series:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"487\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-1024x487.jpg\" alt=\"\" class=\"wp-image-3552\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-1024x487.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-300x143.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-768x365.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-1536x730.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-2048x973.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-1150x547.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-750x356.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-400x190.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-28-00-250x119.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>STM32F303:<\/p>\n\n\n\n<p>Enable Channel 1 as single Ended<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"961\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-1024x961.png\" alt=\"\" class=\"wp-image-3554\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-1024x961.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-300x281.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-768x721.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-1150x1079.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-750x704.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-400x375.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1-250x235.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-1.png 1488w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Enable DMA for ADC1 as follows:<\/p>\n\n\n\n<p>Make sure to enable circular mode.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"895\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-1024x895.jpg\" alt=\"\" class=\"wp-image-3556\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-1024x895.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-300x262.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-768x671.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-1150x1005.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-750x656.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-400x350.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1-250x219.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-31-02-1.jpg 1432w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Enable Continuous Conversion Mode and DMA Continuous Request as follows and also make sure that the ADC resolution is 12 bit, Clock prescaler PCLK2 divided by 2.<\/p>\n\n\n\n<p>STM32F4xx:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"612\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-1024x612.jpg\" alt=\"\" class=\"wp-image-3557\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-1024x612.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-300x179.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-768x459.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-1150x687.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-750x448.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-400x239.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17-250x149.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-33-17.jpg 1436w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>STM32F303:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Set mode to independent.<\/li>\n\n\n\n<li>Clock Prescaler to ADC Asynchronous Clock Mode.<\/li>\n\n\n\n<li>Continuous Conversion mde to enabled<\/li>\n\n\n\n<li>DMA Continuous Request to enabled.<\/li>\n\n\n\n<li>Set Sampling time to 1.5 cycles.<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"908\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-1024x908.jpg\" alt=\"\" class=\"wp-image-3559\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-1024x908.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-300x266.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-768x681.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-1150x1020.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-750x665.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-400x355.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11-250x222.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-38-11.jpg 1450w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>To get the ADC Clock:<\/p>\n\n\n\n<p>From Clock configuration, we can find the ADC clock as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"505\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-1024x505.jpg\" alt=\"\" class=\"wp-image-3560\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-1024x505.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-300x148.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-768x379.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-1536x758.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-2048x1010.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-1150x567.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-750x370.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-400x197.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-41-09-250x123.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>In case there is no dedicated ADC clock, we need to refer to the datasheet of the MCU, in this case, STM32F446RE and check which bus is the ADC clocked from:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"917\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-1024x917.jpg\" alt=\"\" class=\"wp-image-3561\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-1024x917.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-300x269.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-768x688.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-1150x1030.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-750x672.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-400x358.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33-250x224.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-45-33.jpg 1346w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The 3 ADC are clocked from APB2 bus, from Clock Configuration within STM32CubeMX, we can find that APB2 clock is 16MHz, hence the ADC clock is 16MHz.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"932\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58-1024x932.jpg\" alt=\"\" class=\"wp-image-3562\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58-1024x932.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58-300x273.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58-768x699.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58-750x683.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58-400x364.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58-250x228.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_17-46-58.jpg 1140w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\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>For STM32F4 and STM32F303:<\/p>\n\n\n\n<p>In user code begin PV:<\/p>\n\n\n\n<p><\/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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define size 100\nuint16_t adcData[size];<\/pre><\/div>\n\n\n\n<p>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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)\n{\n\tHAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);\n}<\/pre><\/div>\n\n\n\n<p>Start the ADC in DMA as follows:<\/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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">HAL_ADC_Start_DMA(&amp;hadc1, (uint32_t*)adcData, size);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Sample Time Calculation and Results:<\/h2>\n\n\n\n<p>STM32F4:<\/p>\n\n\n\n<p>Since the ADC clock is 16MHz and it is divided by 2:<\/p>\n\n\n\n<p>From the reference manual:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"515\" height=\"141\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-3.png\" alt=\"\" class=\"wp-image-3564\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-3.png 515w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-3-300x82.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-3-400x110.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/image-3-250x68.png 250w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/figure><\/div>\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"662\" height=\"268\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-00-10.jpg\" alt=\"\" class=\"wp-image-3565\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-00-10.jpg 662w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-00-10-300x121.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-00-10-400x162.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-00-10-250x101.jpg 250w\" sizes=\"(max-width: 662px) 100vw, 662px\" \/><\/figure><\/div>\n\n\n<p>Since we are sampling the ADC 100 times before the DMA trigger and interrupt, we shall get 175uS toggle rate as follows:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"630\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS1.png\" alt=\"\" class=\"wp-image-3566\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS1.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS1-300x185.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS1-768x473.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS1-750x461.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS1-400x246.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS1-250x154.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We are getting 187uS which is due to interrupt overhead and HAL latency. The results are near the calculated value multiplied by 100 due to sampling 100 times.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>For STM32F303:<\/p>\n\n\n\n<p>Since the clock of the ADC 7.2MHz which we got it from Clock Configuration within CubeMX<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"108\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-1024x108.jpg\" alt=\"\" class=\"wp-image-3567\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-1024x108.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-300x32.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-768x81.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-1150x122.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-750x79.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-400x42.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25-250x26.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-04-25.jpg 1342w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"232\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-05-29.jpg\" alt=\"\" class=\"wp-image-3568\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-05-29.jpg 602w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-05-29-300x116.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-05-29-400x154.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/2025-06-19_18-05-29-250x96.jpg 250w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure><\/div>\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"630\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS0.png\" alt=\"\" class=\"wp-image-3569\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS0.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS0-300x185.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS0-768x473.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS0-750x461.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS0-400x246.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/06\/RigolDS0-250x154.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Since we are sampling 100 samples, we are getting 194.7uS which is the same as theoretical one multiplied by 100.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Next, we shall control the behaviour of the ADC using timers.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Stay tuned.<\/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 part of the STM32 ADC series, we delve into the concept of\u00a0sample time\u00a0and its critical role in determining ADC accuracy and performance. Sample time directly affects how long the ADC samples the input voltage before conversion, influencing both conversion speed and the ability to accurately capture signals\u2014especially those with high source impedance. In [&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-3547","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\/3547"}],"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=3547"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3547\/revisions"}],"predecessor-version":[{"id":3570,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3547\/revisions\/3570"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}