🛠 Console série et piège DTR du FTDI

Arduino Nano (Classic, ATmega328P) · intermediaire · Schéma : IMG-nano-classic-intermediaire-6-nanoc-console-serie-ftdi.svg — Arduino Nano relié en USB au PC, flux série 9600, chronologie de la réinitialisation automatique par DTR qui avale les premiers octets

Objectives

Master the serial console on the classic Nano: the single hardware UART (pins 0/1) goes through the FTDI FT232RL USB-to-serial bridge on official boards — the ATmega328P chip itself has no native USB. We build a mini command console and understand the DTR pitfall: opening the port resets the board via a 100 nF capacitor, and the bootloader swallows the first bytes sent just after connection.

Hardware

Complete code

// Mini-console serie : commande 'l' = LED, 'v' = valeur analogique
const uint8_t LED = LED_BUILTIN;

void setup {
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
  // Patiente ~2 s : laisse passer le reset DTR + bootloader
  uint32_t t0 = millis;
  while (millis - t0 < 2000) { while (Serial.available) Serial.read; }
  Serial.println(F("Nano pret. Tapez l ou v."));
}

void loop {
  if (Serial.available) {
    char c = Serial.read;
    if (c == 'l') { digitalWrite(LED, !digitalRead(LED)); Serial.println(F("LED toggle")); }
    else if (c == 'v') { Serial.println(analogRead(A0)); }
    while (Serial.available) Serial.read;   // vide le reste de la ligne
  }
}

Expected verification

After opening the serial monitor at 9600 baud, the message "Nano ready" appears about 2 seconds later: this is the trace of the automatic reset. The 'l' and 'v' commands respond immediately. By sending text BEFORE the bootloader finishes, you can observe that the bytes disappear — the pitfall is observable and documented.

Common errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials