33
\$\begingroup\$

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

\$\endgroup\$
29
\$\begingroup\$

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

Serial.begin(9600);

and write your sensor values to the serial interface using

Serial.println(value);

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
}
\$\endgroup\$
  • \$\begingroup\$ Where should the "processing side" code go - in the same arduino code or a separate c script? \$\endgroup\$ – Ulad Kasach Apr 16 '17 at 17:38
  • 1
    \$\begingroup\$ In a separate processing script - processing isn't compiled to C but to java \$\endgroup\$ – Nikolaus Gradwohl Apr 17 '17 at 9:44
  • \$\begingroup\$ @UladKasach "Processing" is a multi-platform programming environment. In fact, it's the programming environment with the world's stupidest name. \$\endgroup\$ – Scott Seidman Jun 26 '18 at 15:00
9
\$\begingroup\$

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.

\$\endgroup\$
6
\$\begingroup\$

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

\$\endgroup\$
5
\$\begingroup\$

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.

Melanie

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – garageàtrois Nov 16 '11 at 19:59
3
\$\begingroup\$

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
    print(line);
    output_file.write(line);
\$\endgroup\$
1
\$\begingroup\$

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(datetime.now()), 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 " % (datetime.now())])
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

protected by Kortuk Jan 12 '13 at 2:59

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.