One major new feature in Processing 3.0 is hardware IO (input/output) access on the Raspberry Pi.  This means you can control the GPIO (general purpose input/output) pins on the Pi and use them to blink LEDs, read buttons, and more.  Combined with the graphic and GUI functionality already in Processing you can prototype and create interesting hardware projects entirely with Processing!

As an example I'll walk through a simple Processing sketch to light LEDs and read a button.  The example will display two boxes on the screen.  If you click/press inside a box it will light up a LED and change the box color.  If you press the button it will change the screen color to blue:

To build the hardware for this sketch you'll need these parts (all of these parts are available in the Raspberry Pi starter kit):

  • Breadboard and breadboard jumper wires.
  • Red LED.
  • Green LED.
  • Two 560 ohm 1/4 watt resistors - You can use any resistors in the range of about 300 to 1,000 ohms, but you must have them or else you could damage the Pi!
  • A momentary push button.
  • A 10 kilo-ohm 1/4 watt resistor - This is used as an external 'pull-up' resistor to help read the button.  Although the Pi actually supports internal pull-up resistors they aren't available from Processing right now.  However you can add your own pull-up like this example will show.
  • Although it's not technically required a Pi cobbler will make it much easier to connect components to the Raspberry Pi GPIO pins.

If you're new to using a breadboard check out this great description of how to use them in an Arduino getting started project.

Connect the components on the breadboard in the following way:

  • The top left corner of the button is connected to the Pi GND pin, and the bottom right corner of the button is connected to Pi pin #17.
  • One leg of the 10 kilo-ohm resistor is connected to Pi pin #17 and the other leg is connected to the 3.3V pin on the Pi.  This will act as a 'pull-up' that keeps the button input at a high level until the button is pressed and 'pulls' it down to ground.
  • The anode (longer leg) of the red LED is connected to Pi pin #22, and the cathode (shorter leg) of the red LED is connected to one leg of a 560 ohm resistor.  The other leg of the resistor is connected to the Pi GND pin.
  • The anode (longer leg) of the green LED is connected to Pi pin #27, and the cathode (shorter leg) of the green LED is connected to one leg of a 560 ohm resistor.  The other leg of the resistor is connected to the Pi GND pin.

Next download the Processing_Pi_GPIO sketch from its home on GitHub by clicking the button below and extracting the archive:

Copy this sketch to the Raspberry Pi using a tool like FileZilla (described in the previous page).

Now you can run the sketch using the steps to run outside the editor described in the previous page.  Make sure the Pi has booted to the graphical desktop environment, then connect to the Pi's command line terminal.

If you're using the Raspbian Jessie operating system which allows access to GPIO as the Pi user run the commands:

cd ~/processing-3.0.1/
DISPLAY=:0 ./processing-java --sketch=/home/pi/Processing_Pi_GPIO --present

However if you're using the older Raspbian Wheezy operating system which requires a root user to access GPIO you need to run the slightly more complex commands:

cd ~/processing-3.0.1/
DISPLAY=:0 XAUTHORITY=/home/pi/.Xauthority sudo ./processing-java --sketch=/home/pi/Processing_Pi_GPIO --present

This command will run Processing's command line tool as the root user so it can access GPIO, however it also sets a special XAUTHORITY environment variable that's needed to use the desktop environment as root.

Once the sketch is running it will show two boxes on the screen:

Press a box to turn on or off the LED (the left box controls the red LED and the right box controls the green LED):

And press the momentary button to change the screen color to blue while it's pressed:

If you're curious how the hardware IO access works you can open the sketch in Processing's editor and examine the code.  The Processing hardware IO reference is a great resource to follow to understand the code too.

The sketch uses the following hardware IO functions to control the LEDs and read the button.  If you've ever used an Arduino these functions should look very familiar:

  • GPIO.pinMode - This function is called in the setup of the sketch and configures a pin as either an input or output.  The LEDs are outputs and the button is an input.  Notice too that each pin is identified by its number on the Pi cobbler (these are the Pi's BCM pin numbers if you're familiar with the Pi's pinout).
  • GPIO.digitalWrite - This function will set an output pin to a high (on) or low (off) value.  When the LED pins are set to a high value they will turn on, and when set to a low value will turn off.
  • GPIO.digitalRead - This function will read the value of an input pin.  It returns either a high or low value depending on if the pin is at a high (on) value or low (off) value.

That's all there is to controlling the GPIO pins on the Raspberry Pi with Processing!

This guide was first published on Nov 13, 2015. It was last updated on Feb 19, 2024.

This page (Hardware IO) was last updated on Feb 19, 2024.

Text editor powered by tinymce.