How to collect Arduino serial print data?

 Here are the most practical ways to collect (log) Arduino Serial.print() data—from quick manual capture to fully automated logging.



1) Arduino IDE Serial Monitor (quick + manual)

  • Open Tools → Serial Monitor

  • Select the correct baud rate

  • You can copy/paste the text out.

  • IDE 2.x also has a Serial Plotter (good for quick graphs, not great for saving clean logs).

Best for: quick debugging.

2) Use a terminal program and log to a file (easy + reliable)

Windows

PuTTY

  1. Find the COM port (Device Manager)

  2. In PuTTY: Connection type = Serial, set COMx and baud

  3. Enable logging: Session → Logging → “All session output” → choose a log file

  4. Open the session → your serial data is saved.

Tera Term

  • File → Log… to save the session output.

macOS / Linux

Use screen to view, and tee to save:

# replace /dev/ttyACM0 or /dev/ttyUSB0 with your device, and 115200 with your baud screen /dev/ttyACM0 115200

For logging, screen alone isn’t great; better use pyserial (below) or:

cat /dev/ttyACM0 | tee serial_log.txt

(May need to configure line settings; pyserial is usually cleaner.)

Best for: simple logging without writing code.

3) Python logger (best overall: clean, automated, CSV-ready)

Install:

pip install pyserial

Minimal logger:

import serial, time PORT = "COM6" # Windows example; macOS/Linux: "/dev/ttyACM0" BAUD = 115200 OUT = "arduino_log.txt" ser = serial.Serial(PORT, BAUD, timeout=1) time.sleep(2) # wait for Arduino reset on serial open with open(OUT, "a", encoding="utf-8") as f: while True: line = ser.readline().decode(errors="ignore").strip() if line: f.write(line + "\n") f.flush() print(line)

If you print CSV from Arduino like:

Serial.print(millis()); Serial.print(","); Serial.println(sensorValue);

…then you can log to .csv and open in Excel.

Best for: long runs, automation, timestamps, CSV.

4) Add timestamps on the PC side (recommended)

Even if Arduino prints plain values, you can add your own timestamps in Python:

  • log format: PC_time, arduino_line

5) If you need high-rate data (avoid losing samples)

  • Increase baud (e.g., 230400 or 921600 if stable)

  • Print fewer characters (binary or compact CSV)

  • Buffer on Arduino (ring buffer) and send in chunks

  • Avoid delay() and heavy String usage


Common “why is my log missing data?” causes

  • Wrong COM port / wrong baud

  • Arduino resets when the serial port opens (normal on many boards)

  • Printing too fast for the baud rate

  • USB power/connection issues

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How do you set up ADC (Analog-to-Digital Converter) in STM32?