Saving Arduino sensor data to a text file

  • How can I save data retrieved from a sensor to a text file on the computer?

  • you can write the sensor data to the serial port using serial-lib and write a small processing program that reads the data from the serial port and writes it to a file.

    in the arduino code initalize the serial lib in the setup method


    and write your sensor values to the serial interface using


    in your loop method

    on the processing side use a PrintWriter to write the data read from the serial port to a file

    import processing.serial.*;
    Serial mySerial;
    PrintWriter output;
    void setup() {
       mySerial = new Serial( this, Serial.list()[0], 9600 );
       output = createWriter( "data.txt" );
    void draw() {
        if (mySerial.available() > 0 ) {
             String value = mySerial.readString();
             if ( value != null ) {
                  output.println( value );
    void keyPressed() {
        output.flush();  // Writes the remaining data to the file
        output.close();  // Finishes the file
        exit();  // Stops the program

    Where should the "processing side" code go - in the same arduino code or a separate c script?

    In a separate processing script - processing isn't compiled to C but to java

    @UladKasach "Processing" is a multi-platform programming environment. In fact, it's the programming environment with the world's stupidest name.

  • Yet another option is to use an SD Card reader / writer, and write your file to a SD card. When you're done collecting data, swap out SD cards with your workstation computer. This approach will allow you to run your project disconnected from a computer, and will provide for non-volatile storage of large amounts of data.

  • The program gobetwino will log sensor values from an Arduino to a text file or spreadsheet with minimal effort. It can also automate things on the computer, add timestamps (so you don't need to program them into the arduino), etc.

    alt text

  • The simplest method is to use the Serial library and output to that. You can then capture the output to a text file using a terminal program. Hyperterminal is available on Windows, Teraterm on Linux and Z Term on OS X.


  • If you want to write sensor data directly to a file on your computer without having to copy and paste the output form the serial monitor window, then try reading the data stream directly from the serial port (which is what the serial monitor does anyway, I suspect). On mac/linux do something like:

    cat /dev/cu.usbmodem1d11 

    God knows what's the equivalent on windows machines.

    You'd probably also want to associate timestamps with each sensor reading, in which case you'll probably need to replace that `cat` command with some sort of script.

  • You can create a python script to read the serial port and write the results into a text file:

    ## Script listens to serial port and writes contents into a file
    ## requires pySerial to be installed 
    import serial
    serial_port = '/dev/ttyACM0';
    baud_rate = 9600; #In arduino, Serial.begin(baud_rate)
    write_to_file_path = "output.txt";
    output_file = open(write_to_file_path, "w+");
    ser = serial.Serial(serial_port, baud_rate)
    while True:
        line = ser.readline();
        line = line.decode("utf-8") #ser.readline returns a binary, convert to string
  • I found easier and safer to use a Python script. I usually use this one based on the serial library. It's very common to use also the datetime module to add the timestamps:

    import serial
    from datetime import datetime
    sensor = "DH11"
    serial_port = '/dev/ttyACM0'
    baud_rate = 9600
    path = "%s_LOG_%s.txt" % (str(, sensor)
    ser = serial.Serial(serial_port, baud_rate)
    with open(path, 'w+') as f:
        while True:
            line = ser.readline()
            f.writelines([line.strip(), " t = %s \n " % (])
  • The next step is to use either the ethernet shield or something like a WIZ810MJ board and internet enable your arduino. You can then write data to that and send it to say a web server to collate it. I do this in my vivarium temperature monitor.

License under CC-BY-SA with attribution

Content dated before 6/26/2020 9:53 AM