Skip to the content.

Processing to Arduino

Processing to Arduino

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

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

This allows you to control physical objects (motors, LEDs, speakers, etc) with a graphical interface created in Processing. For example:

Basic Processing Code

The basic Processing code

import processing.serial.*;

Serial myPort;

void setup() {
  size(200, 200);
  myPort = new Serial(this, Serial.list()[3], 9600);
}

void draw() {
  if (mousePressed == true) {
    myPort.write('1');
    println("1");
  } else {
    myPort.write('0');
  }
}

This code is very simple, but is explained below.

import processing.serial.*;
Serial myPort;
void setup() {
  size(200, 200);
  myPort = new Serial(this, Serial.list()[3], 9600);
}
void draw() {
  if (mousePressed == true) {
    myPort.write('1');
    println("1");
  } else {
    myPort.write('0');
  }
}

That’s it for Processing.

Basic Arduino Code

This works best if you have an LED hooked up to the Arduino. The Arduino does have a very small LED next to pin 13 that can be used to see if the code works, but it’s much more fun to build a circuit. :)

Basic Cicuit

Basic LED circuit

Connect the negative lead of an LED to GND on the Arduino. Then connect the positive lead of the LED to pin 13 on the Arduino.

One LED

Real life circuit

Basic Code

The basic Arduino code

char val;
int LED = 13;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    val = Serial.read();
  }

  if (val == '1') {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }

  delay(10);
  }

This code will listen on the serial port and capture any data and store it in a variable named val. If the value of that variable is the string ‘1’, then it will supply current to pin 13, otherwise it removes current from pin 13.

char val;
int LED = 13;
void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}
  if (Serial.available()) {
    val = Serial.read();
  }
  if (val == '1') {
    digitalWrite(LED, HIGH);
  } else {
    digitalWrite(LED, LOW);
  }
delay(10);

Running the Sketches

First, send the code to the Arduino. Then run the Processing sketch. If you click in the window generated by processing, the LED will light up!

One LED, light it up