Skip to the content.

Arduino to Processing

Arduino to Processing

The basic concept is that we are sending information from the Arduino, through the serial port on your computer (which the USB is connected to).

The Processing program can see that data coming through the serial port, and use it in it’s own code.

This allows you to use the sensors on the Arduino to affect your Processing code. For example:

Basic Arduino Code

Arduino Code

This is super simple, and something you have most like already done at this point.

  // Arduino Code
  void setup() {
    Serial.begin(9600);
  }

  void loop() {
    Serial.println("Hello, Processing!");
  }

All you need to do on the Arduino side is send something to serial using the Serial method.

  Serial.begin(9600);
  Serial.println("Hello, Processing!");

Basic Processing Code

The Processing Code

Processing requires a couple more lines of code to read in the serial port data.

  // Processing Code
  import processing.serial.*;

  Serial myPort;

  void setup() {
    myPort = new Serial(this, Serial.list()[1], 9600);
    // printArray(Serial.list());
  }

  void draw() {
    println(myPort.readStringUntil('\n'));
  }

This is all that is needed to read in the code sent from the Arduino.

import processing.serial.*;
Serial myPort;
myPort = new Serial(this, Serial.list()[1], 9600);

Note: The result of Serial.list() is an array of the different serial ports on your computer. [1] is the index from that list that we want to use. To find which serial port the Arduino is connected to, you can run this sketch but uncomment the printArray(Serial.list()); line. The results are displayed in the Console section of the Processing IDE. Or in the Arduino IDE, go to Tools->Port and find the serial port assigned to the Arduino. Counting starts at zero. So if there is only one port listed, change Serial.list()[1] to Serial.list()[0]

println(myPort.readStringUntil('\n'));

Processing showing text sent from Arduino

This is super basic, and will do the job. See Arduino to Processing with a Photoresistor for adding more functionality.