Nov-12th-2008

RS232 Communication – The Basics


RS232 is a asynchronous serial communication protocol widely used in computers and digital systems. It is called asynchronous because there is no separate synchronizing clock signal as there are in other serial protocols like SPI and I2C. The protocol is such that it automatically synchronize itself. We can use RS232 to easily create a data link between our MCU based projects and standard PC. Excellent example is a commercial Serial PC mouse (not popular these days, I had got one with my old PC which I bought in year 2000 in those days these were famous). You can make a data loggers that reads analog value(such as temperatures or light using proper sensors) using the ADC and send them to PC where a special program written by you shows the data using nice graphs and charts etc.. actually your imagination is the limit!

Basics of Serial Communication.

In serial communication the whole data unit, say a byte is transmitted one bit at a time. While in parallel transmission the whole data unit, say a byte (8bits) are transmitted at once. Obviously serial transmission requires a single wire while parallel transfer requires as many wires as there are in our data unit. So parallel transfer is used to transfer data within short range (e.g. inside the computer between graphic card and CPU) while serial transfer is preferable in long range.

As in serial transmission only one wire is used for data transfer. Its logic level changes according to bit being transmitted (0 or 1). But a serial communication need some way of synchronization. If you don't understand what I mean by "synchronization" then don't worry just read on it will become clear.

The animation below shows you how a serial transmission would look like (if you can see electricity).

rs232 communication  using USART of AVR MCU

Fig- A Serial Line.(HIGH=RED & LOW=WHITE)

 

Can you make out what data is coming? No because you are not synchronized. You need a way to know when a new byte start and when a bit ends and new bit start. Suppose the line is low for some time that means a '0' but how many zeros? If we send data like 00001111 then line is first low for some time and high after that. Then how we know it is four '0's and four '1's?

Now if we add another line called the clock line to synchronize you then it will become very easy. You need to note down the value of data line only when you see the "clock line" high. Lets understand this with help of an animation.

rs232 communication  using USART of AVR MCU

Fig- A Serial Line.(HIGH=RED & LOW=WHITE)

 

Now you can see how the "clock" line helps you in "synchronizing" the incoming data. In this way many serial busses like SPI and I2C works. But USART is different in USART there is no clock line. So it is called UART - Universal Asynchronous Receiver Transmitter. In USART a start bit and stop bits are used to synchronize the incoming data the.

RS232

In RS232 there are two data lines RX and TX. TX is the wire in which data is sent out to other device. RX is the line in which other device put the data it need to sent to the device.

rs232 communication  using USART of AVR MCU

Fig- RS232 transmission. The arrows indicates the direction of data transfer. In addition to RX/TX lines there is a third line i.e. Ground (GND) or Common.

 

One more thing about RS232. We know that a HIGH =+5v and LOW=0v in TTL / MCU circuits but in RS232 a HIGH=-12V and LOW=+12V. Ya this is bit weird but it increases the range and reliability of data transfer. Now you must be wondering how to interface this to MCUs who understand only 0 and 5v? But you will be very happy to know that there is a very popular IC which can do this for you! It is MAX232 from Maxim Semiconductors. I will show you how to make a level converter using MAX232 in next tutorial.

As there is no "clock" line so for synchronization accurate timing is required so transmissions are carried out with certain standard speeds. The speeds are measured in bits per second. Number of bits transmitted is also known as baud rate. Some standard baud rates are

  • 1200
  • 2400
  • 4800
  • 9600
  • 19200
  • 38400
  • 57600
  • 115200
  • ... etc

For our example for discussion of protocol we chose the speed as 9600bps(bits per second). As we are sending 9600 bits per second one bits takes 1/9600 seconds or 0.000104 sec or 104 uS (microsecond= 10^-6 sec).

To transmit a single byte we need to extra bits they are START BIT AND STOP BIT(more about them latter). Thus to send a byte a total of ten bits are required so we are sending 960 bytes per second.

Note: The number of stop bits can be one or two (for simplicity we will be using single stop bit)

There is one more bit the parity bit but again for simplicity we would not be using it)

RS232 Data Transmission.

The data transfer is done in following ways

Transmission

  1. When there is no transmission the TX line sits HIGH (-12V See above para) ( STOP CONDITION )
  2. When the device needs to send data it pulls the TX line low for 104uS (This is the start bit which is always 0)
  3. then it send each bits with duration = 104uS
  4. Finally it sets TX lines to HIGH for at least 104uS (This is stop bits and is always 1). I said "at least" because after you send the stop bit you can either start new transmission by sending a start bit or you let the TX line remain HIGH till next transmission begin in this case the last bit is more than 104uS.
