STM32 ADC: Read Analog Values with STM32 Microcontroller

Reading analog values is one of the most common tasks in embedded systems. Sensors like temperature, light, and pressure often give analog signals. Without an ADC, a microcontroller cannot read them. Beginners often struggle with ADC configuration on STM32 because of multiple channels, resolution settings, and CubeIDE options. This STM32 ADC tutorial will guide you step by step. We will use STM32CubeIDE to configure ADC, write example code, and test readings on an STM32 microcontroller.

What is ADC in STM32?

ADC (Analog to Digital Converter) converts analog signals into digital values. STM32 microcontrollers have built-in ADC modules with the following features:

  • Resolution: 6, 8, 10, or 12 bits depending on settings.

  • Multiple channels (up to 16 or more).

  • Single, continuous, and scan conversion modes.

  • Support for DMA (Direct Memory Access) to handle fast sampling.

  • Conversion speeds up to several MSPS (mega samples per second).

STM32 ADC Pinout

The actual ADC pins depend on the STM32 family. For example:

  • STM32F103C8T6 (Blue Pill): ADC pins on PA0 to PA7.

  • STM32F407 Discovery: ADC pins on PA0 to PA3, PC0 to PC5, etc.

Always check the datasheet or CubeIDE pinout for your board.

Configuring STM32 ADC in STM32CubeIDE

Step 1: Create New Project

  • Open STM32CubeIDE and start a new project.

  • Select your board (e.g., STM32F103C8).

Step 2: Configure ADC

  1. In Pinout View, select an analog pin (for example, PA0).

  2. CubeIDE will automatically assign it as ADC1_IN0.

  3. In the Configuration Panel, set:

    • Resolution: 12 bits.

    • Conversion Mode: Continuous or Single.

    • Data Alignment: Right.

Step 3: Generate Code

Click Generate Code. CubeIDE will create initialization functions for ADC.

STM32 ADC Example Code

Here’s a simple program to read ADC values from PA0 and store them in a variable:

#include "main.h"

uint32_t adcVal = 0;

int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_ADC1_Init();

while (1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
adcVal = HAL_ADC_GetValue(&hadc1);
HAL_Delay(500);
}
}

This reads the analog voltage at PA0 and stores the digital value in adcVal.

STM32 ADC Example with UART Output

To view ADC values, send them over UART:

#include "stdio.h"

char buffer[50];

while (1)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
adcVal = HAL_ADC_GetValue(&hadc1);
sprintf(buffer, “ADC Value: %lu\r\n”, adcVal);
HAL_UART_Transmit(&huart2, (uint8_t*)buffer, strlen(buffer), HAL_MAX_DELAY);
HAL_Delay(500);
}

Now, values appear on a serial terminal like PuTTY or Arduino Serial Monitor.

STM32 ADC with DMA

DMA allows the ADC to transfer data directly to memory without CPU load.

Benefits of Using DMA

  • Faster sampling.

  • Reduced CPU usage.

  • Better for applications like audio or waveform capture.

Example DMA use case: Reading multiple channels continuously for real-time monitoring.

Converting ADC Value to Voltage

If the reference voltage is 3.3V and ADC resolution is 12 bits (0–4095):

float voltage = (adcVal * 3.3) / 4095.0;

This converts ADC readings into actual voltage values.

Applications of STM32 ADC

  • Reading temperature from sensors like LM35 or TMP36.

  • Reading light levels using LDR.

  • Battery voltage monitoring.

  • Interfacing potentiometers for control.

  • Audio input sampling.

Common Problems in STM32 ADC

  • Noise in ADC readings – Use capacitors near power pins.

  • Wrong pin configuration – Double-check CubeIDE pinout.

  • Floating ADC pins – Always connect sensors or use pull-down.

  • Reference mismatch – Ensure Vref+ is set correctly.

FAQ on STM32 ADC

