STM32 Potentiometer Reading Examples (Single & Multiple)

In this tutorial, we’ll discuss the STM32 Potentiometer Reading and how to read single & multiple potentiometers with STM32 microcontrollers. You’ll learn how to implement STM32 ADC (Single-Channel & Multi-Channel) sampling example projects for reading (Single & Multiple) potentiometers.

The practical example we’ll implement in this tutorial is an STM32 LED dimmer using potentiometers hooked up to analog input pins and PWM outputs to control the brightness of LEDs. We’ll read single & multiple potentiometers and use them to control the brightness of the PWM output LEDs. Without further ado, let’s get right into it!

Table of Contents

  1. STM32 Potentiometer Interfacing (Reading)
  2. Reading Single Potentiometer With STM32 Example
  3. Reading Multiple Potentiometers With STM32 Example
  4. Wrap Up

STM32 Potentiometer Interfacing (Reading)

First of all, let’s explore all possible ways of interfacing potentiometers with STM32 microcontrollers. What are the possible STM32 ADC configurations (modes) that we can use to read a single potentiometer or multiple potentiometers? That’s what we’re going to summarize in this section before deciding on which ADC configuration (mode) to use in a given situation (or project).

1. Reading Single Potentiometer With STM32

The STM32 ADC’s hardware supports so many modes of operations and features that we can selectively “mix and match” to customize the ADC sampling for each analog channel. Below are the most common, not the only, configurations (modes) that can be used to sample a single ADC channel connected to a potentiometer.

Each mode has a separate tutorial + examples for further understanding if you’re interested. But we’ll use the “Single-Channel Single-Conversion Mode” in this tutorial’s single-potentiometer example.

Single-Channel Single-Conversion Mode

In this mode, the ADC starts the conversion of the selected channel only once. Upon conversion completion, the user can poll for the ADC result, trigger an interrupt, or DMA transfer instead. To start another ADC conversion for the selected single channel, it has to be manually triggered again and again.

Single-Channel Continuous-Conversion Mode

In this mode, the ADC will complete a single-channel conversion and immediately trigger itself automatically to start a new conversion and keep repeating, hence the name “continuous conversion mode”. As it doesn’t need to be repeatedly initiated, you just initiate it once and it’ll keep going until you manually stop it in software.

The STM32 ADC Continuous-Conversion Mode can also be used with Polling, Interrupt, or DMA to get the ADC reading results as soon as the conversion is completed

Timer-Triggered ADC Channel Mode

The default option to trigger the STM32 ADC to start the conversion process is the Software Trigger source. Therefore, we have to manually call the  HAL_ADC_Start() function whenever we want to start a new ADC conversion.

However, the ADC can also be automatically triggered by internal hardware timers in the STM32 microcontroller itself. This can be really useful to set the ADC conversion to occur periodically using a timer trigger to achieve a desired ADC sampling rate.

2. Reading Multiple Potentiometers With STM32

The STM32 ADC’s hardware supports so many modes of operations and features that we can selectively “mix and match” to customize the ADC sampling for each analog channel. Below are the most common, not the only, configurations (modes) that can be used to sample multiple ADC channels connected to multiple potentiometers.

Each mode has a separate tutorial + examples for further understanding if you’re interested. But we’ll use the “Multi-Channel Single-Conversion Manual (No Scan) Mode” in this tutorial’s multiple-potentiometers example.

Multi-Channel Single-Conversion Scan Mode

In this mode, the ADC will start converting the configured regular group of channels one by one according to the channel ranks (from low to high) till the end of the group where it stops and generates an interrupt signal indicating the end of the regular group’s single-conversion process. The STM32 ADC Multi-Channel Scan Mode can also be used with Polling or DMA to get the ADC reading results as soon as the conversion is completed

Multi-Channel Single-Conversion Manual (No Scan) Mode

While working with the STM32 ADC with multiple channels, 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.

Multi-Channel Continuous-Conversion Mode

In this mode, the ADC will start converting the configured regular group of channels one by one according to the channel ranks (from low to high) till the end of the group where it stops and generates an interrupt signal indicating the end of the regular group’s conversion process. The ADC will then “automatically” restart/re-trigger itself to start converting the regular group channels one by one again and keep repeating forever.


Reading Single Potentiometer With STM32 Example

In this LAB, our goal is to build a system that initializes the ADC with an analog input pin (channel 7). And also configure a timer module to operate in PWM mode with output on channel 1 pin (LED pin). Therefore, we can start an ADC conversion and map the result to the PWM duty cycle, 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 Channel-7 & Set it to be triggered by software. You’ll find that the analog channel has these default configurations which happens to be ok for us in this example project.

STM32 ADC Example HAL Code And CubeMX Configurations

Step #3

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

STM32 ADC Read Example HAL Code

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 Potentiometer LED Dimmer Example Code

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

Wiring

STM32 ADC DMA Example HAL Code And CubeMX Tutorial

STM32 Potentiometer LED Dimmer Testing Demo


Reading Multiple Potentiometers With STM32 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 manual mode (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) 

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 Multiple Potentiometers 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 Multiple Potentiometers 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 different configurations (ADC Modes) to read single & multiple potentiometers with STM32 microcontroller’s ADC and how to use the reading for controlling the brightness of output LEDs (LED Dimmer).

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