Jonathan Thomson's web journal

DIY USB to Serial Cable For $3! February 21, 2010

Filed under: Electronics — jethomson @ 5:25 am

 

Please take a moment to vote for my entries in the Hack It! and Make It Glow Challenges!

 

Update
Several good, inexpensive, purpose-built USB to serial modules are now available on eBay for around five to six dollars. Just search for PL2303 or CP2102.

Introduction
The Arduino board is a great way for beginners to start learning about developing software for microcontrollers; however it’s cost, while a steal when compared to the initial investment required in the past, can be a little too steep for a do-it-yourselfer who wants to do things cheaply. To be fair there are arduino clones that do lower the price a good bit, but the real cheapskates that don’t mind doing a little extra work can’t help but think they could do it for less. For these penny pinching DIYers setting up an ATmega on a breadboard seems like the way to go until they discover the FTDI USB cable is $20. What a heart breaker! There are slightly cheaper alternatives, but a real scrooge googles on. Well look no further because this article has just what you’ve been looking for.

The trick is to use a Nokia cell phone data cable based on Prolific’s PL-2303. These cables have a USB to Serial TTL converter built right in. They’re available on eBay and DealExtreme and very inexpensive. I got mine for just $2.69 shipped. Data cables DKU-5 or CA-42 will work. This article will focus on the CA-42 containing a PL2303HX rev. A. The methods described here should work in general for cables using different chips.

Preparation
Tools:
    Wire Stripper
    Multimeter
    Anti-static wrist strap

Driver:
    Recent versions of Linux have the driver pre-installed.
    Users of other operating systems may find drivers Prolific’s site.
    Install the driver.

