Using the Processing.org Language with Microcontrollers

Posted by – March 22, 2009

Media-technology engineers at MIT have created a computer language and easy-to-use runtime environment called Processing, hosted at processing.org.  I wrote a small code snip for accessing the PIC microcontroller from a USB port, using Processing; it’s pasted below.

This PIC microcontroller connects to USB on a PC, Mac, or Linux machine

This PIC microcontroller connects to USB on a PC, Mac, or Linux machine

The Processing language and frameworks run on top of Java, so all Processing programs can run on Windows PC, or Unix, or Mac OS/X.  The frameworks are geared towards simple-to-write graphics and data presentation, though a lot of underlying Java libraries are directly accessible.

// Read firmware version from connected UBW Microcontroller; jcline 2009-03
import processing.serial.*;
Serial ubw;
String ubwVersion;
try {
  println(Serial.list());
  ubw = new Serial(this, Serial.list()[0], 9600);
  float tdelay=millis() + 5;
  for (int i=0; millis() < tdelay ; i++) {
      // Get+Verify UBW version string, print to console
      ubw.write("v\n");
      ubwVersion = ubw.readStringUntil('\n');
      if (ubwVersion != null) {
        if (ubwVersion.startsWith("UBW", 0)) {
          println("Found UBW attached to USB:\n");
          println(ubwVersion);
          break;
        }
      }
  }
}
catch (Exception e) {
  println("device access error\n");
}
println("done\n");

Listing 1: Access a UBW-firmware PIC Microcontroller from Processing

It looks like Processing might be a good environment for fetching and graphically animating information from my data acquisition project.  I’ll post more about these techniques if it works out.

2 Comments on Using the Processing.org Language with Microcontrollers

  1. nahro says:

    hi, i want to known how i can working usb port by java language, please help me…??

    • JonathanCline says:

      I haven’t looked into using the USB port with Java. These boards (UBW and Arduino) use a USB chipset that acts like a serial port on the host PC side, so the communication is the same as a terminal/serial device. That way there’s no need to use USB drivers. There are others who have used similar hardware with USB HID controller (keyboard/joystick) drivers with success. Check sparkfun.com forums for info, or Arduino forums.