A/D Converters (ADC)

In this tutorial, I will show you how to use ADC in STM32F303CBT6/STM32F103C8T6 to measure the Input Analog Voltages.

Analog to Digital Converters (ADC) translate analog electrical signals for data processing purposes. With products matching performance, power, cost, and size needs, Analog Devices offers the industry’s largest A/D converter portfolio. As the world’s leading provider, these data converters enable accurate and reliable conversion performance in a range of applications such as communications, energy, healthcare, instrumentation and measurement, motor and power control, industrial automation, and aerospace/defense. A variety of A/D converter resources are provided to assist the engineer in every project phase, from product selection to circuit design.

Components Required:

  • STM32F303CB/STM32 blue pill boards – 1
  • 10k potentiometer -1
  • ST-LINK – 2
  • FTDI – 2
  • Breadboard – 2
  • Jumper wires

STM32F303

STM32F103C8T6 is a 32-bit microcontroller manufactured by STMicroelectronics having ARM Cortex-M3 Architecture. The part number indicates the below specifications :

  • STM : Manufactures name STMicroelectronics
  • 32 : 32-bit ARM architecture
  • F103 : Indicate the architecture ARM Cortex M3
  • C : 48 pins
  • 8 : 64 KB Flash Memory
  • T : package type LQFP

6 : Operating Temperature – 40°C to 85°C

ADC in STM32

The ADC embedded in STM32 microcontrollers uses the SAR (successive approximation register) principle, by which the conversion is performed in several steps. The number of conversion steps is equal to the number of bits in the ADC converter. Each step is driven by the ADC clock. Each ADC clock produces one bit from result to output. The ADC internal design is based on the switched-capacitor technique.

12-bit Resolution

This ADC is a 10 channel 12 -bit ADC. Here the term 10 channel implies that there are 10 ADC pins using which we can measure analog voltage. The term 12-bit implies the resolution of the ADC. 12-bit means 2 to the power of ten (212) which is 4096. This is the number of sample steps for our ADC, so the range of our ADC values will be from 0 to 4095. The value will increase from 0 to 4095 based on the value of voltage per step, which can be calculated by formula

VOLTAGE / STEP = REFERENCE VOLTAGE / 4096 = (3.3/4096= 8.056mV) per unit.

How an Analog Signal is converted into Digital Format

As computers store and process only binary/digital values (1’s and 0’s).  So Analog signals like sensor’s output in volts has to be converted into digital values for processing and the conversion needs to be accurate .When a input analog voltage is given to STM32 at its Analog inputs, the analog value is read and stored in a integer variable. That stored Analog value(0-3.3V )  is converted into integers values (0-4096) using the formula below:

INPUT VOLTAGE = (ADC Value / ADC Resolution) * Reference Voltage

Resolution = 4096

Reference = 3.3V

STM32 ADC Formulas

ADC Conversion Time

Tconv = Sampling time + 12.5 cycles

ADC Sampling Rate

SamplingRate = 1 / Tconv

ADC Result Voltage (Analog Input Value)

Vin = ADC_Res x (Reference Voltage / 4096)

Where Reference Voltage = (VREF+) – (VREF-)

ADC Features

  • 12-bit resolution
  • Interrupt generation at End of Conversion, End of Injected conversion and Analog watchdog event
  • Single and continuous conversion modes
  • Scan mode for automatic conversion of channel 0 to channel ‘n’
  • Self-calibration
  • Data alignment with in-built data coherency
  • Channel by channel programmable sampling time
  • External trigger option for both regular and injected conversion
  • Discontinuous mode
  • Dual-mode (on devices with 2 ADCs or more)
  • ADC conversion time: 1 µs at 56 MHz (1.17 µs at 72 MHz)
  • ADC supply requirement: 2.4 V to 3.6 V
  • ADC input range: VREF– ≤ VIN ≤ VREF+
  • DMA request generation during regular channel conversion
And now, let’s build this system step-by-step

STEP 1: Open CubeIDE & Create New Project

STEP 2: Choose The Target MCU & Double-Click Its Name
STEP 3: Configure The ADC1 Peripheral, Enable Channel & Set it to be triggered by software

Code

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include<stdio.h>
#include<string.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

uint16_t raw;
char msg[20];

Programming STM32 for reading ADC values

while (1)
  {
/* USER CODE END WHILE */

	  HAL_ADC_Start(&hadc1);
	  HAL_ADC_PollForConversion(&hadc1, 300);

	  raw =  HAL_ADC_GetValue(&hadc1);
	float vin = raw*(3.3/4096);
	sprintf(msg2," vol = %.2f\r\n",vin);
	 HAL_UART_Transmit(&huart1, (uint8_t*)msg2, strlen(msg2), 300);
	 HAL_Delay(5000);

/* USER CODE BEGIN 3 */
  }

OUTPUT

OUTPUT VIDEO

Download Code

click here

By Devilal

Leave a Reply

Your email address will not be published. Required fields are marked *