🛠 Télémétrie série robuste vers le Traceur série
Objectives
Send data from the UNO to the PC properly: pace sampling with micros/millis instead of delay, save SRAM with the F macro, and above all, understand the DTR auto-reset quirk — the UNO resets every time the serial port is opened, and its bootloader "intercepts the first bytes" sent after connection.
Hardware
- 1 Arduino UNO R3 + USB-B cable (the 16U2 handles the USB-to-serial bridge, nothing else is required)
- PC with Arduino IDE (Serial Monitor + Serial Plotter)
- Optionally another serial terminal (PuTTY, screen) to reproduce the DTR quirk.
Complete Code
// Échantillonnage non bloquant 20 Hz sur A0, format Traceur série.
const uint32_t PERIODE_US = 50000; // 50 ms = 20 Hz
uint32_t precedent = 0;
void setup {
Serial.begin(115200);
while (!Serial) { } // attend l'ouverture du port (utile sur les cartes USB natives)
Serial.println(F("pret")); // F : la chaîne vit en flash, pas en SRAM (2 Ko seulement !)
}
void loop {
uint32_t maintenant = micros;
if (maintenant - precedent >= PERIODE_US) {
precedent = maintenant;
int a0 = analogRead(A0);
int a1 = analogRead(A1);
// Le Traceur série trace une courbe par valeur séparée par tabulation :
Serial.print(a0); Serial.print("\t"); Serial.println(a1);
}
// ... ici on peut faire autre chose : le delay ne bloque plus la boucle.
}
Expected Verification
Tools > Serial Plotter: two continuous and regular curves, without gaps. If you open the Serial Monitor, the message "ready" appears (the board has just restarted — that's the DTR!). Test with a raw terminal: the first 2–3 characters emitted just after opening may be lost, and the first bytes YOU send may be swallowed by the bootloader.
Common Errors
- Believing that the first characters are lost "because of Arduino": it is the DTR reset + bootloader window, documented; on the PC side, open the port then wait ~2 s before sending data.
- Printing literals without F: with 2 KB of SRAM, a few Serial.print("long string") are enough to fragment memory and crash the board.
- delay between each measurement: the timing becomes inaccurate as soon as another task arrives; hence micros.
- Confusing Serial Monitor (text) and Serial Plotter (curves): without a tab between values, the Plotter displays only one curve.
- 9600 bauds for 20 Hz multi-channel data: switch to 115200, the 16U2 handles it without problem.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-poussoir.inoArduinopwm-fade.inoArduinocapteur-lumiere-ldr.inoArduinoservo-balayage.inoArduinoruban-neopixel.inoArduino