A custom Python script was developed to record ECG and GSR data streamed from the Arduino via the serial interface. The Arduino transmits raw sensor values as comma-separated integers (ECG, GSR) at a fixed baud rate. On the computer side, the Python script establishes a serial connection, continuously reads incoming data, and stores it in a structured CSV file together with precise timing information.
Serial connection and configuration
PORT = “/dev/tty.usbmodem1101”
BAUD = 115200
ser = serial.Serial(PORT, BAUD)
This section defines the serial port and baud rate used by the Arduino. The baud rate must match the value specified in the Arduino sketch to ensure correct data transmission.
Automatic file creation and session-based storage
start_stamp = datetime.now().strftime(“%Y%m%d_%H%M%S”)
csv_filename = f”{start_stamp}_ecg_gsr.csv”
Each recording session generates a new CSV file whose name includes a timestamp. This prevents accidental overwriting and allows recordings to be clearly associated with specific experimental sessions.
CSV structure and timing
writer.writerow([“timestamp”, “time_ms”, “ECG”, “GSR”])
start_time = time.time()
The CSV file contains both an absolute timestamp and a relative time counter in milliseconds. This dual timing system supports synchronization with experimental events while also enabling precise signal processing.
Parsing and writing incoming data
line = ser.readline().decode(errors=”ignore”).strip()
ecg_str, gsr_str = line.split(“,”)
ecg = int(ecg_str)
gsr = int(gsr_str)
Each line received from the serial port is expected to contain two comma-separated values. Basic validation ensures that malformed or incomplete lines are ignored.
Writing samples to CSV
time_ms = int((time.time() – start_time) * 1000)
timestamp = datetime.now().strftime(“%Y-%m-%d %H:%M:%S.%f”)[:-3]
writer.writerow([timestamp, time_ms, ecg, gsr])
For each valid sample, the script writes one row containing the current timestamp, elapsed time since the start of recording, and raw ECG and GSR values. Data is flushed to disk continuously to prevent loss during longer sessions.