Arduino Basics: Reading a text or CSV file using the Processing language

23 June 2011

Reading a text or CSV file using the Processing language

In a previous post, I showed you how to export data to a text file. Now I will show you how to import it back into your Processing program. This will come in handy later on.

This is what my data looks like in the text file:

There are many ways to import text from a text file, and there are many ways to store the data within your code after you have imported it. Feel free to make mention of your method in the comments, however, this one definitely works, and is doing what I want it to do.

I have decided to store each line within an ArrayList.
The first column is stored in another ArrayList by splitting the data into bits using splitTokens.
Most of the file reading code was taken from this Java site, and seems to work quite fine with the Processing programming language. I have taken bits and pieces of code from various places, and added my own flavour.

Here is a snippet of the data after import.

You can see that I have separated the two ArrayLists using dots "....."

Processing Code:
Project: Read data from Text file into Processing
Author: ScottC
Created: 23rd Jun 2011 (Updated 26th Aug 2014)
Description: Use processing to read a text file and populate an ArrayList.
             The ArrayList is then printed to the debug window.
Processing version tested: 2.2.1
References: This was made possible using bits and pieces from these sites
===========================================================  */

ArrayList sensorData;
ArrayList columnOne;

void setup(){
  sensorData=new ArrayList();
  columnOne=new ArrayList();

void readData(String myFileName){
  File file=new File(myFileName);
  BufferedReader br=null;
    br=new BufferedReader(new FileReader(file));
    String text=null;
      String [] subtext = splitTokens(text,",");
  }catch(FileNotFoundException e){
  }catch(IOException e){
    try {
      if (br != null){
    } catch (IOException e) {
  for (int i=0; i<sensorData.size()-1; i++){
    print(columnOne.get(i) + ".....");

 Processing Code Explained

void setup(): this creates 2 new ArrayLists to hold the data, and then calls the readData function. The readData() function needs the name of the file that you want to analyse, eg. C:/mySensorData.txt

br=new BufferedReader(new FileReader(file));
This just sets up the file that you will be reading, which then allows you to use a "while-loop" to read one line at a time using br.readLine().

String [] subtext = splitTokens(text,",");
This splits each line into bits, using a "," as a separator, and puts it into the subtext array.

This shows how you can extract the first number or column from each line.
If you wanted the second number or column, you would use subtext[1] instead.

This shows how you can extract the ENTIRE line.

  }catch(FileNotFoundException e){
  }catch(IOException e){

This is just error handling related to file reading.

This closes the file.

for (int i=0; i<sensorData.size()-1; i++){
    print(columnOne.get(i) + ".....");

This just prints the data to your screen so that you can see if the whole process has been successful.

Update : If you want to read values from a text file and then send these values to the Arduino - read this blog entry.


  1. Is it possible to read live data from a text file?
    I want to read a text file that autoupdates. It has numbers inside, but I want to convert those to variables and send them over to Arduino. Then the Arduino would use those variables.
    Thank you!


  2. Good question Razvan: I cannot see why not. But not sure if it is the best way to do it. Perhaps you could get the processing script to grab the data from the original source rather than double handling. But once the Processing script has the variables, you can send these to the Arduino via a serial command, and you should be able to get the Arduino to respond appropriately.

  3. Nice post!
    I am a beginner in processing. I am trying to read a text file from SD card on Arduino. The space separated text file has 4 fields (i.e. 4 columns) of sensor data. I am trying to compare the data with a live sensor connected to arduino.

    Well, if you can please explain the post a little bit more.
    (You can do that by commenting each line of the code).

  4. Hi, could you tell me how to convert sensor.Data.get(i) to integer, thx

  5. Sarvesh:
    I would encourage you to read the websites provided:

    and then come back and ask about the part of the code you don't understand.

  6. This is one way to convert a string to an int:

    String myString = "16";
    int myInt = int(myString);


    int myInt = Integer.parseInt(myString);

    Please note that sensorData.get(i) is not a number, it is a set of numbers separated by commas.
    If you want the individual numbers, then you will have to set up more ArrayLists (eg. ColumnTwo, ColumnThree etc etc), and grab them in the same way that I did for the ColumnOne Arraylist. See the "Processing Code Explained" section above.

  7. There's no library to use FILE?

  8. I am assuming that you are talking about the processing language ?

  9. so once you are able to read the array through processing .. u can send it to arduino??? by any chance do you happen to have an example for that one

    1. Have a look at this post:

  10. I'm trying to get the arduino to read a single number inside a txt file that is constantly updated. Do you know the code for doing this.

    1. Dear Anonymous,
      Have you tried asking in the Arduino forums?

    2. Have a look at this post:

  11. Hello I'm trying to send a text file from my computer to Arduino UNO using Arduino software and also send that text file from Arduino UNO to another Arduino UNO through wireless using two Xbee S2. Can you tell me how to do it. can I do it using Arduino Software or I have to use Processing Language.

    My text file contains three columns, and three headings for each and the data under the headings is almost 7500 values for each column.

    Can you please help me how to code it.

    1. I only know how to do this using the Processing Language. But based on what you describe, it should be possible to do what you want - using the Processing language.
      You don't have to use Processing, you could use a Java, VB.NET, Python (choose a language of your choice) program to send the information to Arduino using a Serial Interface.
      Once your 1st Arduino gets the information, it should not be hard to transmit it to the 2nd Arduino using an XBee.

      See the following post for some tips on how to send a text file to an Arduino:

      Arduino Forums are also a useful way of getting help with coding etc.

      Much quicker than I could ever do :)

    2. Thanks a lot for the response, I wanted to know if the python is executable in Arduino software, else I have to use Python 2.7 software to run it?

      I can use Processing language as well if others are time taking, I can be adaptable to any language. So can you please suggest me which is good to go to start away my project.

      Thanks for the help :)

    3. You would run the Python/JAVA/C++/VB.NET/Processing program on your computer. The USB cable attached to the Arduino will be used as a virtual serial port. Your executable on your PC will communicate and transfer information from your hard drive to the Arduino via the USB cable.

      The Arduino can be programmed to perform various tasks using the data being sent from the computer. To do this, you will need to use the Arduino IDE.

      I have provided you with a link that will get you started on the right track (see previous comment). So I would suggest a Processing/Arduino software combination. But you can use any other programming language on your computer. However you should use the Arduino IDE to program the Arduino.

      The Processing language is well documented and is very similar to C++ and Java - the Processing IDE can be downloaded for free from :

      The Arduino IDE can be found here:

      Like I said before, the Arduino Forums are very useful for getting the answers you need.

    4. I have actually used Arduino to communicate with othr Arduino using the Arduino IDE. and I also have installed Processing IDE. I'm unable to link between these two, i mean I copied the Arduino Libraries to Processing libraries but still I'm unable to use Processing IDE to communicate with my Arduino

    5. How to I make processing IDE to communicate with my Arduino? Can you please tell me.

    6. Processing runs on your computer. The Arduino Program runs on the Arduino.
      You cannot run processing on your Arduino.
      Processing runs on your PC, communicates with Arduino through USB.
      Follow the example I provided in the link. It will show that you use Serial commands to communicate with the Arduino.

    7. the text file which I want to send is 200KB. Can Arduino store this size? or else Shall I use the SD card for sending the data?

    8. It depends on the type of Arduino you have:
      Arduino Uno:
      Arduino Leonardo:
      Arduino Mega 2560:
      Arduino Mega ADK:

      Unless you have an Arduino Mega, you will need to use an SD card to store the data. However, it depends on what you are trying to do with the data. What will the Arduino do with the data after receiving it?

    9. It will just send it to the computer. I'm using Arduino just for wireless transfer. I reduced the file to 105KB but still im not able to figure it out how to store the data into the Arduino and also how to send from one Arduino to another Arduino.

      I'm using Arduino Uno board with Xbee Series 2 for both the Arduino.

      Can you please help me.

    10. Dear Anonymous,

      1. I don't think you need to send all of the data at once. Send one line or one value at a time, and pass it on. You can easily use an array, or a variable to do this. No need to use valuable Arduino memory for this task.

      2. You are not trying to send whole numbers, you are trying to send values like: 0.016, -0.305, -0.195, And while it is possible to transfer this data through an Arduino, it will take a bit more effort than the examples I have provided so far.

      3. I would recommend that if you have a value of 0.4, that you store this value as 0.400. This will make it much easier to write the code necessary to transfer the data.

      4. I would also recommend that you delimit the values with a comma or some other type of separator. And perhaps include a line delimiter such as a * or some other symbol at the end of each line.

      5. It looks like you are trying to jump into more advanced stuff, without learning the basics. Rather than trying to get someone to do all the hard work for you, maybe try and learn the simple stuff first, and once you understand how it works, you will then be able to put it all together.

      6. Yes - I can help you. But I am taking the long road. Feel free to join me - I will post a link soon to a progressive tutorial that will hopefully meet your needs - and if you take the time to follow through the examples I provide you, it will all become clear (I hope).

    11. Dear Anonymous:

      You can start here:

    12. Dear ScottC,

      I'm ready to learn, I was asking for the basic examples to start with and yes I got it how to write the program using array. I'm looking forward for the examples so that I can write my own code. Please try to provide me some examples even I'll find out meanwhile.


    13. Does this require Processing IDE?? or only Arduino will do?

    14. I don't know how to get the Arduino to read a computer's hard drive. However, I do know how to get a program that is running on your computer to read text files (as per comments above). Therefore I would use a Processing script to read the text file. Processing is also capable of communicating with the Arduino through the serial port. This will become clear when you go through the progressive tutorial which can be found here:

      I haven't got to the bit where the Processing is used, but please be patient, I will get there. We need to learn to walk before we can run.

  12. Hi in my datalogger i used the string datasring and had my values in csv format in excel. the problem is that it only increment to 7 or 10 data. and it will restart. I know there's something wrong with my program. So im planning to use this code. I also wish that my logger will only count if theres someone who pass that sensor. I use digital output. When high it is one when not low and i don't want to log all the 0(low) data in my logger.


    1. Hi Iggy,

      Not sure if this is a question or a statement. Please clarify if this is a question??

  13. Hi there

    I am new to arduino, is there any way to do the same thing above but using the ardunio wifi shield V2.1? how would i communicate between the two using processing?

    1. Hi Anonymous,

      I don't have a Wifi shield, so am not sure how you would do it. I would ask the Arduino/Processing forums, they are much better at answering these kinds of questions.

      I would start by trying to communicate with one Arduino via wifi.
      I assume that this could be done via a Serial command???

  14. Thank you for your quick response , yes i do have it communicating using putty just trying to figure out how to use it with processing.

    Thanks again i will try asking them

  15. I have a Excel file with 3 columns(Column A,B and C).. I wanna read the excel file in arduino Uno board and find maximum of each column (say MaxA for Column A , MaxB for Column B and MaxC for Column C)..


    1. Why not find the maximum in excel, and save the maximums to a file that the Arduino can read?

  16. Hello, I am getting a new window but it is empty and there is nothing coming in it. Program is showing no errors, no warnings. I don't know why it is doing like this. I am reading a text file which is having "help me" text stored.

  17. Maybe with createReader is easier...

    1. Processing has been updated since I wrote this tutorial - createReader is worth looking at.

  18. Hello, i'm not sure, that you are still here, but i have few questions about the csv file.
    How can i read csv file on arduino.


    1. That would be a whole new tutorial.
      Never done it - so would have to do my research.

  19. hello,
    i have 0 to 255 integer with 9 bits string in text file format.
    e.g. 0 010010011
    1 100100100
    now i want to load 9 randomly integer's strings in arduino through processing so how to give input manually in processing and how can i call the string from text files?

    1. I would load all the numbers into an Array in Processing, Then use a random function to choose the number and send to Arduino.
      If these numbers are in a CSV file, you would read the file like it is done above.

    2. how can i call the file from PC. It is in text format and how can i check it is going to read or not in processing.

    3. This may be more relevant in the current version of Processing:

  20. in this project it read numbers column wise in text can i read that no. row wise??

    1. Use loadStrings(textfile.txt) to load the text file into a String array. It will load each line into the array. You can then chop that up and arrange it in any way you choose.
      split() or splitTokens() is a really good choice for this kind of stuff.


Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked on my Arduino Tutorials Discord Server.

Comments are moderated due to large amount of spam.