STM32 ADC Channel Select (Multi Channel Without DMA)

In this tutorial, we’ll discuss the STM32 ADC Channel Select and how to switch between different ADC channels using HAL functions. You’ll learn how to implement STM32 ADC Multi-Channel

The practical example we’ll implement in this tutorial is an STM32 LED dimmer using multiple potentiometers on multiple analog input pins and multiple PWM outputs to control the brightness of some LEDs. We’ll read the multiple analog input channels (regular group) in single-conversion mode using the ADC Channel select (switch) method. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 ADC Channel Select
  2. STM32 ADC Multi-Channel Without DMA
  3. STM32 ADC Multi-Channel Select (No Scan) Example
  4. STM32 STM32 ADC Multi-Channel Without DMA
  5. Wrap Up

STM32 ADC Channel Select

While working with the STM32 ADC with multiple channels being used, you can use the STM32 ADC Scan Mode which will automatically select and convert every single channel in the scan’s regular group of channels. Alternatively, you can disable the scan mode and manually (with software) switch between the ADC channels and convert each one as needed per your application requirements.

Here is how to manually select and switch between 4x different ADC channels and store the conversion result of the 4 channels into an array.

Using the built-in ADC scan mode is more efficient in terms of CPU time and ADC’s throughput overall. However, using the manual channel selection and conversion method (the code example above) that we’ll explain in this tutorial is very flexible and allows achieving any custom applications’ requirements. The STM32 ADC Scan Mode is illustrated in detail in the tutorial linked below (with examples using polling, interrupt, and DMA).

STM32 ADC Multi Channel (Scan) + DMA (Single-Conversion)

This tutorial will give you more in-depth information about the STM32 ADC Scan Mode and how to read multi-channels of the ADC regular group using (polling, interrupt, and DMA) in single-conversion mode with code examples for each configuration.


STM32 ADC Multi-Channel Without DMA

In this tutorial, we’ll explore the STM32 ADC Multi-Channel Without DMA operation in single-conversion (one-shot) mode. In this mode, the ADC will select a desired channel of the regular group and start the conversion process. Upon conversion completion, the application can poll for the ADC result or get an interrupt or DMA notification instead. Then, the ADC can switch to (select) another channel of the regular group and repeat the conversion process.

This manual channel selection & conversion enables us to sample different ADC channels with different periodicities (sampling rates). Here is an illustrative diagram of how this process works.

❕ Note

For the multi-channel operation of the STM32 ADC, we can use the auto “scan” mode instead of manually switching between the regular group of channels as show in this tutorial. To learn more about the STM32 ADC Multi-Channel scan mode you can check the following tutorials:


STM32 ADC Multi-Channel Select (No Scan) Example

In this example project, we’ll create an STM32 LED Dimmer using ADC & PWM to read multiple analog inputs from 4x potentiometers to control the brightness of 4x PWM outputs going to 4x LEDs. This demo will run the STM32 ADC in multi-channel single-conversion mode without DMA (no scan).

  • Set up a new project as usual with system clock @ 72MHz
  • Set up 4x Analog Input Pins (Channel 6, 7, 8, and 9) – The 4x Pot. Pins
  • Set up Timer2 in PWM mode with output on channels 1:4 (The 4x LED Pins) 

STM32 STM32 ADC Multi-Channel Without DMA

In this LAB, our goal is to build a system that initializes multiple ADC analog input pins (4x channels: 6-9). Also, configure a timer module to operate in PWM mode with 4x outputs on channels 1-4 (4x LED pins).

Then, we can select the ADC regular group channels one by one to start the conversion of each channel sequentially, get the ADC results, map them to the PWM duty cycle registers, and repeat the whole process over and over again.

And now, let’s build this system step-by-step

Step #1

Open STM32CubeMX, create a new project, and select the target microcontroller.

Step #2

