Nucleo-F401RE: read a potentiometer
Read a potentiometer on A0 with ADC1, set up in registers: 12-bit conversions on channel 0, printed in millivolts.
Sensors · Beginner
In a real product: Volume knob (Consumer electronics): The knob's position is read as a voltage and turned into a setting.
A potentiometer is a voltage divider: its wiper (SIG) gives anything from 0 V to the voltage across it. Here that is 3.3 V, the most the STM32's ADC can read. The F401's ADC has 12 bits, so it gives 4096 steps from 0 to 3.3 V.
The wiper goes to A0, which is PA0 on the chip and channel 0 of ADC1.
What you'll learn
- Putting a pin in analog mode (
MODER = 11), which disconnects its digital input - Choosing the channel in
ADC1->SQR3and its sampling time inADC1->SMPR2 - Starting a conversion with
SWSTART, waiting forEOCand readingDR
How it works
ADON powers the ADC up. Each conversion then starts with SWSTART in CR2, samples channel 0 for 84 ADC clock cycles (SMP0 = 100), and converts. When it is done the ADC sets EOC in SR, and reading DR gives the 12-bit result and clears the flag. The reading becomes millivolts as reading × 3300 / 4095.
Every 200 ms the program prints the reading and the voltage. In between, it blinks LD2 with a period set by the knob, from 50 ms to about 1 s, by comparing ticks with the last change rather than waiting, so readings keep coming.
The ADC's clock is APB2 divided by 2 (the reset value of ADC->CCR): 8 MHz, well under its 36 MHz limit.
Try this
- Turn the knob all the way each way and check you see 0 and 4095.
- Set
RESinCR1to 10 bits and see the range drop to 0-1023. - Average eight readings before printing and see whether the value settles.
main.c
// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// A potentiometer on A0 (PA0, ADC1 channel 0): print the 12-bit reading and its voltage; blink LD2 at a rate it sets.
#include <stdio.h>
#include "stm32f4xx.h"
static volatile uint32_t ticks; // milliseconds since SysTick started
void SysTick_Handler(void) {
ticks++;
}
int __io_putchar(int ch) {
while (!(USART2->SR & USART_SR_TXE)) {}
USART2->DR = ch;
return ch;
}
static uint32_t read_adc(void) {
ADC1->CR2 |= ADC_CR2_SWSTART;
while (!(ADC1->SR & ADC_SR_EOC)) {}
return ADC1->DR; // reading DR clears EOC
}
int main(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
// PA0 analog (11), PA5 output (LD2), PA2 alternate function (USART2 TX).
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER0 | GPIO_MODER_MODER5 | GPIO_MODER_MODER2))
| GPIO_MODER_MODER0 | GPIO_MODER_MODER5_0 | GPIO_MODER_MODER2_1;
GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8);
USART2->BRR = SystemCoreClock / 115200; // 16 MHz HSI after reset
USART2->CR1 = USART_CR1_TE | USART_CR1_UE;
ADC1->SQR3 = 0; // the one conversion: channel 0
ADC1->SMPR2 = (ADC1->SMPR2 & ~ADC_SMPR2_SMP0) | ADC_SMPR2_SMP0_2; // 84 cycles of sampling
ADC1->CR2 |= ADC_CR2_ADON;
SysTick->LOAD = SystemCoreClock / 1000 - 1; // 1 ms
SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
uint32_t lastPrint = 0, lastBlink = 0;
while (1) {
uint32_t reading = read_adc();
uint32_t now = ticks;
if (now - lastPrint >= 200) {
lastPrint = now;
printf("A0: %lu %lu mV\r\n", (unsigned long)reading, (unsigned long)(reading * 3300 / 4095));
}
if (now - lastBlink >= 50 + reading / 4) {
lastBlink = now;
GPIOA->ODR ^= GPIO_ODR_OD5;
}
}
}