🛠 Lire un potentiomètre avec l'ADC 12 bits
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
- GD32F103C8T6 / CBT6 Blue Pill compatible board
- 10 kΩ potentiometer: one end to 3.3 V, the other to ground, wiper to the measurement pin
- Measurement pin: PA1 (analog input type ADC0/ADC1_IN1) >
- 3.3 V USB-serial converter on USART0 (PA9/PA10) for the console, as in the console tutorial
- Never apply more than 3.3 V to an analog input pin (VDD max 3.6 V)
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
- Value fluctuating by ±50: source impedance too high (100 kΩ potentiometer) or floating measurement ground; 10 kΩ is a good compromise, and a small 100 nF capacitor between the wiper and ground acts as a filter.
- Expecting 0-1023: the GD32 core uses 12 bits; code ported from Arduino AVR that divides by 1023 will yield millivolt values off by a factor of 4.
- Exceeding 3.3 V: powering the potentiometer with 5 V sends 5 V to the input, outside the specified 2.6-3.6 V range; risk of latch-up/damage.
- Looking for ADC2: it is absent from C4/C6/C8/CB; any ST code sampling on ADC2 will not work on your C8T6.
- ADC clock > 14 MHz: in SPL, if you configure the ADC yourself, choose an appropriate APB2 divider (the datasheet limits the ADC clock to 14 MHz), otherwise conversions will be incorrect or unstable.
- Internal temperature sensor: channel ADC_IN16 is the internal temperature sensor; do not connect it to an external pin. >
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-led.inoArduinorespiration.inoArduinopotentiometre.inoArduinopont-uart.inoArduinooled-i2c.inoArduino