Configure The ADC1 Peripheral, Enable the regular Channels: 6-9, and set the number of regular conversions to 1. You’ll find that the analog channel has these default configurations which happens to be ok for us in this example project.

STM32-ADC-Channel-Select-Multi-Channel-Without-DMA-CubeMX-Configurations.jpg

Step #3

Configure Timer2 To Operate In PWM Mode With Output On CH1-4.

STM32-Multiple-Channel-Scan-Single-Conversion-Example-CubeMX

Step #4

Go to the RCC clock configuration page and enable the HSE external crystal oscillator input.

STM32 RCC External Clock Selection CubeMX

Step #5

Go to the clock configurations page, and select the HSE as a clock source, PLL output, and type in 72MHz for the desired output system frequency. Hit the “ Enter” key, and let the application solve for the required PLL dividers/multipliers to achieve the desired clock rate.

STM32-Blue-Pill-Proteus-Library-Simulation-Clock-Configuration

Step #6

Name & Generate The Project Initialization Code For CubeIDE or The IDE You’re Using.

STM32 ADC Multi-Channel Without DMA Example Code

Here is The Application Code For This LAB (main.c)

Wiring

I know that it may look messy, but there isn’t too much about it. These are only 4x LEDs and 4x potentiometers connected to the corresponding GPIO pins on the STM32 blue pill board.

STM32-ADC-Multiple-Channel-Scan-Example

STM32 ADC Multi-Channel Without DMA Example Testing


Required Parts For STM32 Examples

All the example Code/LABs/Projects in this STM32 Series of Tutorials are done using the Dev boards & Electronic Parts Below:

QTY.Component NameAmazon.comAliExpresseBay
1STM32-F103 BluePill Board (ARM Cortex-M3 @ 72MHz)AmazonAliExpresseBay
1Nucleo-L432KC (ARM Cortex-M4 @ 80MHz)AmazonAliExpresseBay
1ST-Link V2 DebuggerAmazonAliExpresseBay
2BreadBoardAmazonAliExpresseBay
1LEDs KitAmazonAmazonAliExpresseBay
1Resistors KitAmazonAmazonAliExpresseBay
1Capacitors KitAmazonAmazonAliExpress & AliExpresseBay & eBay
1Jumper Wires PackAmazonAmazonAliExpress & AliExpresseBay & eBay
1Push ButtonsAmazonAmazonAliExpresseBay
1PotentiometersAmazonAliExpresseBay
1Micro USB CableAmazonAliExpresseBay

★ Check The Links Below For The Full Course Kit List & LAB Test Equipment Required For Debugging ★

Download Attachments

You can download all attachment files for this Article/Tutorial (project files, schematics, code, etc..) using the link below. Please consider supporting our work through the various support options listed in the link down below. Every small donation helps to keep this website up and running and ultimately supports the whole community.


Wrap Up

In conclusion, we’ve explored how to manually select and convert the STM32 ADC Channels (regular group) without using the auto “scan mode” or the DMA for reading this multi-channel configuration.

You can build on top of the examples provided in this tutorial and/or explore the other parts of the STM32 ADC tutorials series below for more information about the other STM32 ADC operating modes and conversion schemes.

Share This Page With Your Network!
Join Our +25,000 Newsletter Subscribers!

Stay Updated With All New Content Releases. You Also Get Occasional FREE Coupon Codes For Courses & Other Stuff!

Photo of author
Author
Khaled Magdy
Embedded systems engineer with several years of experience in embedded software and hardware design. I work as an embedded SW engineer in the Automotive & e-Mobility industry. However, I still do Hardware design and SW development for DSP, Control Systems, Robotics, AI/ML, and other fields I'm passionate about.
I love reading, writing, creating projects, and teaching. A reader by day and a writer by night, it's my lifestyle. I believe that the combination of brilliant minds, bold ideas, and a complete disregard for what is possible, can and will change the world! I will be there when it happens, will you?

Leave a Comment