const float Vref = 3.3; // STM32 ADC reference voltage
// Voltage divider resistors (Ohms)
const float R1 = 2000.0; // Top resistor
const float R2 = 1000.0; // Bottom resistor
void setup() {
Serial.begin(115200);
analogReadResolution(12); // STM32 = 12-bit ADC
pinMode(PA0, INPUT_ANALOG);
}
void loop() {
int rawADC = analogRead(PA0);
// Convert ADC reading to voltage at pin
float Vout = (rawADC * Vref) / 4095.0;
// Calculate original input voltage
float Vin = Vout * (R1 + R2) / R2;
Serial.print("ADC: ");
Serial.print(rawADC);
Serial.print(" | Vout: ");
Serial.print(Vout, 3);
Serial.print(" V | Vin: ");
Serial.print(Vin, 3);
Serial.println(" V");
delay(500);
}