🛠 Console série sur le ST-LINK VCP : printf, ADC 12 bits et piège du USB
Goals
Master the serial console on the Nucleo-F401RE: the ST-LINK/V2-1 exposes a virtual COM port, and the Arduino core wires the default Serial object to USART2 (RX = PA3, TX = PA2) through it. You will build a small measurement logger that prints formatted lines, and learn to scale the F401's 12-bit ADC correctly.
Hardware
- 1× Nucleo-F401RE + data micro-USB cable
- Optional: 10 kΩ potentiometer between 3.3 V, GND and A0 (PA0) so there is something to log
The wiring truth
The board's micro-USB socket goes to the ST-LINK, not to the MCU's USB OTG peripheral. That is a feature for beginners: the COM port is always there, even if your sketch never touches USB. The MCU's own OTG FS pins are broken out on the morpho headers for advanced USB device work (see the advanced USB OTG tutorial).
Code
const int kProbePin = A0; // PA0 on the Arduino connector
void setup {
Serial.begin(115200); // ST-LINK VCP, no native USB needed
while (!Serial && millis < 2000) {} // brief wait for the VCP to settle
Serial.println("Nucleo-F401RE serial log up");
}
void loop {
int raw = analogRead(kProbePin); // 12-bit ADC, 16 channels (F401RE)
Serial.printf("ADC raw=%d mV=%lu\n", raw,
(unsigned long)(raw * 3300 / 4095));
delay(250);
}
Steps
- Upload the sketch with board Nucleo F401RE selected.
- Open the serial monitor at 115200 baud on the ST-LINK COM port.
- Turn the potentiometer: the millivolt column should sweep 0 → 3300 mV.
- Unplug and replug the cable: the log restarts — the VCP re-enumerates with the board.
Expected result
Clean ADC raw=... mV=... lines at 4 Hz. Because the ADC is 12-bit (0–4095 over 0–3.3 V), the conversion divides by 4095, not 1023 — a classic leftover from 10-bit AVR tutorials. Serial.printf works out of the box on the STM32 core, no TinyPrintf gymnastics needed.
Common mistakes
- Opening the mass-storage drive or the debugger interface instead of the COM port — the ST-LINK enumerates as debugger + VCP + MSD at once.
- Expecting USB-CDC:
Serialhere is USART2 on PA2/PA3; the OTG peripheral is a separate, advanced topic. - Forgetting the 12-bit scale and hardcoding 1023 — your millivolts top out at a quarter of the range.
- Injecting a 5 V signal into A0: the MCU domain is 3.3 V (VDD range 1.7–3.6 V) — use a resistor divider.
Other tutorials for this board
- Prise en main de la Nucleo-F401RE : ST-LINK, LED LD2 et alimentation
- Premier Blink Arduino sur la Nucleo-F401RE avec le core STM32duino
- STM32CubeF4 et HAL : blink + UART printf générés pour la Nucleo-F401RE
- ADC 12 bits et PWM sur la Nucleo-F401RE : oversampling, millivolts et LED en fondu
- Zephyr RTOS sur la Nucleo-F401RE : west build, devicetree et west flash
- Bare-metal sur le STM32F401 : clignoter LD2 en écrivant les registres GPIO