Blinky LED ‘Hello World’ using USB Microcontroller in ‘Processing’

Posted by – March 24, 2009

Every good embedded systems hardware project begins with a blinking LED (or toggling level as seen on the oscilloscope).  In language, there’s the opportunity for both, since the built-in graphics allow for data display as well as the USB microcontroller interface.  (There’s several Processing projects for Arduino, BTW.)   Source code is below.

USB Microcontroller blinks happily under program

USB Microcontroller blinks happily under program

The firmware in this micro allows simple string commands to control & query the port pins.  So controlling the LED was easier than figuring out the drawing co-ordinates for the graphics.  This short program plots a graphical line corresponding to the output of the LED.  The language reminds me of the simplicity of Logo (the simple-as-basic pen plotting language everyone used for simple robotics), plus Processing has all the Java abilities as well.

Processing runtime environment and output

Processing runtime environment and output

So here’s the code; note the use of automatically-called setup() and draw() functions:

// UBW Microcontroller 'Hello World'; jcline 2009-03
import processing.serial.*;
Serial ubw; // USB microcontroller communication
int loops = 0;
char toggle;

void setup() {
  String ubwVersion;
  boolean found;
  float tdelay;
  found = false;
  background(0); // black
  frameRate(1); // sets draw rate
  stroke(255); // white
  try {
    ubw = new Serial(this, Serial.list()[0], 9600); // serial rate doesnt matter
    tdelay=millis() + 5000;
    // Read firmware version from connected UBW Microcontroller; jcline 2009-03
    for (int i=0; millis() < tdelay ; i++) {
        // Get+Verify UBW version string, print to console
        ubwVersion = ubw.readStringUntil('\n');
        if (ubwVersion != null) {
          if (ubwVersion.startsWith("UBW", 0)) {
            println("Found UBW attached to USB:\n");
            found = true;
  catch (Exception e) {
    println("device access error\n");

  if (found != true) {
  else {

// Toggle LED 10x depending on framerate & draw graphical level indicator
void draw() {
  String cmd;

  if (loops > 9) {
  if (toggle == '0') {
    toggle = '1';
  else {
    toggle = '0';
  cmd="PO,B,0," + toggle;
  ubw.write(cmd + "\n");


Change the framerate to modify the iteration of draw().

If you enjoyed this post, make sure you subscribe to my RSS feed!

1 Comment on Blinky LED ‘Hello World’ using USB Microcontroller in ‘Processing’

  1. […] is pretty much a copy & paste of the previous simple example, with some added error checks and non-optimized state machine.  (This will all be re-written as […]