Ambient Orb

One of my hobby projects is Hudson, and there I use colored ballsas a metaphor of the build status — red means fatal errors, yellow means test failures, blue means OK, etc.

One day I came across the ambient orb, which I thought would be a rather cool widget as the real world representation of the Hudson ball. I commit to a large number of projects, and I often break a build, so I thought maybe I can use this orb so that I can notice whenever I break a build (or cause test regressions.)

Then I checked the price, and it was pretty expensive! It also apparently incurs monthly expense, because it fetches the data over the pager network.

Why don't I make it myself?
PIC 18F2550 with USB support
Whole computer in a chip

That's when I noticed that the company makes the schematic of AmbientOrb available along with very helpful technical notes. In my junior high school days, I was in the electronics club for about a year or so, so I do know the basics (well, a little bit of it, anyway.) So I looked at the schematic, and thought it's actually pretty simple.

The key to the simplicity is a programmable micro-controller called "PIC" chip. It's a whole computer in one chip, and best of all it's programmable, so you can put all the hard part (in this case controlling the color of LEDs) in a software. PIC then uses its I/O pins to turn on/off transistors, which in turn drives LEDs.

PIC is apparently a flagship product of Microchip, and there are a whole series of those with different capabilities. I noticed thatsome of them have USB support, and thought that would be just excellent — this allows me to leave the "hard part" in PC, such as checking the build status via HTTP, then PC can tell the orb higher-level instructions (like "become flashing yellow ball".) The orb will take that and actually drives the LEDs.

The other benefit of using USB is that I can draw power from USB, so I won't have to make the power circuit, which makes things simpler.

Initial Design
USB Bit Whacker
Look at how small it is!

After a bit of research like that, I started drawing the schematic on the paper. The hard part turns out to be LEDs. I wanted to do at least red, yellow, and blue, so I was looking for tri-color LEDs that has all R,G, and B in it. LEDs also needed to be fairly bright, since I have to light up an orb.

While I can easily find them in Japan, it was very difficult for me to find equivalent parts here in the U.S. Eventually, my colleague Ken (who turns out to be a real electronics geek!) told me about SparkFun, and its very bright tri-color LED offering.

While I was at it, I made a great discovery. SparkFun sells a PIC chip with all the necessary parts (like oscilator and USB connector) in a tiny board, called "USB Bit Whacker" (originally developed by Brian Schmalz.) This not only greatly simplified my orb design, but it also made it a lot cheaper. Normally, to download a program into a PIC you'd need a separate programmer device, which sells > $100 (or you can make one, but that was beyond me.) UBW comes with a "bootloader" program that is capable of flashing itself, so I won't need the external programmer.

Buying Parts

HSC Santa Clara store. Great place.

I ordered two LEDs and UBW from SparkFun, but I still needed other parts, like resistors, boards, transistors, etc. I first went to nearby parts store that I found via Google Map, but when I went in the store, it was clear that was not the place I was looking for — it was just too small, and everything was pretty expensive.

