Monday, June 23, 2008

Build a USB Bit Whacker in 10 minutes



I've moved!  Please go here: http://bafoontecha.com/2008/06/23/build-a-usb-bit-whacker-in-10-minutes/ for the latest version of this article.

Every so often you come across a reinvention of an old idea that blows you away, not only in its elegance but in its sheer utility.  For me, this occurred a few weeks ago, when I came across the USB Bit Whacker.  The name was familiar, I'd surely seen it while browsing the Sparkfun product catalog, but for some reason the description hadn't compelled me to click.  

First, a bit of history...  In the good-old days, computers came with these things called "parallel ports", or so I've been told (should that be active tense?)  These "parallel ports", with a bit of register twiddling, empowered the user with 17-some-odd digital pins for inputting and outputting data.  Newer computers computers tend to forgo parallel I/O methods for faster and sleeker I/O methods, ala, USB, USB, and USB.  While these newer generation of I/O ports are much more compact and support higher bandwidths, they are a bit harder for the common user to interface with.

In comes the USB Bit Whacker.  Calling the USB Bit Whacker a virtual parallel port is doing it a disservice; it's much, much more than that.  It gives you complete control of a fairly powerful microcontroller, the PIC18F2550, through a serial, command-driven interface.  The entire platform is powered from the USB bus, no wall-warts or additional wires!  In addition, all communication occurs through a virtual serial (COM) port, using the USB CDC protocol.

Simple ASCII commands allow you to configure and use the following onboard hardware peripherals: SPI/I2C module, USART serial module, PWM module, and 13-channel 10-bit Analog-to-Digital converter module.  Support for additional modules can easily be added to the open-source firmware.

I love microcontrollers, and primarily due to the generosity of Microchip, have a number of PICs in my parts collection.  I came across the USB Bit Whacker project while desperately trying to get USB CDC working on a PIC18F2550.  Encouraged by the extreme simplicity of the schematic , I decided to breadboard it.  In a matter of minutes, I had a working development kit.  You can either buy a kit, or follow these instructions to build your own.


Requirements
  • 220 uF  electrolytic capacitor
  • 0.1 uF capacitor
  • 20 MHz oscillator (see the PIC18F2550 datasheet or additional options)
  • 2 push buttons
  • 1 USB Type B connector
  • 2 3.3k resistors (the value of the pull-up resistor is not critical)
  • 2 pieces of straight headers
  • A few jumper wires
  • A breadboard/perfboard
  • 1 PIC18F2550 or PIC18f2450 microcontroller
  • PIC Programmer
Hardware Construction


Prepare the USB connector by cutting off the mounting brackets


It should look something like this


Solder the header pins to the USB connector.


Alternate view

Original schematic (Credit: Brian Schmalz)


Begin placing the components on the breadboard


Completed breadboard USB Bit Whacker.  (I left out the status LED)

Software

Now that you have a completed Bit Whacker, you need to burn the bootloader onto your PIC.  Instructions for burning firmware onto your PIC are out of the scope of this document.  Luckily, you can find a wealth of information online.  I use an MPLAB ICD2 clone that I purchased on eBay.  

If you are using a 20 MHz oscillator, you can use this bootloader firmware.  Additional bootloader firmware is available on the USB Bit Whacker homepage (Search for 'Bootloader').  (I know this is confusing, but the *Bootloader* firmware is different than the USB Bit Whacker firmware.  You need to burn the *Bootloader* firmware onto your PIC first using a dedicated PIC programmer.  After this, additional firmware updates can be done through the USB port!)

Bit Whacker in Bootloader mode

Once you've burned the Bootloader firmware onto your PIC18F2x50, you need to load the Bit Whacker firmware.  Microchip provides a USB Framework, which comes with an application named PDFUSB.exe.  Using PDFUSB, you can load Bit Whacker firmware onto your PIC.  To prepare the PIC for loading firmware serially, you need to connect your development board to a USB port on your computer.  Then, while holding down the "Program" button (the orange pushbutton in the picture above), press and release the "Reset" button (the black pushbutton in the picture above).  The status LED on the prenatal USB Bit Whacker should be steady, and a Custom USB Device named "Microchip Custom USB Device" should show up in your Device Manager.



Download the latest USB Bit Whacker firmware, here.  At the time of writing, this was v1.4.3.  Load the PDFUSB.exe bootloader and select the firmware and the USB Board as follows:


Click on "Program Device" to load the firmware onto the PIC.  Once the loading is complete, hit "Execute" to switch to the USB Bit Whacker Firmware.  The status LED on the breadboard should begin to blink.  Looking in your Device Manager, you should have a new COM port.  On my machine, this shows up as COM4.




Now, open you serial emulator of choice (HyperTerminal, teraterm, etc...)  and open up the serial port.  If you're using a Mac, try "screen /dev/tty.usbmodem1d11 57600".

