How to build a simple TTL to RS232 adaptor for your microcontroller projects. You should be able to scrounge all the parts you need or buy them at your local Radio Shack. In this article I explain how the adaptor works and provide many links to more information you need to know as a microcontroller hobbyist. This article should be very helpful to those that receive the free Arduino compatible kit offered by uC hobby as part of the Arduino Microcontroller kit giveaway.
Just about every microcontroller available today includes at lest one UART or serial port. Serial ports are the most common way to communicate with a uC or other devices such as GPS receivers. The most common interface type is RS232, only a simple conversion from the logic level signals of the uC serial RX and TX pins is required to work with RS232.
For a detailed understanding of RS232 and serial interfacing in general, go to Beyond Logic, the mother load for interfacing information. From this point forward I will assume you know enough about serial communications and will focus on the why and how for the TTL to RS232 adaptor.
The easiest way to make an RS232 interface is with an interface IC, the MAX232 for example. There are a great number of different dedicated chips available with cost ranging in the $1-$3 range. You simply add a few capacitors and you have a reliable RS232 interface. For hobby work you can buy a versatile RS232 adaptor from SparkFun for about $7 as a kit. The picture shows the kit parts and the assembled adaptor. I highly recommend this kit if you want to solve this problem quickly and at little cost.
The basic problem we have to solve is that the uC UART is a logic level device and RS232 is not. To send data over RS232 the voltages need to be about +/-10V while the output at the uC will be 0-5V or 0-3.3V. To see a zero signal the receiving RS232 device needs to see a negative voltage. We also need to invert the phase of our logic signal so that a 1 (5V) is converted to the negative voltage (-10).
Typically the threshold for 1 vs. 0 in the RS232 receiver is slightly above 0V we can get away with using less then +/- 10V. +/-5V should be sufficient for almost any devices. We usually have +5V available in our electronics projects but the negative voltage is a problem.
After doing a quick search on the net I found that many TTL to RS232 adaptors use a common two transistor circuit. I have recreated the schematic for this circuit and will explain briefly how it works below. This is the circuit used in the recommended SparkFun kit.
This is basically the same schematic provided by SparkFun with some re-arranging to make it easier to explain. Each major section is enclosed in a dotted line with a name for that region. Each of these regions is explained below.
This shows the DB9 connector which would connect to your PC. The PC has a male connector so you would need a female if you built this circuit with an integrated cable. I recommend a DB9 female connector so that a straight through serial cable could be used. While there are many other signals available on the serial port we are only concerned with Ground, RX, and TX. Ground is the signal reference against which all other signals are measured. RX is the serial data input to the PC. The voltage levels here should be switching between +/-10V. We will get away with -10 and +5 for this circuit.
This is the tricky part of the adaptor. The major problem is getting a negative supply to feed back to the RX input. This circuit steals that negative voltage. The diode D1 is arranged so that the negative voltage output by the PC is used to charge the capacitor C1. Note that C1′s + input is connected to ground. C1 will store up the -10V output by the PC so we can use it to drive the RX input to a negative voltage. D1 is necessary because the TX output will not always be at negative voltage. It will transition to +10 as data is transmitted and this would quickly discharge C1 if the diode did not limit the direction of current flow. With D1 in place, only the negative voltages at the TX pin will be stored.
RS232 to TTL
This is a simple transistor switch which is turned on when the TX output goes positive. Fully explaining how a transistor works is beyond the scope of this short article but I am sure you can learn more on the net. I will say that the NPN transistor, Q2, used here turns on when it’s base pin (on flat symbol side to the left) is pulled up over it’s emitter pin (with the arrow pointing toward the bottom). When the transistor sees the +10V from the TX pin it turns on and pulls down it’s collector (top of the symbol). By pulling down (towards ground, the signal on the emitter), the voltage drop across the 10K resistor is increased so that the TTL RX signal is brought very near ground or 0V. This will be a valid 0 signal to our uC. When the PC RS232 TX voltage goes negative, Q2 will turn off and the 10K resistor R1 will pull the voltage up to VCC (+5V) which will be a valid signal to the uC. Notice that the +10 to -10V RS232 signal is inverted in phase and changed in level (0 to +5V) to the TTL voltages required by the uC. The voltage will not go all the way to 0V at the collector because the transistor will have some voltage drop between it’s collector and ground. But it should go low enough to satisfy the logic low level input requirements of our uC.
TTL to RS232
The second transistor is a PNP type and will turn on when it’s base is below it’s emitter in voltage. This means that when our uC outputs a logic 1 or 5V the transistor will turn off because it’s emitter is connected to VCC (+5). Since the base is not below the emitter (tied to +5) the transistor will turn off. Note the difference in how the transistor is turned on is because one is PNP an the other is NPN. You can tell the type by the direction the emitter arrow is facing. To help me remember I look at the symbol and to see if it’s P-in-P as “in” is the direction the arrow is facing.
Q1 turns on when the uC outputs a logic 0 (0V) on it’s TX pin, because it’s base is lower in voltage then it’s emitter. When the PNP transistor is on it pulls up it’s collector, toward the emitter which is tied to +5. This inverts the uC output and feeds +5V to the PC RS232 input. When the uC outputs a logic 1 (5V) the transistor turns off and the 10K resistor R3 pulls the signal down to the negative voltage built up in the negative supply across C1. The negative voltage signals the PC RS232 and satisfies the negative voltage requirement of the interface.
This optional part of the circuit drives some LED indicators from the logic side of the adaptor. When the uC pins are at logic 0 the LED will light up. As serial data is transferred the LEDs will flash which is a nice debugging aid.
Flaws in this description.
I have greatly simplified how a transistor works to a general form I use when quickly scanning a design. I am sure to have missed several points that some of the experts will no-doubt point out in the comments. I welcome these comments and hope that any flaws will be discovered. The most likely concern will be that the transistors can not pull the signals to 0V as there will be a required voltage drop between the collector and emitter. You should see the voltage drop at about 0.5V if your transistors are saturated with current from the base. If you have trouble with the interface you should check the low logic voltage levels at Q2. if it’s not 0.5V or less you may need to lower the value of R4 or find a better transistor. This will not be an issue if you use the recommend SparkFun kit.
Modern Device Company Arduino Compatible kit.
The Arduino Compatible kit from Modern Device Company does not have a standard serial port interface so connecting it to your development PC or other devices may require the adaptor described above. New PCs are not including the old RS232 interface and if your PC does not have this interface you will need to use USB. You could purchase a USB to RS232 serial device which would be a versatile solution that would allow you to interface any RS232 device to your PC or you could purchase a USB to TTL serial interface. The Modern Device Company Arduino Compatible kit has a header designed to mate with the USB to TTL adaptor available from their site or from Mouser Electronics for about $20. You will be able to use this adaptor on any of your future hobby projects so it is a good option to consider.
All the parts you need are probably sitting in electronics devices you could scrounge. You don’t have to have 2N3906 or 2N3904 transistors to make this circuit work. These transistor parts are very common so it’s possible that you will find exactly these devices. Look for the small black (T0-92) packages in electronics scrap and remove them. You will need a PNP and a NPN signal transistor. Many Digital Multi-Meters (DVM) have a built in transistor checker you can use. Try and find transistors that check out with a beta of at least 100. I hope to do an article about testing scrounged transistors soon. Maybe someone will do this article to receive a free Arduino kit.
The other parts such as a diode and LEDs should also be very easy to find. The resistors could be more difficult. You will no doubt find plenty of them but they may not have leeds long enough to work with. The best solution here my be to buy a resistor kit. I recomend a kit from All Electronics. If you know of a better kit please let us know in the comments.
If you find this article useful please comment. I would like to do many more articles like this one but do not know if the uC Hobby audience is interested. Also if you have some related information please let us know with a comment.