rs232 transmission and reception  basics

Fig- Data Transmission on RS232 line.

 

 

Reception

  1. The receiving device is waiting for the start bit i.e. the RX line to go LOW (+12V see above para).
  2. When it gets start bit it waits for half bit time i.e. 104/2 = 51uS now it is in middle of start bit it reads it again to make sure it is a valid start bit not a spike.
  3. Then it waits for 104uS and now it is in middle of first bit it now reads the value of RX line.
  4. In same way it reads all 8 bits
  5. Now the receiver has the data.
rs232 transmission and reception  basics

Fig- How the Receiver receives the data on RS232 RX line

 

To be continued ...

Ok friends that's it for now meet you in next tutorial. Till then good bye!

If you are facing any difficulties in understanding the topic, feel free to post a comment and I will surly help you as soon as possible.


19 Responses to “RS232 Communication – The Basics”

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

    [...] to the third part of my 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 [...]

  2. 2
    shiv sambhu Says:

    its very very interesting…

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

    [...] RS232 Basics [...]

  4. 4
    Ritesh Says:

    Its very difficult to find the next tutorial, Is it possible for you to give the link of next tutorial when some tutorial finished . In the its only written this tutorial is finished meet in next tutorial but how to go for next tutorial is not given . CAN YOU PLZZZZ GIVE LINK FOR NEXT TUTORIAL not only for this but for all tutorial

  5. 5
    Avinash Says:

    Hello,

    Go to “AVR Tutorials” section from the right sidebar navigation. There are all tutorials are listed.

  6. 6
    dinesh Says:

    thank you very much.

  7. 7
    Pankaj Says:

    gud. thanx

  8. 8
    AVR ATMEGA8535 : USING USART [SERIAL COMM] « My English Blog Says:

    [...] http://extremeelectronics.co.in/avr-tutorials/rs232-communication-the-basics/ [...]

  9. 9
    Rohit Says:

    good tutorial.

    any code for debounce problem.

    can you suggest how to use TI cc2500 . i have Atmega 16L kit. it would be nice of you if you send me any link of Atmega16 schematic with cc2500.

    nice work !!!

  10. 10
    Savio Says:

    Avinash Gupta you are a Guru! I take my hat for you.
    I am 48 years old, and only today i could really (really underlined) RS232.

    Thanks man!
    Its guys like you who make the Internet a great place to spend time and learn.
    …much appreciation, from Sávio Gonçalves – PORTUGAL

  11. 11
    Avinash Says:

    @Savio

    Thanks Mr Savio for your kind words !!!

  12. 12
    Probal Says:

    Thank you so much…really interesting

  13. 13
    vikram Says:

    Avinashji , hats off for u.
    i hav done b.tech. but this type of explain i havenot seen anywhere. may this site live long life

  14. 14
    sumit Says:

    Hi! frnds,
    I have 3+ exp. in embedded domain and I have done lots of communication protocol base projects. I think this documents really gives you better gudeline. I am greatful to gives a thanx to technical person who Involved such great kind of activity.

    Thanx & Regards
    Sumit Gupta

  15. 15
    Gaurav Parida Says:

    there is a minor error i found in the article is that in the reception example
    Quote
    “When it gets start bit it waits for half bit time i.e. 104/2 = 51uS now it is in middle of start bit it reads it again to make sure it is a valid start bit not a spike.”

    well i m pretty sure that 104/2 is 52 and not 51.
    the explanation of the articles are just aweinspiring(awesome+inspiring)and gr8 for beginners in the field of robotics and electronics.
    Lastly,Thank you for your contribution.

    Regards,
    Gaurav Parida

  16. 16
    Bruce Says:

    Thanks for that excellent info. i never understood this comms protocol until now.

  17. 17
    Jon Says:

    Dude! Thanks for this website! I love avr’s and your website and tutorials has helped me tonnes!!!!!!!!!!

    Keep it up!!!

  18. 18
    kamran Says:

    sir, thanx 4 all the info u provided……very useful…..
    my ques is that wat does this mean….
    baudrate=25; when baudrate is 2400….
    wat hav to b writtten for 9600…. and by which criteria…
    regards

  19. 19
    Avinash Says:

    @Kamran Where it is written ???

    “baudrate=25; when baudrate is 2400″

Leave a Reply

Comments

    • Del Chann: Hello is it possible to use this board to interface with a smoke sensor, motion sensor,...
    • Elijah Nicolia: May I consider part regarding your main guide to my personal site
    • Tifany Wiebusch: I go along with you actually, I believe! Might this become doable for you to have...
    • 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...

Video