Type the following commands to set Port A on the Bit Whacker to analog input, and to set a timer to output values of the analog pins at a 100ms interval (Unfortunately, you won't see what you are typing, so if you think you've made a mistake just hit "Enter" and try again.  The next revision of the firmware should fix this.  Also, this isn't as big a problem as it seems now, keep reading!)
c,31,0,0,5
t,100,1
The first line calls the "Configure" command indicating that all five available pins on Port A are inputs, all the pins on Port B are outputs, all the pins on Port C are outputs, and the 5 Port A pins are analog inputs.

The second line calls the "Timer" command with an interval of 100 ms, in analog mode.

You should see a stream of 0's filling your terminal window.  Try touching a few of the pins (2-7) to see the numbers change!  You now have a rudimentary, but fairly powerful DAQ at your fingertips.  

Term output

Further instructions on using your Bit Whacker can be found here.

Advanced Example


The major advantage of having a command-driven interface is that it is easily scriptable.  I like to write small Python scripts to control my USB Bit Whacker.  I had an old Geophone from EPO Depot laying around my parts bin.  If you click the link, you can see that a Geophone is a simple electromagnetic device which generates a voltage based on the displacement of a magnetic mass.  It's useful for detecting low-frequency vibrations such as earthquakes, footsteps, and in some cases, a combination of both.  


Scoping the output of the Geophone, I determined that the maximum voltage swing was around 4VPP (shaking it violently).  The average VPP was around 100 mV.  I wired up a quick op-amp circuit to center the Geophone output to 2.5V (1/2 the Bit Whacker rail voltage) and add a fair bit of amplification.  I rely on the fact that the opamp can not exceed its supply rail voltage to protect the Bit Whacker inputs.  This is generally not a good design practice, but hey, I was in a hurry.

I then wrote a Python script to capture the analog output of the Geophone at a sampling rate of 200 Hz.  Download it here.  

Plot of captured footstep data

The plot above is of four footsteps near the Geophone.  From the plot, I suspect the op-amp is latching up, but that's something I'll save for another post!

Additional Notes

I've made a number of Bit Whackers over the last few weeks and have made many modifications to the firmware, including adding SPI and I2C support.  I've sent the changes to the developer, Brian Schmalz, who will hopefully roll them into the next firmware revision.

Here are a few pictures of the other Bit Whacker's that I've built.  One way to prevent components from moving around is to hot-glue them.








Perf-board USB Bit Whacker

Thanks,

Sumanth P.

12 comments:

Anonymous said...

Maybe I'm missing something here, but when you say you can't see what you're typing in the serial terminal window, why don't you configure the terminal to 'autoecho' what the user is typing ? Many terminal software allows for that.
Ciao !

arges said...

Nice post. Very informative!

Anonymous said...

Absolutely excellent writeup! This is something that the UBW community has needed since day one, and I've just never got around to doing it (like a million other things). Thanks so much for sending me your code - I'm working on the UBW32 (based upon the new PIC32 parts) and as soon as that design is finalized, I turn back to the next version of UBW firmware. Very good pictures, and I love your breadboard techneique. On the SparkFun USB forum there is tons more good information about what people are doing with their UBWs. Don't forget that it is very easy to write your own code (Like blinking LEDs) and use the bootloader to download it to the UBW - use it purely as a development board for whatever code you want to write.

Thanks again!

*Brian

peddamat said...

5volt, doh! You're completely right. I'm ashamed to say this didn't cross my mind. And thanks for your great contributions to the Maker scene!

Arges, ARGES!

embeddedman, I wouldn't have happened if it wasn't for you. Thanks again!

Anonymous said...

will this work on a mac? or will I need any special software to comunicate with the PIC?

peddamat said...

anonymous, yes, this works perfectly on a Mac. In fact, I use my UBW exclusively with my Macbook. The device shows up as a serial port named usbmodem1d11 in '/dev'/.

I haven't been able to find a really good, free, terminal emulator for the Mac platform, so I just use GNU Screen. It's installed by default.

After plugging in your UBW, open a terminal window and type 'screen /dev/usbmodem1d11 57600'.

Anonymous said...

good then, I'll definitely try this out.

Anonymous said...

hey peddamat what software are you using on your mac to edit make schematics? I'm new with macs and still haven't found any electronic design software :S

A list of useful software would be useful!

Thanks

avanrotciv

peddamat said...

avanrotciv,

I've switched over from Protel in a Parallels session to the Mac-native version of Eagle 5. You should check it out, it's fantastic. I'll whip up a post on the other Mac-specific development tools that I use.

Anonymous said...

boy, you can spend the lines of code I use on this occasion for the pic, thank you very much for your input.

Mark said...

Hello,

You wrote: c,31,0,0,5
but shouldnt the 31 be a 32?

See you!

embeddedInAUS said...

Hi,

Great article - thanks.

I'm actively developing enhancements for UBW FW 1.4.9

I've implemented the serial commands and was wondering if you are able to send me your code for the SPI commands? I'm looking to port a library for the RFM69 module (a communications transceiver module) and need the SPI capabilities of the UBW to drive it. Porting the library is a job in itself - so any UBW SPI code that you have would be a great help.

Best regards,

Ken

theory meets application.