I asked anyway if they have the parts that I was looking for. They didn't, but another customer waiting behind me kindly told me that there's another place that I should go. This place, HSC electronic supply, turns out to be a great place. You can buy individual parts at a very low price (like 2 cents per resistor, 8 cents per transistor — it's a real mystery how they manage to make ends meet), and the store staff are very friendly folks who don't mind my asking dumb questions!

The other trouble I had was multi-meter. I bought one from SparkFun, but it was DOA. My colleague Dhiru have told me before that he had one, so I asked him if I could borrow his. It turns out that his was broken too, and so I had to start by fixing a multi-meter. It turns out Dhiru's multi-meter is dead just because its probe is dead, so Dhiru and I removed it and was able to make it work.

Nothing beats good old breadboard

Now that I have all the parts, I started putting them together on a breadboard (which I bought from Fry's.) I also found a wall-mount light fixture at HomeDepot for $5, which I decided to use as the orb.

It quickly became obvious that 2 LEDs are not bright enough. It looks OK in night, but in the daylight I can barely see the color. So I bought 6 more, making the LEDs total of 8. Also, 3 colors were not evenly bright (red is weak — datasheet says so too), so I decided to add extra red LEDs to compensate that, making total of 12 LEDs!

When you change one thing, you need to change other things to match. Each LED draws 20mA per color, so with this change, now a transistor needs to drive as much as 240mA. The transistors that I initially had can't drive that much current, so I needed different ones.

And if I change transistors, now I need to also change the resistors to match with its parameters. There's a few rounds of adjustments like that, until it became a reasonable prototype.

I tried so many different resistors,
but they are $0.02/ea

I was more optimistic about the software side, because after all I'm a professional software engineer :-).

There are two pieces of the software I needed to write. One that runs on PC, and one that runs inside the PIC.

On the PC side, how do you talk to USB devices? Well, there are a few pre-defined "class" of USB devices (like mouse, keyboard, etc), and one of them is called "communication device class" (CDC). This is a fairly well supported type of devices on Windows and Linux, and from those operating systems, CDC devices will look like serial ports, so programming PC side is really easy, and you can even do that in Java.

The PIC side is bit harder, mainly because those devices are designed for getting maximum juice out of small memory and CPU power and not for ease-of-development (gotta love Java!) But there's a large community of hobbiests who share their source code, and Microchip makes IDE and C compiler freely available. So I just needed to spend a bit of time to play with this stuff. I wish Microchip had a C++ compiler, but writing this in C was not too bad.

The whole chip only has just 384 bytes that I can use for data space. It was a really big change from my day job, where my IDE could easily weigh in at 386 MB — 106 times more memory!

Eventually I decided to develop a mini language for controlling the color. It reads like this:

// yellow flash mode
repeat {
  // gradually become full yellow in 500ms
  become (255,255,  0) in 500;
  // gradually become full black in 500ms
  become (  0,  0,  0) in 500;

I wrote a compiler that translates this into small byte code, which is sent to PIC. PIC stores the interpreter of this byte code, and executes it. I still need to write the program to connect this to Hudson, but that should be a piece of cake.

Assembling Pieces
Assembled pieces

Now that both the hardware and the sofware are fully tested on breadboard, it's a soldering time!

I first started with a cheap-cheap $2.99 soldering iron. Unfortunately it took me no time to spend all $2.99 worth out of it. The tip quickly broke, and it was no good! It could have been the problem with my soldering skill, but I decided to blame the tool.

So I went out to buy another soldering iron. This time I chose "made in Japan" :-), and while it cost $20, I regretted not buying it from the get-go. I think it is actually the same model I used in my junior high school electronics club, because I remember the shape and the color. It's amazing that this thing sells for 15 years without any model change.

I put all the LEDs in one board, so that I can put it at the center of the orb. The PIC, transistors, and resistors went to another board. I think it took me 6 hours or so to solder them all up and wired correctly.

And Finally...

When I'm done with wiring it was pretty dark already. I briefly wondered whether I should leave it at that and call it a day, or to connect that to PC and see if it really works. I haven't done any soldering for a long time, so I thought there's a good chance I didn't get some of them right. And if there's a trouble, I'd spend more time trying to fix the issue. I'd be late, and my wife would be upset.

But I couldn't resist the temptation to try it out, so I told myself "just this once..." and plugged it in.

It was a moment of truth!

To my pleasant surprise, it actually worked!

So here's the orb in the demonstration "rainbow" mode, showing all colors:

While it's not as pretty as the ambient orb, this is cheaper, and more importantly it's way more fun! If anyone is interested in the source code / schematic, I'd be happy to make them available.

Uh-oh. Build is broken!

Thanks to Takeshi Umezawa, for his advices. He was one of my friends at the junior high school electronics club, and he knows this stuff pretty well. Thanks to Dhiru, for his multi-meter. Thanks to Ken, for his many advices and telling me about SparkFun. And thanks to Rajiv for being my 3rd arm during some tricky soldering.