Send large data from Processing to Arduino via serial port communications

Hi, all,

I am planning to use Arduino Uno to control one LED based on the binary sequence (100011100…) stored in one text file. I have written one Processing code to read and pass the data to the arduino board via serial communication port.

For a short binary sequence length, e.g. 10 or 20 , I can get exactly the output signal from the arduino board’s digital pin. However, once the length increases to more than 64, it seems only the first 64 bit data can be passed via the serial port.

I am aware that the input serial port buffer is 64 byte, so I use a if-else statement in the Processing code to pass only 64 binary data to the serial port, then clear the buffer, then pass another 64 binary data. However, it will not change the output to the one I expect.

One strange thing is that, if I change the baud rate to a low value (e.g. 600) from the standard one (9,600 ), I can get the output signal that is even longer than expected from the arduino board. In practice, I need to send a binary sequence with a length of ~1 million. Is there any efficient way of solving this problem? Or do you have any other recommendations on hardware part which can handle such large data?

I have attached both the Processing and Arduino code, any suggestion will be appreciated. Many thanks in advance.


read_data_100bit.pde (2.04 KB)

test.ino (936 Bytes)

100bit.txt (199 Bytes)

import processing.serial.*;
int mySwitch=0;
int counter=0;
String [] subtext;
Serial myPort;
//boolean firstContact = false;

void setup(){
 //Create a switch that will control the frequency of text file reads.
 //When mySwitch=1, the program is setup to read the text file.
 //This is turned off when mySwitch = 0
 //Open the serial port for communication with the Arduino
 //Make sure the COM port is correct
 myPort = new Serial(this, "COM11", 600);
 background(255,0,0);  //Change background to red

void draw() {
 if (mousePressed == true)
   if (mySwitch>0)
   /* Rread data from txt or csv file */
   println("Length of the binary sequence is ",subtext.length);
   /*The following switch prevents continuous reading of the text file, until we are ready to read the file again. */
  /* Send new data. This for loop will allow new data to be sent to the arduino. */
   for (counter=0;counter<subtext.length;counter++)
     if (counter>0 && counter%64==0)
 else {background(255,0,0);

/* The following function will read from a CSV or TXT file */
void readData(String myFileName){
 File file=new File(myFileName);
 BufferedReader br=null;
 br=new BufferedReader(new FileReader(file));
 String text=null;
 /* keep reading each line until you get to the end of the file */
 /* Spilt each line up into bits and pieces using a comma as a separator */
 subtext = splitTokens(text,"\tab");
 }catch(FileNotFoundException e){
 }catch(IOException e){
 try {
 if (br != null){
 } catch (IOException e) {
int outPin = 2; //define outpin
byte binary_signal;
void setup() {
  pinMode(outPin,OUTPUT); //set output pin

void loop() {
    /* check if data has been sent from the computer: */
    while (Serial.available()>0) {
    /* read the most recent byte */
    binary_signal =;
    //You have to subtract '0' from the read Byte to convert from text to a number.
    //Send LOW signal to the output pin if the byte Read = 0
      //Turn off all LEDS
      digitalWrite(outPin, LOW);
      //Send HIGH signal to the output pin if the byte Read = 1
      digitalWrite(outPin,HIGH); //set the output to high
      digitalWrite(outPin,LOW); //then set the output to low

In case you cannot download the source codes, I have copy them here. Thanks.

You need what is called handshaking. You send the data in chunks, say 32 bytes at a time. When the arduino has recieved 32 bytes it sends an acknoglement to processing, this can just be a single byte say a letter A. Processing will not send the next chunk of data until it receives the acknowledgment character.