Tuesday, September 15, 2015

USB-to-Serial Mod for Sort of an Embedded Guy

I was first introduced to embedded design back in college where we had two classes based on the Motorola 6800 and 68000 series microcontrollers.  One class focused on the architecture and the other on assembly and the instruction set.  I immediately wanted to make a project of my own.  Unfortunately the classes were a little too theoretical and didn't detail the necessary steps to actually get a circuit up and running.  But even so, I managed to order myself a Microchip ICD2 decently cheap and a couple of DIP packages (and oops, a QFN chip) as samples.  It was all very exciting, until I unboxed it realized I needed a serial COM port, and my computer didn't have one.  Off to the electronics store to buy a USB to Serial adapter.

So that is all well and good until you get comfortable blinking lights and such, and want your little circuit to talk to a computer.  I actually used a MIDI port as my first serial interface because I had musical devices in mind.  A fun learning experience, but in the end I did end up getting a USB ICD2 down the road freeing up the serial adapter.  Then started a different set of issues.

To comply with RS-232 standards, the adapter not only inverts the signal, it bumps it up to 12V.  Your garden variety embedded circuit will be 3.3V or 5V.  Inverting the signals and dealing with the voltage difference can be handled easily enough though, with a 74x04 Hex Inverter or a couple of transistors.  It just crowds the bread board and is one more thing to assembly to get a circuit up and running.  Also, that DB9 connector is a pain and you're stuck tethered about 12 inches from the USB port.

Possible Solutions:

  1. Make a breakout board.  Seems easy at first, except the DB9 headers I have don't fit into a .100" grid due to offset rows.  The target circuit would also have to power the circuitry needed to invert and convert the voltages.  You could remove the DB9 and solder direct or use a different connector, but still not the best idea.
  2. Remove the DB9 and add a .100" header or solder pins directly to the PCB of the adapter.  I actually did this, it made hooking it up easier, but destroying the plastic housing of the adapter made things kinda hairy.  There was pretty much no more strain relief, and everything was exposed and unprotected.  It just wasn't a comforting setup.
  3. Buy a breakout module off the internet.  This is actually probably the best idea.  They're small, cheap, and self contained.  As a matter of fact, I could just take an Arduino Nano and blank the program of the chip (it'd probably be cheaper than a breakout module).  Then I'd get a 3.3V and 5V supply with it as well.  But then I'd always be tempted to make something else out of it....  And what fun is that?  I already have this adapter, let's make something out of it.
  4. Build a module based around the PCB of the adapter.  The 5V from the USB bus can power any extra circuitry, and even the target device if needed.  And, it uses up stuff I already have!
Shown above is my resulting device.  As an afterthought I added J4 to support the original function of the adapter.  It turned out that after I built this, the one thing I wanted to test it on already had circuitry built in to invert and level shift the voltages.... Oops.  I had to include jumper J3 so that the output of my hex 74ALS04 wouldn't fight against the output of from a RS-232 device.  The output of U2.6 would be unpredictable with nothing plugged into J2, resulting in a situation where it could try to drive +5V while a RS-232 device tries to drive -7V or even lower.  I don't know if there is enough short circuit protection in these IC's for a 12V differential, but I don't want to find out.  De-soldering chips is a huge hassle.  The jumper also makes the circuit look like it has more features!
Speaking of short circuits, I added a few resistors in places to protect stuff.  The 120R resistors take the brunt of improper connections, though probably survive too long.  But they are easier to replace.  In retrospect, I should have gone larger, but I won't fix that issue until something starts on fire.  We'll just pretend they're fuses.
The 4.7K resistor essentially handles my "level shifting".  While the datasheet says the 74ALS04 can handle up to +7V, that may be dependent upon the supply voltage.  I'm showing my estimation of the input circuit.  Protection diodes D1 & D2 may or may not exist.  If they don't, a +7V input is no big deal as the emitter of Q1 won't take any current.  On the other hand, a -7V input will likely draw excessive current.  R3 of my circuit above limits that to a safe amount.  Now if the diodes do exist, the positive input could present an issue.  Any voltage above Vcc (+5V) plus the diode drop (typically spec'd at .3V from what I've seen from datasheets) will be seen as a short across D1.  Again, the 4.7K resistor limits the current.  The same also goes D2 with negative input voltages.  Anything below -.3V starts to short across the diode.  There are likely some small resistors (or perhaps just inherent resistances) in series with D1 & D2, but they may not be up to par.
I could probably just use the diode check of my multimeter across the input of a 74ALS04 with respect to the supply pins to determine the presence of the protection diodes; but at the time of writing this I'm sitting in a coffee shop.  And besides, I've already soldered it all together.  Better safe than sorry.
Photo taken from USBGEAR.com
Above is a similar adapter to what I started with.  The little box in the middle of the cord houses the PCB and a ton of potting material.  Below is my finished little module.  Not the greatest piece of workmanship, but I did get to use my router table.  My skills with the hot snot need improvement, but overall I'm happy with the result.  It's heavy enough that attached cables don't force it to move or twist, and the solder side is protected from any incidental contact with parts lying on my desk.  And best of all, I made it.
Not pretty, a bit messy with the hot glue, but it's worked really well so far.