So you don’t waste time with a dud cable or bad drivers, you should first check that it’s recognized and assigned a COM port. To do this…

      …in Linux, use the command ‘ls /dev/ttyUSB*’ before and after you plug in the cable. You should notice a new ttyUSB is added after you connect the cable.

      …in Windows, plug in the cable then open up the Device Manager by clicking Start–>Run and typing ‘devmgmt.msc’ followed by Enter. Now find the section named “Ports (COM & LPT)” and click it to expand. There should be a port named something like “Prolific USB-to-Serial Comm Port (COM#)” or perhaps “NOKIA… (COM#)”. Unplug the cable; the cable’s entry in Device Manager should disappear.

Precautions
Never work on a live circuit! Always power off the breadboard and always unplug the cable before making adjustments to either; I’ve tried to remind you of this throughout the article but I may have missed a couple of places. There is a possibility you could damage your computer, some other property, injure or even kill yourself! I’ve tried to be very thorough in my instructions, this however is not an excuse for you to turn off your brain. In no way does this paragraph convey all of the various precautions one should take when working with electricity and electronics. Please read the entire article before you begin to modify the cable. Wear an anti-static wrist strap.

Probing
There are most likely several different manufacturers of these cables because the same model will have a different number and color of wires depending on who you buy it from. Therefore we will have to probe the pins of the phone connector to determine which wires are TxD, RxD, and GND. To do this cut the cable a couple of inches (5 cm) from the phone connector. NOTE: I said the phone connector not the USB connector. Most of the cable should remain attached to the USB connector, but you need to leave enough cable attached to the phone connector to be able to strip the wires and attach an alligator clip (or a terminal block if you like). Strip each wire. Set your multimeter to diode/
continuity, and attach the black probe to a stripped wire. Now probe each of the phone connectors pins with the red probe until your meter beeps. Write down the color of the wire and its corresponding pin number from the picture below. Repeat until you’ve matched each color with a pin number. Not all pins will have a connection. You should double check your work because the wires are very thin and the pins are small and close together.

Pin 6 is the cable’s TxD line, pin 7 is RxD line, and pin 8 is ground. For my cable pin 6 was white, pin 7 was blue, and pin 8 was black. I also had two additional wires, one green (pin 3), one red (pin 4). However, I can almost guarantee your colors will be different so you must perform the steps described above. To be positive we’ve identified the wires correctly we can perform a couple of tests.

Testing
We’re now finished with the phone connector, so now we’ll turn our attention to the remaining cable which has the USB connector. Strip the wires which correspond to pins 6, 7 and 8. Set your multimeter to measure voltage. Attach the black cable to the wire which corresponds pin 8 (GND). Attach the red cable to the pin 6 wire (TxD). Make sure none of the wires are touching one another! Plug in the USB connector. The multimeter should read ~3.3V (I measured 3.5V). Unplug the USB cable. Repeat the previous steps for the pin 7 wire (RxD). The RxD line’s voltage should be less than the TxD line. The other wires are not needed for communicating with your ATmega; nonetheless the pin 3 wire measured 0V and the pin 4 wire measured 3.0V. Your cable may not have a pin 3 or pin 4 wire.

For our second test we’ll temporarily turn our cable into a loopback cable and use a serial communication program to see if we’ve identified the wires correctly. Make sure the cable is unplugged! Twist the RxD and TxD lines together. Make sure none of the other wires are touching one another! Plug the cable in.

_In Debian_
You must determine which device name Linux gives to the cable. For me, since I didn’t have any other USB devices plugged in, Linux gave it /dev/ttyUSB0. If you do ‘ls /dev/ttyUSB*’ before and after you plug in the cable you should be able to determine which device name the cable gets by comparing the outputs. I used screen as my serial communications program.

$ screen /dev/ttyUSB0

Press several keys on the keyboard. If they are displayed, then you have correctly identified the TxD and RxD line. If they are not displayed, you’ve done something wrong, reread and retrace your steps. To exit screen type Ctrl-A \.

_In Windows XP_
The Preparation section explains how to find your cable’s entry in the “Ports (COM & LPT)” of Device Manager. Repeat those steps and make a note of the cable’s COM#. Then launch HyperTerminal:

Start–>All Programs–>Accessories–>Communications–>HyperTerminal

If you’ve never used HyperTerminal answer it’s questions about your location.
When the “Connection Description” dialog comes up give it any name you like
(e.g. loopback). In the “Connect To” dialog change the “Connect using:”
drop down to COM# where COM# is the COM number you made note of above. Click OK.

In “Port Settings” change:

    “Bits per second:” to 115200

    “Data bits:” to 8

    “Parity:” to None

    “Stop bits:” to 1

    “Flow control:” to None

Click OK. Press several keys on the keyboard. If they are displayed, then you have correctly identified the TxD and RxD line. If they are not displayed, you’ve done something wrong, reread and retrace your steps.

Using with Arduino
Once you’ve tested your cable you can make it more permanent by adding crimp-on wire connectors with a 1×6 housing. Look at the instructions for the BBB in the links section to see how to arrange your wires the same way as an FTDI USB cable.

Set up your breadboard with the ATmega chip and it’s supporting components. The Auto-reset input can be ignored and capacitor C1 can be left off. Do not hook up your USB cable just yet. For more examples on how to setup your breadboard see the Links section. You can use any of the ATmega chips supported by Arduino, but it absolutely MUST be pre-programed with a bootloader. Several places sell chips with a bootloader; Modern Device seems to give you the best deal.

Apply power to the breadboard. The LED should blink a couple times. If not you should recheck your setup.

Once you’ve got a working setup, you can try out your cable. Make sure the power is off and the cable is not plugged into the USB port. Hookup the TxD and RxD wires as shown in the schematic. Connect the cable’s GND wire to the breadboard’s GND bus. Plug in the cable, power on the device, and start the Arduino IDE. Under Tools–>Serial Port make sure your USB cable is selected; also make sure you’ve chosen the correct ATmega chip. On the toolbar click the up arrow (Open), go to Digital, and choose Blink. Now we’re ready to try uploading.

A downside of using this type of cable is that auto-reset is not available, so you’ll have to do a manual reset to upload. To do this press down and hold the reset button, click Upload, count off X seconds then release the reset button. If you’ve gotten the timing right, Arduino will say “Done uploading” and the LED will blink continuously. If you release the button too soon the bootloader will timeout before the program gets a chance to be uploaded and you will get an error. If you hold the button too long Arduino will eventually give up and report that the programmer is unavailable. I said “X seconds” above because the timing depends on how fast Arduino runs on your computer. Using a dual boot machine, in Debian I had to count off approximately 5 seconds and in Windows XP I counted only 3. You’ll have to use trial and error and a bit of patience to figure out the right X for you. Getting the timing right can be tricky because the bootloader only listens for a brief period of time. Once you’ve got the correct timing for a manual reset, that’s it, you’re done. Congratulations, you’ve got a basic USB to Serial TTL cable. The next section explains how to make it a lot nicer.

Further Modifications (Optional)
Since these modifications depend heavily on the version of the PL-2303 used and the layout of it’s PCB, this section should not be considered a guide on how to modify your board but as a demonstration of how I modified my board. Earlier in the article I mentioned that my cable had too extraneous wires. I hope you were so lucky as well, because one can be soldered to the USB VCC 5V pad which will allow you to provide USB power to your breadboard, and the other can be soldered (indirectly) to the PL-2303HX’s DTR pin giving you auto-reset! The picture below shows the location of the USB VCC pad and a via that connects to pin 2 of the PL-2303HX (pin name DTR_N). Please note that the section showing the bottom of the board was vertically flipped in GIMP to give a sort of X-ray vision effect. That is, if the top section was overlaid on the bottom all the vias would line up. In reality, when I look at the bottom of the board it does not appear as such.

The casing around the USB connector is not a solid piece but a two part shell. It is easily removed by wedging a spudger (I used a guitar pick) into the seam and gently working the two pieces apart. The shell can also be put back together with little effort. The extraneous wires are colored red and green. I did not unsolder them for fear of breaking some unknown connection, rather I simply clipped them at their pads. There was not enough slack in the wires to reach the new solder points so I removed a section of the cable’s insulation below the strain relief and gently pulled a little slack into the enclosure. (The cable’s foil had to be torn to pull the slack. This is not a big deal since the foil has no connections. You may want to look into connecting the foil to the USB connector’s shield; I didn’t bother.)

Then I soldered the red wire to the square pad to get USB 5V; followed by a quick test providing power to an LED from the cable. Et voila, it works! Unfortunately the cable reports itself as a low power device. This means the maximum current your circuits should draw is 100 mA minus the current required by your cables circuitry. The typical/max operating current required by the PL-2303HX is 20/25 mA. If you try to exceed 100 mA, bad things can happen. My computer limits the current and will not supply more than 100 mA to a normal circuit, though I did blow my meter’s 250 mA fuse when I shorted power and ground. Your computer may beep, reset or even smoke. A (resettable) fuse is highly recommended. See this page for a quick guide on how to check your USB device’s “Power Required” in Windows. In Linux, use “lsusb -v” and search for the “MaxPower” entry associated with your cable to get the same information. If you’ve found a cable that has a “Power Required” of 500 mA, please leave a comment on where you bought it. Auto-reset works by connecting the DTR line to RESET through a capacitor and pulling it low thereby temporarily lowering RESET too. In the picture above you can see that the via just below the silk screened “C6” probably leads to the DTR pin. I did a quick probe to confirms this, then attempted to solder the green wire to the via. It was a bit of a headache. The picture is deceptive; things are much smaller than they appear! What ultimately worked for me was to fill the via with solder, tin the wire, then place the wire over the via and the iron directly on the wire. It’s not a great or even good joint, but auto-reset works nonetheless.

Quite sometime after I first wrote this guide I wired a 24L02 EEPROM to the PL2303HXA. I successfully wrote new device configuration registers settings to the EEPROM using this program. The device configuration registers allow you to change settings like the VID, PID, serial number, and maximum power required (i.e. max. current). I think only the PL2303HXA supports writing to an external EEPROM. Two hints that you have a PL2303HXA are the presence of a crystal and two 4.7k resistors.

Level Translation Note
You may have noticed that the cable’s output level isn’t 5V; it’s 3.3V. So how does it work when the ATmega expects 5V? Well if you’ll look at the ATmega datasheet you’ll see that the minimum input high VIH is 0.6*Vcc, which is 3V when Vcc is 5V. So even though the cable puts out only 3.3V the ATmega still recognizes it as a high. Going the other way, the ATmega TxD pin puts out 5V which is possibly too high for the PL-2303 chip to receive without being damaged. This is why a voltage divider was put on the cables RxD line; 5V*(15/(10+15)) = 3V. For the PL-2303HX rev. A the voltage divider is not necessary because it’s RxD pin is 5V tolerant. Other chip versions may not be; Check their datasheets. If you have a chip with a 5V tolerant RxD pin you’ll get better performance if you leave out the voltage divider because of the inherent parasitics of the resistors.

Request for Comments
Odds are that not all of you will get exactly the same cable that I did, so please share your unique experiences and insights in the comments section. They might help someone else out. Thanks for reading. Good luck!

Acknowledgements
I wish I could say the idea for this project was all my own but most of my inspiration came from Tim Small’s page

Tim Small also provided the picture with the phone connector’s pinout.

Links
Tim Small’s Guide
A Similar Guide
Another Guide Using DKU-5
Data Cable Pinout
FTDI FT232RL Cable
USB Devie “Power Required” Info
Level Translation Article
Arduino Breadboard Setup
ATmega Standalone (Breadboard)
Really Bare Bones Board Arduino Instructions
CA-42 at Deal Extreme
PL-2303 Drivers
PL-2303 Datasheets
ATmega328P Datasheet

Advertisements