Arduino + Processing

The instructions to make arduino talk to Processing through Firmata are here.

1- Unzip the Processing library and copy the “arduino” folder into the “libraries” sub-folder of your Processing Sketchbook.
(You can find the location of your Sketchbook by opening the Processing Preferences. If you haven’t made a “libraries” sub-folder, create one.)

2- Run Arduino, open the Examples > Firmata > StandardFirmata sketch,
and upload it to the Arduino board.

3- Make this circuit:

4- Run the processing sketch below.

//import the libraries for serial communication
//and for arduino/firmata specifically
import processing.serial.*;
import cc.arduino.*;

//this object class Arduino
//represents guess what? Your board
Arduino arduino;

//like in an arduino sketch it's good to
//use variables for pin numbers 
int buttonPin = 13;
int sensorPin = 0;

void setup() {
  size(500, 500);
  //the arduino object needs to be created at the beginning
  arduino = new Arduino(this, Arduino.list()[0], 57600);
  //once it's created I set the pin modes  
  arduino.pinMode(buttonPin, Arduino.INPUT);
  arduino.pinMode(sensorPin, Arduino.INPUT); 

//we are in processing so the main function is draw (not loop)
void draw()
  //read a digital value from the button pin
  int digitalValue = arduino.digitalRead(buttonPin);
  //change the background accordingly
  if (digitalValue == Arduino.HIGH)
  //read an analog value from the sensor pin
  int analogValue =  arduino.analogRead(sensorPin);
  println(analogValue); //print it for testing purposes
  //draw a red circle ellipse the size of the analog value
  ellipse(width/2, height/2, analogValue, analogValue);

Some exercises:
– Threshold switch, if the pressure sensor reaches a certain value a sound is triggered.

– Combine this example with your order and chaos processing sketch, replacing the pressure sensor with the slider.

Try with a photoresistor:

Or a magnet+Hall Effect

– Team up with a friend and use two knobs potentiometers to make a digital etch-a-sketch. One pot controls x, controls y, the red buttons clear the drawing.

Processing to arduino without Firmata

You can write your own serial communication protocol without using firmata in case you have issues with servo and stuff.
Here’s an example with two analog inputs to processing.

Arduino Code:

int sensorValue0;
int sensorValue1;
void setup() {
  // initialize the serial communication:
void loop() {
  sensorValue0 = analogRead(A0);
  sensorValue1 = analogRead(A1);

Processing code:

import processing.serial.*;
int sensor0 = 0;
int sensor1 = 0;
Serial myPort;
void setup() {
  size(200, 200);
  // List all the available serial ports
  // I know that the first port in the serial list on my mac
  // is always my  Arduino, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  // don't generate a serialEvent() unless you get a newline character:
void draw() {
  // do your stuff here!
void serialEvent(Serial myPort) {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    // trim off any whitespace:
    inString = trim(inString);
    // split the string on the commas and convert the
    // resulting substrings into an integer array:
    int[] sensors = int(split(inString, ","));
    // if the array has at least two elements, you know
    // you got the whole thing.  Put the numbers in the
    // sensor variables:
    if (sensors.length >=2) {
      sensor0 = sensors[0];
      sensor1 = sensors[1];
Posted in

Post a comment