🛠 Lire un potentiomètre avec l'ADC 12 bits

GD32F103 (GigaDevice) — chip + Blue-Pill-compatible boards · intermediaire · Schéma : IMG-gd32f103-intermediaire-2-adc-potentiometre.svg — potentiomètre entre 3,3 V et masse, curseur relié à PA1, conversion ADC 12 bits du GD32F103C8 affichée sur la console série

Objectives

Configure and read an analog input: the GD32F103C8 features 12-bit SAR ADCs up to 1 MSPS (note: ADC2 is absent from C4/C6/C8/CB variants) and the ADC clock must never exceed 14 MHz. With the CommunityGD32Cores core, analogRead natively returns a 12-bit value (0-4095), which is more precise than the 10-bit resolution of a classic Arduino. You will convert the position of a potentiometer into millivolts.

Hardware

Complete Code

// adc_pot.ino — GD32F103C8, core CommunityGD32Cores
const uint8_t POT_PIN = PA1;    // entrée analogique (canal IN1) > 

void setup {
  Serial.begin(115200);
  pinMode(POT_PIN, INPUT);
}

void loop {
  uint16_t brut = analogRead(POT_PIN);        // 12 bits : 0..4095
  uint16_t mv   = (uint32_t)brut * 3300UL / 4096UL;
  Serial.print("ADC = ");
  Serial.print(brut);
  Serial.print("  ->  ");
  Serial.print(mv);
  Serial.println(" mV");
  delay(100);
}

Expected Verification

When turning the potentiometer, the value follows the position from approximately 0 (wiper on the ground side) to 4095 / approximately 3300 mV (wiper on the 3.3 V side). At rest, with a finger on the wiper, the value may fluctuate: this is normal, as an unconnected input picks up noise. The conversion becomes stable when you release the knob.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials