Nov-13th-2008

RS232 Communication – The Level Conversion


Hello and welcome back. Continuing our discussion on RS232 serial communication in this part we will make a RS232 level converter. In the last tutorial we saw that how RS232 level signals differs from normal logic signals. So to interface RS232 level signals to our MCUs we need a "Level converter". And in this tutorial we will make one.

What a level converter will do is to convert RS232 level signals (HIGH=-12V LOW=+12V) from PC to TTL level signal (HIGH=+5V LOW=0V) to be fed to MCU and also the opposite.

rs232 level convertor using max 232

Fig - Working of RS232 level converter

 

 

 

As RS232 is such a common protocol there is a dedicated IC designed for this purpose of "Level Conversion". This IC is MAX232 from Maxim. By using charge pumps it generates high voltages(12V) and negative voltages(-12V).

Now lets make it!

Things you need

S.No
Item
Value
Qty
1
1
2
Capacitors
1uF
4
3
10uF
1
4
DB9 Female Connector
1
5
General Purpose PCB
1
6
Some Wires
-
-

 

Stuffs required for RS232 level Convertor

Fig - Stuffs required for RS232 level converter.

 

 

Now having all the stuffs in our working table lets begin.

The Schematic

rs232 level convertor using max 232 schematic

Fig - Schematic for RS232 level converter.

 

 

Assembly

Assemble the circuit according to the schematic on a small piece of general purpose PCB. Take out two wires for the power supply(5V) and two wire that connects to the MCUs RX/TX lines. Connect a DB9 female connector with longer wires because it connects to your PC. But don't make it too long keep it within 1.5 meter to 2 meter that will be enough.

Take care while connecting the DB9 connector wire must be connected to proper pins as shown above. To help you the connector has pin numbering on it.

db9 for USART

db connector for rs232 communication

Fig - DB9 PIN Configuration.

 

 

After soldering wires put the plastic covering.

making a level converter using max232

 

Fig - DB9 with Cover

 

 

Now the connector can be easily connected to your PC's COM port (Serial Port).

making a rs232 level converter

rs232 level converter

 

Fig - Fully assembled level converter.

 

 

Testing

It is always better to check each module separately. So we will test our converter to see if its working fine. For testing we will use a Hyperterminal a Windows software that can be quickly used to open COM ports and send and receive textual data. Right now you don't need a MCU or any MCU programming. The theory of testing is that we will connect output (RX/TX) together so any data written to COM port enters our circuit get converted to TTL level and loops back and enter MAX232 get converted to RS232 level and enters COM port, that's it.

making and testing max232 based level converter

Fig - Loop back testing.

 

 

 

Now open hyperterminal from Start Menu (All Programs>Accessories>Communication>Hyperterminal)

  1. Enter the name of connection say "testing" and select any icon for it.
hyperterminal

Fig - Hyperterminal Main Window

 

 

2. Select your COM port in the "Connect Using Drop Down List". Note your PC might be having more than one COM port but commonly only one is available outside the rest are connected internally to modems etc.

hyperterminal

Fig - Select COM port.

 

 

3) Select

Bits Per second = 9600

Data Bits = 8

Parity = None

Stop bits = 1

Flow control = None (important)

To know what they mean see previous tutorial ().

4) Now the HyperTerminal is ready. Make sure that Hyperterminal has the input focus and type something on the keyboard, they should echo on screen. Now disconnect the RX from TX and do the same. This time you should not see any thing on screen. Now your circuit is ready and working correctly.

If the characters are not echoing to screen your circuit is not working as expected. Check your connections and try changing the COM port in Hyperterminal.

Note: the circuit should be powered up(by 5V supply) during testing.


39 Responses to “RS232 Communication – The Level Conversion”

  1. 1
    OKORO CHARLES Says:

    Thank u very much for ur wonderful tutorial, I enjoyed myself, may GOD continue to encourage u.
    My question is this what is MCU I need further explaination on this.
    Thanks

  2. 2
    Avinash Says:

    Hi Charles,
    Thanks, The MCU is the Mircocontroller. It is a single chip computer. An example is AVR series from Atmel. They are easy to use and this tutorial series is dedicated to them. More info on MCUs is here

    http://extremeelectronics.co.in/avr-tutorials/part-i-introduction/

    http://extremeelectronics.co.in/avr-tutorials/part-ii-getting-started/

  3. 3
    Using the USART of AVR Microcontrollers. | eXtreme Electronics Says:

    [...] RS232 serial communication tutorial. Till now we saw the basics of RS232 communication and made our level converter. Now its time to understand the USART of AVR microcontroller and write the code to initialize the [...]

  4. 4
    Using the USART of AVR Microcontrollers : Reading and Writing Data | eXtreme Electronics Says:

    [...] See previous tutorial for calculating UBRR value. The MCU should be connected to PC using a RS232 level convertor. In PC you can run hyper terminal to send/receive data via usart. Start hyper terminal and press [...]

  5. 5
    deen Says:

    Hello,
    i’ve followed all steps instructed above, but unfortunately, when i run the hyperterminal, I can’t write (can’t type any char with my keyboard) in hyperterminal window to send data to MCU..
    how to fix this problem avinash??..plz

  6. 6
    Avinash Says:

    Hello Deen,

    Hyperterminal by default does not echos the locally typed characters. So characters are transmitted but not shown on screen. The screen only shows the received characters.

    But you can easily modify this behavior.

    First tell me that have you connected the MCU at other end or just using the level converter and performing a loop back test ?

    If you have used a MCU what program you have on it?

  7. 7
    deen Says:

    i just connecting the MCU using level converter, & send data from PC to MCU..to control motor speed..

    if I enable echos, what will happen..?
    as you said that typed char does not shown on the screen, but my motor speed does not change as desired..

    is there anything that i should set/enable b4 using hyperteminal or normally juz working good as you instructed above?

    and may i ask what is the function of ‘flow control’?

    thanx avinash…

  8. 8
    Avinash Says:

    Hi Deen,

    I recommend to to first check everything separatly. Go for following steps

    ->Check if you are sending command to right serial port. Short PIN 2 and PIN3 and type on Hyperterminal the character should echo, now remove the short and no echo should occur. If this is so then hyperterminal is sending command to the port you are expecting it otherwise may be a internal serial port (may be connected to modem is getting the data).

    ->Check the level convertor as done above.[Result OK/ERROR]

    -> Now check it the MCU is configured and working properly. See this program to test the MCU.

    http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers-reading-and-writing-data/

    -> Now modify the program to control motor. Be sure to test the motor code separatly.

    ->Then combine all these.

    You may send me the result of each step for some help.

    [Make sure "Flow Control" is "none"]

  9. 9
    Avinash Says:

    @Deen

    You can also mail me the code (if you like) so that I can inspect it.

  10. 10
    lakshmi narayanan Says:

    hi
    this seemed to be a useful article please could you assist me on how to get the output of com serial port in the form of zeros and ones into a text file. actually iam interfacing an A/D converter to my computer through rs232 cable i want to know the data bits that is going to appear and i want in a text file. please assist me in this

  11. 11
    Avinash Says:

    @Narayana

    Firt why you want output in form of zeros and ones? When you can get the valus.

    U said you wanna interface A/D to PC, what software u would be running at PC?

    I think you are planning to read a text file (containing A/D value) and process/display the value in some way, am I right.

    For that pls tell me what programming tool u would be using & i will tell u the method to get the value directly from serial port.

  12. 12
    Johnny Says:

    Hi Avinash

    I was looking for some information on how to do an USART interface with a MCU. I’m using PIC16F877A. I want to know how to communicate with the MCU from the PC. I don’t know which software to use, but what I want to do is: connect the MCU with the PC and the MCU will communicate with other MCU via I2C. it is going to be 4 MCU and 1PC. 1MCU will act as an interface with the PC and another MCU will act as a master for the remaining 2 MCUs. I will have to control all of them with the PC.

    I have seen the code you asked DEEN to see, the one on: http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers-reading-and-writing-data/

    I understand the code, but I’m still wondering where do you run that code? is it C# or C or something else?

    please, I would really appreciate some help here!

    thank you

  13. 13
    Avinash Says:

    Hello Johnny,

    1) the code ur talking abt is written in C (AVR-GCC) for AVR MCU and it will be ran on the microcontroller.

    2)I have given NO code for the PC end to keep tutorial simple and concentrate only on MCU specific coding. So I have use a ready made software “Hyperterminal” supplied with Windows. This is just for test. Finally u have to write ur own code in PC as well.

    3) Pls tell be about ur PC programming knowledge i.e. which language/tool (VC++,VB,Delphi,C,MFC,.NET,c# etc) ur familier with so that I can help you with Serial Communication on PC end.

    4) If your a Visual C++ /MFC coder I can give u a library for serial communication.

    5)All the codes in my site is specific to AVR series of MCUs from Atmel & I currently don’t have any material on PIC MCUs (On which u r workin)

    We can discuss this further on forum

    http://forum.eXtremeElectronics.co.in
    For any doubt pls feel free to post.
    :)

    A

  14. 14
    Johnny Says:

    Thank you very much Avinash.

    I’m familiar with C, so I think I can handle C++. I have used bloodshed C++ as the compiler, but I can get visual studio too. please tell me which version of visual basic to get and that will be great to have your libraries.

    I already did the coding for the PIC MCUs, now the hardest part for me is how to communicate with the PIC from my computer.

    thank you very much for your help.

  15. 15
    Avinash (Site Admin) Says:

    Hello Johnny,

    So its clear that you r comfortable with C using Bloodshed Dev C++. Thats fine no need to get Visual Studio. But to program WinXP in C/C++ you need the knowledge of either the Windows API or MFC or Toolkits like wxWidget. Do you have any knowledge of using Windows API? or You r just familiar with text based programming (printf(),scanf() etc)

    You can search the net for tutorial on serial communication in C. But all the result will require you to have knowledge of Win API or MFC.

    see
    http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c5395

    If you can mail me a brief requirement of you app then I can give you some suggestion or if the application is small i will develop it for u!
    :)

    A

  16. 16
    Johnny Says:

    Hi Avinash, it is great to hear about you again.

    I’m not quite familiar with Windows API or MFC…,and yes I’m text based programmer. I have also used JAVA before too. My Email address is jkzh0429@gmail.com you can email me or just add me to your google talk too.

    And about the program I need, I think it is quite simple. I will connect 3 PICs, one will be the master which is going to communicate with the PC and the other two PICs. The connection to PC-PIC is going to be through USART and the PIC-PIC is going to be through I2C.

    I will 1 Master and 2 Slave. One slave is going to have a temperature sensor and the other one will control the intensity of a light bolt via PWM. So the PC program will just have to tell the user the current temperature and be able to increase-decrease the intensity of the light.

    Please Avinash, give me as much advise as you can. This is the first time I try interfacing something trough serial with a PC.

    Thank you so much!!

  17. 17
    Chaitra Says:

    hi avinash,
    really good tutorial.i would request u to post more in depth tutorials.what ive observed is dat u give the basics n leave it at that.i would request u to give us,your readers,more insight into each topic that u deal with.i do realize that its tedious,but u can atleast tell us about topics that v can explore for ourselves,u can make a mention of these in ur blog..
    plz consider my suggestion.neverthless,u make excellent e tutorials and keep it coming.thnx a lot for ur time..
    bye

  18. 18
    Avinash Says:

    Hello Chaitra :)

    Thanks. I will go into more in depth once I complete the basics. I mean basic intro to all things “AVR” chips has in it.
    I again thank you for your feedback, they are most valuable for improvement of contents. I also request other users to give feedback, just anything they thing. I would be very glad to hear.

  19. 19
    Devansh Says:

    Hey Avinash, i can not thank you enough for taking the pain all this information compiled in the simplest manner possible. It has been of great help to me, and your site is one of the few that is so simple, conscience and yet complete in all respect.

    thank you a lot. keep writing…

  20. 20
    Avinash Says:

    Hello Devansh,

    Thanks a lot for your appreciation. I am happy that many peoples are getting help from my website. :)

  21. 21
    prasad Says:

    Avinash,
    Very intresting. I need a help I want to rotate stepper motor at particular rotations cw and ccw I want to control through computer help what are all I need I am very fresher.
    I also wanted pressure sensor if over load it come to original position. Thanks

  22. 22
    prasad Says:

    Avinash, waiting for reply I don’t have any xperince in electronics but passionated. Help in learning making stepper motor controller controlling with pc through usb/serial port.

    Thanks

  23. 23
    mayank Says:

    hi , i have the made the Rs 232 level converter by following ur algorithm. on connecting Tx and Rx ,i am receiving the characters in hyperterminal.But when Tx and Rx are not shorted,hyperterminal shows some random characters which appear only when i move the wires connected to Tx and Rx.

  24. 24
    Avinash Says:

    Your circuit is 100% percent correct.

    The garbage characters are just noise. They are generated by AC signals around you.

    Now u are ready to connect a MCU at other end

    see http://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers/

  25. 25
    Andrei Says:

    Hy Avinash
    Thanks for these great tutorials, I’ve made this circuit and I noticed that you don’t have the supply links for MAX232(pin 15 -> GND, pin 16 -> +5V)

  26. 26
    Avinash Says:

    @Andrei,

    Yes the Power Pins are missing as the schematic software automatically connect them properly and hide them.

    Thanks for reporting. :)

  27. 27
    Sanket Says:

    hi Avinash,
    i made the level converter circuit as shown in the schematic in your tutorial. first i shorted the Rx Tx pins on the serial port and sent a char using MATLAB and received it back successfully. then i connected the MAX232 circuit to the port and shorted the Rx Tx pins on the max232 circuit but this time when i send data i receive a ‘time out’ error. i am using 57600 baud rate and since my laptop doesn’t have a serial port i am using a usb to serial converter (BAFO BF-810).
    can you please help me.
    thanks

  28. 28
    Ashutosh Upadhyay Says:

    JhamaJam tutorial barse re!

    Very Very Very Very Very goooooood jooooob I learned a lot from these tutorials.

    You are the champians af champians

  29. 29
    Avinash Says:

    @Ashutosh :)

    I am glad to help you all !!! We are proud on our community members like you !

  30. 30
    Ashutosh Upadhyay Says:

    Its your colorful, easily understandable, detailed, accurate, error free tutorials that makes you to proud on me. Please don’t stop to put new tutorials regularly i am not only the person who get helped from that many students under me and from all over the world get help from your tutorials on site.

    I found many tutorials on net but the way you express the things is different from others. It shows your class
    Warm Regards!

  31. 31
    JK BAWA Says:

    avinash ji
    we have a security device for vehicles. it has tiny 26 microprocessor and the warnung indicatores are showen on LEDs. we wish to connect it with gps using RS232, pls help me to do so .
    jagjeet kumar
    bawa

  32. 32
    robodam Says:

    Is the serial comm. possible wirelessly.

  33. 33
    kumarasuhwaran Says:

    sir, good evening. i would like to know whether can we burn the command directly to the PIC18F2550 by using C language or do we to must convert that command to hex file first???

  34. 34
    Avinash Says:

    @kumarasuhwaran

    YOUR QUESTION IS TOO SILLY TO BE ANSWERED ! :(

  35. 35
    Zenlen Says:

    I am facing a strange issue implementing serial communication of the 16f877a pic with a pc. I am using the above tutorial and have the following issue:
    when i use the assembly language everything works fine. I send a string from the pic and received it to the pc. When i use the c language i send the string from the pic and i received stranger characters. do you have any answer why this happens?

  36. 36
    Shouvik Says:

    Hi Avinash,
    Thanx for your fabulous tutorials. I wanted to know if we can use a usb-serial converter to connect the serial port with a laptop. If so then how will the baud rate and other settings modified. I want to interface a AT command compatible phone through RS232 with my laptop.Is that possible.What are the things we should consider. Thanks for your time.

  37. 37
    Shouvik Says:

    I am sorry to post this issue here but I couldn’t find any other suitable space. There is some problem with the forum. Every time i click on a link ( view post,new post,reply) it asks for user name and password so i have to login everytime. Please check this out. Also wouldn’t it be a good idea to use a unified account for the shop and forum!!Cheerz

  38. 38
    Avinash Says:

    usb to serial adaptor is installed as a serial port and works transparently. so baud rate and other settings is done in exactly same way.

  39. 39
    Santosh Says:

    Avinash,
    thanx for ur too much valuable articles. This site is my first preference when searching for an AVR related issue.
    Now the problem is I am using a laptop with no serial port in it. So i am using Prolific USB to Serial COM port. when i used Hyperterminal as per your tutorial I cant get any response. Then I followed guidelines you provided to Deen, but all in vein. I failed in very first step. So I want to know whether it is problem with USB serial converter or any thing else. I had also checked my COM no. from the device manager. Please help.

Leave a Reply

Comments

    • Sinopteek: When i try to run this software under OpenSuSe 11.2 (x64), i’ll have next error:...
    • kapil: @Dhananjay I used that its working and it can programme 3 to 4 times after that there is a...
    • Dhananjay: Sir, At present I am using progisp to flash AT89SXX with usbasp(with modified firmware)....
    • Shashi Jain: plz dont fight wid each other…k i accept my fault… neway m not a rich person...
    • vivek: @Avinash “Not every one is as rich as you to purchase Code Vision Compiler. I can’t...
    • Niclas: PS. you need both (and i suppose its WIN-AVR that includes the gcc not AVR-studio) DS.
    • Niclas: and ofc WIN-AVR, google it and youll find it ^^

Video