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.
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 |
– |
– |
Fig – Stuffs required for RS232 level converter. |
|
Now having all the stuffs in our working table lets begin.
The 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.
Fig – DB9 PIN Configuration. |
|
After soldering wires put the plastic covering.
|
Fig – DB9 with Cover |
|
Now the connector can be easily connected to your PC’s COM port (Serial Port).
|
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.
Fig – Loop back testing. |
|
Now open hyperterminal from Start Menu (All Programs>Accessories>Communication>Hyperterminal)
- Enter the name of connection say "testing" and select any icon for it.
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.
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.
Other Parts of this Tutorial
- RS232 Communication – The Basics
- RS232 Communication – The Level Conversion
- Using the USART of AVR Microcontrollers.
- Using the USART of AVR Microcontrollers : Reading and Writing Data
- Visualize ADC data on PC Screen using USART – AVR Project
Facing problem with your embedded, electronics or robotics project? We are here to help!
Post a help request.
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
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
https://extremeelectronics.co.in/avr-tutorials/part-i-introduction/
https://extremeelectronics.co.in/avr-tutorials/part-ii-getting-started/
Hi,
Avinash
You have a beautiful and rare values: spread knowledge and patience. Thanks a lot
Pingback: Using the USART of AVR Microcontrollers. | eXtreme Electronics
Pingback: Using the USART of AVR Microcontrollers : Reading and Writing Data | eXtreme Electronics
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
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?
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…
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.
https://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”]
@Deen
You can also mail me the code (if you like) so that I can inspect it.
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
@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.
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: https://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
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
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.
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
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!!
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
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.
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…
Hello Devansh,
Thanks a lot for your appreciation. I am happy that many peoples are getting help from my website. 🙂
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
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
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.
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 https://extremeelectronics.co.in/avr-tutorials/using-the-usart-of-avr-microcontrollers/
dear sir
I have set up using Avr Atmega 32A board
with serial port.
connection are like below
Atmega 32A–>MAX232—-MAX232—>Sim900 Module
here my Atmega32A is on 5 volts, and both MAX-232 also on 5volts, I have also connected Laptop using via 232 port as sniff to view response on hyper terminal,
Now problem i m facing is receiving garbage junk as NULL on terminal along response. But when I was using ATMega16L same set up work fine everything work fine,I am using Atmega32A, as code size exceed 16K size.
so overall problem is when using Atmega16L it is fine but when using Atmega32A garbage junk is viewed on terminal.
Please help me
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)
@Andrei,
Yes the Power Pins are missing as the schematic software automatically connect them properly and hide them.
Thanks for reporting. 🙂
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
JhamaJam tutorial barse re!
Very Very Very Very Very goooooood jooooob I learned a lot from these tutorials.
You are the champians af champians
@Ashutosh
🙂
I am glad to help you all !!! We are proud on our community members like you !
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!
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
Is the serial comm. possible wirelessly.
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???
@kumarasuhwaran
YOUR QUESTION IS TOO SILLY TO BE ANSWERED ! 🙁
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?
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.
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
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.
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.
Pingback: RS232 Communication using PIC18F4520's USART - PIC Microcontroller Tutorial | eXtreme Electronics
Hi Avinash,
Thanks a lot publishing so many high quality articles. I learned a lot from your posts.
I was just thinking that rather than RS232 if I want to connect to USB then how to accomplish it. If you have any link or document then can you please share it?
I know I can use a serial to USB converter and use the current circuit.
Thanks in advance.
Hi Avinash,
This is very informative about USB-serial converters. I too amy trying to use one (BAFO-810) and though the driver installs fine and everything looks good (as far as device settings & status go), the loopback test fails – everything works except Rx. As a result I am simply not able to connect any legacy serial devices to this converter. Is the converter faulty? I would really appreciate it if you could help.
Thanks & regards,
Rohit
Bafo 810 is NOT faulty. I have worked with many of them!
@Avinash:Are u able to transmit through it ?
@Avinash – if the converter is not faulty then why is it not receiving anything? I am using a software called RS232 analyzer to check it – here it shows all signals working except Rx (receive).
In fact I have connected the converter to an external serial device (OBD scanner) – this device can receive signals from the converter (activity LEDs on the scanner flash on receiving valid command) but whatever data the scanner outputs is simply not received. This is consisten with the loopback test (using the above software as well as hyperterminal) – Txed characters are simply not Rxed back.
hey, thank you for your tutorials, can u also tell how we can make usb to serial port conversion
@Ashish
For USB To Serial Conversion See this
http://www.dharmanitech.com/search/label/USB%20converter
Dear,
thanks for this great tutorial..
i just wanted to know if i can use the if i can use the internal calibrated rc oscillator instead of external crystal
and what connections i must use for the attiny2313 when testing it
i supose i must connect the two data wires(RXD & TXD), the GND and VCC
I am planning to use an UART to RS232 converter, can I use this circuit for that? An by the way, the microcontroller that I will be using is an ATxmega32Ad which has maximum operating voltage at 3.6V, can I still use this circuit?
@Joaquin
Although not rated but MAX232 works good in low (3.3v) too!
Just use Vcc as 3.6v same as your ATxmega32Ad
thank you all, you were very helpful
Excellent tutorial! Thanks for the infos!
Thankyou Avinash!
It was fantastic. Especially the test method
Thank you very much
ya tengo el circuito anterior pero al tecleear en la hiperterminal no me deja a q se debe el circuito ya lo hice como 5 veces y no acabo de entender por que puede ser???
Hi, great tutorial!
I just have a question… If I use a USB to RS232 converter, do I really need to use a level converter (MAX232) ? As usb is already designed to use just 5v.. right?
So I wonder if I can connect my pic directly to my usb to rs232 converter.
Thanks in advance!
@Alex you miss the basic. USB to Serial converters are designed to connect legacy serial peripheral to USB port. Since all serial device works on RS232 levels the commercial USB to Serial convertor (with DB9 connector) also converts USB TTL signal to RS232 using a chip similar to max232 so that legacy devices could accept the signal.
So the POINT is that if you use a USB to Serial Conveter (commercial make with a DB9 connecter) you must use max232 as level convertor.
Pingback: RS232 Communication – The Basics | eXtreme Electronics
Can you please mail me the CAN bus tutorial.
@avr noob
NOT at all !
Why??
Had i said something wrong?
Controller Area Network (CAN)
Please sir..i would be highly obliged…
Hi Avinash, I want to write a c code (instead of using a 3rd pirty software) for serial(rs232) comunication with atmega32. I am using a laptop and it dosen’t have a serial port so I have bought USB to RS232 converter from your website. Can you provide me c code example to do so.
Thank you in advance.
Hello Avinash,i have a digitizer board (Lectra systeme) manufactured by lockheeds and calcomp,it is designed to plot co-ordinates for clothing design patterns, with the aid of pc software. Unfortunetly i dont have any cables and not quite sure what cable specification should be, after endless searching and buying various cables and adapters i am no further with getting it up and running. If i send more information and photos from the ports can you advise me? thanks brian
Hello. Great Tutorial
Thanks for the effort.
I just have a (very) basic question
I built the circuit as you showed, and i am now connecting it to a PC. I want to perform the autotest (pins 13 and 14 connected) to see if it is working, so I run the hyperterminal.
at this point, what is what I should see in the hyperterminal to know it is working or not. COuld you specify please?. I ve tried both cases (pin 13 and 14 connected and disconnected) and the behavior is the same. I just type stuff and it appears in the screen.
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 received nothing. i am using 9600 baud rate . i also took all the steps carefully.
can you please help me.
thanks
Thanks for the steps of Hyper terminal settings.
Pingback: Error while serial communication between PC and microcontroller
Hi, Avinash. I used this circuit but there’s some problem while testing it. I am not getting the same characters that I am inputting into the Hyperterminal. I am instead getting some strange characters ( See image : http://i.imgur.com/a8PKt.png ). Can you explain why ?
Aakash
You have to change the configuration of the hyperterminal. Probably the bits per second. It happened to me until I realized that it was just that.
Hi Kansai,
Thanks ! Lowering the baud rate helped ! 😀
Respected Sir, u have mentioned DB9 female but in the schematic u have connected DB9 male connector, is the configuration same for both???
There are pin numbers written on the connector. Just follow the Pin numbers written on connector. It does not matter, either you use male or female connector.
Good Answer Imtiyaz
In newer version of Windows Hyperterminal is not available , It has to be downloaded seperately.
In that case I suggest these alternate( and free ) and more useful softwares to consider
1. Clear Terminal ( http://www.clearconnex.com/content/clearterminal )
2. Realterm ( my personal favorite – you can see signals on pins like blinking leds )
(http://realterm.sourceforge.net/)
3. Teraterm ( http://ttssh2.sourceforge.jp/index.html.en)
4. Coolterm
hi…i was trying to conduct loop back test..but instead of echoing back the ascii letters i write …i got these wierd words
“zôúúújýýýúúúÊúýúýýûûûûûû öýûûû±ûýòû±µ ö ûýò ëûòxûûûööû ìûûòû ûýû í ûûûû
û û û û컶ð÷÷÷÷÷÷÷÷÷÷÷÷÷÷À€ð”
so i tried to find problem so disconnected the power from max232 and i still got these characters..but less in numbers..
so i checked my serial port by connecting 2 n 3 ..it works
but with max232..it doesnt
can u please help..?
hi
When we convert the signal from RS-232 to TTL,what will be the speed of the data? whether speed will change or it will run at same speed
@senthil kumar
after reading the whole article still you have that doubt?
dear avinash
it’s great informative website!
I want to Communicate between weighing machine rs232 output with PIC 18f4550 to directly show the valve of Weight and to calculate on the Weight (5 digits 000.00) but only consider on 000 first three digits for calculation
plz share ur idea
I’ve been beta testing a program called Serialware Advanced. It is software that lets you design your own control panels that send rs232 signals via serial port on the computer. No programming skills are needed. It’s all just point and click. Hope to use it to control a serial device I’m planning to make create using this schematic.
So far I’ve used it for existing devices like a projector or TV. But now I’m looking into creating some of my own hardware as well. Thanks for giving me a push in the right direction here.
Cheers,
Gregg