9

I have some position data continually coming in and I am currently printing it to the serial.

Say I have the string "5" and want to print that to a text file, "myTextFile", what would I need to do to achieve this? To be clear, the text file would be saved on my computer not on an SD card on the Arduino.

Also, is their a way to create a text file within the program before I start saving to it?

4
  • So you have some SD card connected to the Arduino and you want to create and write file onto it? Commented Nov 8, 2016 at 7:16
  • I want to write the file onto the computer that the arduino is connected to. Going to make that more clear in question now, thanks. Commented Nov 8, 2016 at 7:23
  • You can use Putty, to connect Arduino and configure it to write output. Otherwise your own computer side software is required. Commented Nov 8, 2016 at 7:32
  • Windows or Mac/Unix? Commented Nov 8, 2016 at 7:43

2 Answers 2

6

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  # sudo pip install pyserial should work

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);
Sign up to request clarification or add additional context in comments.

1 Comment

perfect answer. just had to change serial_port to mine on mac /dev/cu.usb____
0

U have to Use serial-lib for this

Serial.begin(9600);

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
}

2 Comments

This is the answer I am after however it is very unclear. Can anybody clarify how to set this up and explain what is happening in these functions?
how do you even do import on Arduino?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.