🛠 Console série sur le ST-LINK VCP : printf, ADC 12 bits et piège du USB

STM32 Nucleo-64 F4 family — flagship: Nucleo-F401RE · debutant · Schéma : IMG-stm32f4-nucleo-debutant-3-stm32f4nuc-console-serie-stlink-vcp.svg — Nucleo-F401RE avec un potentiomètre sur A0, flèches vers une fenêtre de moniteur série affichant des lignes ADC en millivolts

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

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

  1. Upload the sketch with board Nucleo F401RE selected.
  2. Open the serial monitor at 115200 baud on the ST-LINK COM port.
  3. Turn the potentiometer: the millivolt column should sweep 0 → 3300 mV.
  4. 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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials