Save Serial data to a text file – Arduino, Processing, PuTTY

Here shows the method to save the serial data displayed on a serial monitor to a text file. This method is useful to save and use data as logs, records, etc.

Arduino Serial data


Here the serial data is provided by the Arduino. The below code can be used for testing, it just prints numbers from 0 to 10 as serial data at a baud rate of 9600. This data could be a sensor reading, measurements, output values, etc consisting of numbers or characters.

Arduino Code

int number = 0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  //Print numbers from 1 to 10.
  while (number < 10) {
    number += 1; //Add by one
    Serial.println(number);
    delay(1000);
  }
}

Save data to txt file using PuTTY

PUTTY is a free and open-source SSH and telnet client software which supports variety of network protocols and connection types, including connection to a serial port. PuTTY has an inbuilt feature to save session logging, hence using a serial connection type the serial data can be displayed on the terminal console of the PuTTY and at the same time the it save the  serial data logs to a text file.

Steps

PuTTY User manual: https://the.earth.li/~sgtatham/putty/0.75/htmldoc/

Refer PuTTY logging: https://the.earth.li/~sgtatham/putty/0.75/htmldoc/Chapter3.html#using-logging

  • Select appropriate Serial PORT (COM1, COM2, COM3…). Here the baud rate of the serial communication is set as 9600 in the Arduino code, so set the exact baud rate at the PuTTY also.

  • Under Logging select the location of the file to be saved.

It also has a feature to add current Year, Month, Date, Time, etc with the file name of the generated file which helps to identify or sort the files.

Also, on next serial communication the previous data can be overwrite or append the new data with the existing data. Either of the option can be selected or can be set to ask always.

Save data to text file using Processing

Processing gives more freedom to manipulate or process the serial data. It can append prefix or suffix text with the serial data, use additional data, use mathematical functions, etc. The file name with time, date, etc and the data can be prefixed with time, data parameters on each readings.

Download Processing : https://processing.org/download/

Refer : Send Data from Processing to Arduino

The serial data is saved to a file using the “savestring()” function of the Processing.

Processing code

import processing.serial.*;

Serial COMPort;  // Create object from Serial class

String[] lines = new String[0];

void setup() 
{
  frameRate(5);
  String portName = Serial.list()[0];
  COMPort = new Serial(this, portName, 9600);
}

void draw() 
{
  if (COMPort.available() > 0) {  // If data is available,
    String read = COMPort.readString();  // read and store it to string read
    read = "value : " + read;
    println(read);
    lines = append(lines, read);// append new read to string lines
  } else {
    saveStrings("serial_data.txt", lines);//save string to file
  }
}

Resultant file “serial_data.txt”.

serial_data.txt

See also :

2 Responses

  1. Mike says:

    Suppose you use Arduino with a sensor and Putty to create a txt file, which contains sensor data. Subsequently, you wish to play back the txt file over the Arduino serial monitor or plotter. To do that, you need first to copy the txt file from the directory in which it is saved on your computer to an SD card since the Arduino Dump file only reads from SD card as far as I can determine. Is there a modification of Arduino Dump file that can be used for txt files on computer hard drive?

Leave a Reply

Your email address will not be published. Required fields are marked *