What is the resolution of STM32 ADC?
Most STM32 ADCs support up to 12-bit resolution.

Which STM32 pins support ADC?
Depends on the series. Check the datasheet or CubeIDE.

Can I use multiple ADC channels at once?
Yes, STM32 ADC supports multi-channel scanning.

What is the speed of STM32 ADC?
Up to several MSPS depending on the microcontroller family.

Does STM32 support DMA with ADC?
Yes, DMA can be used for continuous high-speed ADC sampling.

How to reduce ADC noise on STM32?
Use averaging, filtering, and decoupling capacitors.

Can I read battery voltage with STM32 ADC?
Yes, by connecting it to an ADC pin through a voltage divider.

What is the Vref for STM32 ADC?
Usually 3.3V, but some boards support external reference.

Can STM32 ADC read audio signals?
Yes, with proper conditioning circuits.

Does STM32CubeIDE support ADC configuration?
Yes, ADC can be configured easily using CubeIDE graphical interface.

Can STM32 ADC read negative voltages?
No, STM32 ADC can only read between 0V and Vref. Use an op-amp or level shifter for negative inputs.

How many ADC channels does STM32 have?
It depends on the family. For example, STM32F103 has 10 channels, STM32F407 has up to 16.

What is the maximum input voltage for STM32 ADC?
It is limited to Vref+, usually 3.3V. Exceeding this may damage the microcontroller.

How to use multiple ADC channels on STM32?
Enable scan mode in CubeIDE, configure multiple inputs, and read them in sequence or with DMA.

Does STM32 ADC support interrupts?
Yes, you can enable ADC interrupts to trigger when a conversion is complete.

What is oversampling in STM32 ADC?
Oversampling improves resolution by averaging multiple samples, supported in newer STM32 series.

Can I use STM32 ADC for current measurement?
Yes, but you need a shunt resistor and possibly an amplifier to convert current into voltage.

What is the default ADC reference voltage in STM32?
It is usually the supply voltage (3.3V), but some models support external Vref pins.

Can STM32 ADC work with DMA and interrupts together?
Yes, you can combine DMA with interrupts for continuous background sampling.

What is the formula for converting STM32 ADC values to voltage?
Voltage = (ADC_Value / (2^Resolution – 1)) Ă— Vref. For 12-bit ADC at 3.3V: Voltage = (ADC_Value * 3.3) / 4095.

Conclusion

This STM32 ADC tutorial explained how to read analog values step by step. Using STM32CubeIDE makes configuration simple, and with UART output, you can monitor sensor data in real time. By adding DMA, you can build advanced projects such as waveform analyzers or IoT sensors.

For more STM32 tutorials and projects, visit ControllersTech and start building your embedded systems journey today.

Trusted Love Percentage Calculator Check Compatibility

Throughout history, people have tried to interpret love in countless ways—through poetry, symbols, and traditions. In the digital age, one of the most engaging...

What Is White Cement, And How Can It Be Used?

White cement is a type of cement composed almost entirely of calcium hydroxide. It's used for waterproofing and as a refractory material in construction....

Bapesta: The Streetwear Icon That Redefined Sneaker Culture

Streetwear and sneaker culture are deeply intertwined, with certain silhouettes transcending trends and becoming cultural landmarks. Among them stands the Bapesta, a sneaker that...

How Disposible Vape Box Help Brand With Packaging

What Is Disposible Vape Box Printed disposible vape boxes is packaging for vape pen and cartridge. Box give product cover and help brand share product...

Why Carnelian Stones Shine Best in Sterling Silver Settings

Carnelian, with its fiery orange and red hues, has captivated jewelry lovers for centuries. This vibrant gemstone, a variety of chalcedony, is celebrated for...

Understanding the 20kW Solar System Price in Pakistan

The demand for renewable energy solutions in Pakistan has never been higher. With electricity costs rising and frequent power outages disrupting daily life, many...
Skip to toolbar