Jan-17th-2010

RF Communication Between Microcontrollers – Part III

Welcome to the 3rd part of RF Communication tutorial. In the last two parts I have introduced the basics of RF Communication.

Part III will be covering mostly the practical part, i.e. we will build a complete & working data transfer system. Here you will get circuit and program to implement the solution. The application is very simple in this case, just to transfer a byte of data from Tx station to the Rx station. Once you implement it and get it working you will have enough information and experience to make other RF based projects.

I request all users to follows the instruction exactly as given (unless they are smart enough to know what they are doing). The most important thing in this article is timing of the MCU, so

  • Use the exact frequency crystals as used in the designs.
  • Write High Fuse = C9 (HEX Value) and Low Fuse FF (HEX Value) to enable external crystal.

Hardware

We will have two units. One is Tx (Transmitter) and Other is Rx (Receiver). Both units are based around ATmega16 MCU(you can use ATmega32 also) on external 16MHz crystal. On the Tx unit PORTC will act as input. While in Rx unit it will act as output. The value at PORTC of TX unit is constantly sent over the air to the RX unit where it is latched on its PORTC. That means whatever value you put in the PORTC of Tx station is available on PORTC of Rx station (8bits or 1 byte). We will connect 8 micro switches on the PORTC of Tx station and 8 leds on the PORTC of Rx station. For testing you can press keys on the Tx side and corresponding LED on the Rx side will glow. Simple!

You can then use the same techniques of sending/receiving data in any other application, like SWARM robotics.

RF Module Interface with AVR ATmega

RF Module Tx + AVR ATmega16

 

AVR Atmega interface with RF Module

RF Module Rx + AVR ATmega16

The above two schematic gives detailed connection of AVR ATmega16 (or ATmega32) MCU with RF Modules.

Note About the Schematic

  1. Power PINs of MCU are not shown but must be connected properly.
  2. BC548(NPN) transistor and two 4.7K resistor forms a very simple NOT gate(inverter). This is because in RS232 communication the "idle" state is HIGH to make idle state LOW we have use the inverter.
  3. You can use a low cost ready made avr development board for quick and easy experimentation.(like this one). It has inbuilt power supply, crystal, reset circuit and ISP port(and much more) Only you have to connect the transistor and RF modules that's it! You can easily program the MCU by using USB AVR Programmer.
AVR ATmega16 RF module circuit

RF Communication Test

Software

The software is written in C language and compiled using the open source compiler avr-gcc. For project management AVR Studio was used. I have used my fully buffered, interrupt driven USART library for usart related job. The library comes in two files.

  • USART.c
  • USART.h

These files must be copied to the current project folder and added to AVR Studio Project. How to add a file to avr studio project is given here.

Some important functions of USART library are as follows

USARTInit();

Initializes the USART of AVR MCU. The parameter is the UBRR value. What is UBRR? Its not the topic of this article! I assume that you know about the Basics of RS232 communication and have knowledge of USART. Don't worry much if you don't know that. Just leave this article and read the following articles

UWriteData();

This function sends a byte of data over the Tx channel.

UDataAvailable()

Returns the number of byte of data currently waiting in the fifo queue.

UReadData()

Reads and returns a byte of data from the buffer.

Complete Listing of Tx.c


#include <avr/io.h>
#include <util/delay.h>

#include "usart.h"

void main()
{
   //Initialize the USART with Baud rate = 2400bps
   USARTInit(416);

   //Enable Internal Pullups on PORTC
   PORTC=0xFF;

   /* 

   Keep transmitting the Value of Local PORTC
   to the Remote Station.

   On Remote RX station the Value of PORTC
   sent on AIR will be latched on its local PORTC
   */

   uint8_t data;
   while(1)
   {
      data=PINC;

      /* 
      Now send a Packet
      Packet Format is AA<data><data inverse>Z

      total Packet size if 5 bytes.
      */

      //Stabilize the Tx Module By Sending JUNK data
      UWriteData('J');  //J for junk

      //Send 'A'
      UWriteData('A');

      //Send Another 'A'
      UWriteData('A');

      //Send the data;
      UWriteData(data);

      //Send inverse of data for error detection purpose

      UWriteData(~data);

      //End the packet by writing 'Z'
      UWriteData('Z');

      //Wait for some time
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);
      _delay_loop_2(0);
   }
}


Complete Listing of Rx.c


#include <avr/io.h>

#include "usart.h"

void main()
{

   uint8_t i; //Clasical loop varriable

   uint8_t packet[5],data=0;

   DDRC|=0xFF; //All Output

   //Initialize the USART with Baud rate = 2400bps
   USARTInit(416);

   /*
   Get data from the remote Tx Station
   The data is the value of PORTC on Remote Tx Board
   So we will copy it to the PORTC of this board.

   */

   while(1)
   {
      //Wait for a packet
      while(!UDataAvailable());
      if(UReadData()!='A') continue;
      while(!UDataAvailable());
      if(UReadData()!='A') continue;

      while(UDataAvailable()!=3);

      //Get the packet

      for(i=2;i<5;i++)
      {
         packet[i]=UReadData();
      }

      //Is it ok?
      if(packet[2]!=((uint8_t)~packet[3])) continue;

      if(packet[4]!='Z') continue;

      //The packet is ok

      data=packet[2];

      //Now we have data put it to PORTC
      PORTC=data;
   }


}

Downloads

Videos

Comming soon !

 


49 Responses to “RF Communication Between Microcontrollers – Part III”

  1. 1
    bittusrk Says:

    is it really necessary to invert the data?

  2. 2
    Avinash Says:

    @Bittusrk,

    As I said before starting the article “Do as I say” or interpret the results your self. If it was not necessary then why would I have done it ?

    I did lots of experiment before posting this article and I posted the setup which performed the best. If you are in the mood of re-inventing the wheels then you are most welcomed to waste your time!

  3. 3
    AVR Micro Says:

    Nice project!

    I am just about starting a similar RF project.

  4. 4
    bittusrk Says:

    @Avinash

    Cool down! My fault. Sorry

  5. 5
    Gonio Says:

    What is the maximum possible baud rate of the system,should it extendable,what is the tested range of praposed system.
    Thanks Avinash

  6. 6
    Ravindra Says:

    I want to know about antenna whether it will be used for a distance of 3meter or not.
    Your tutorials are just best and you have done nice job for the students.
    Thanx in advance.

  7. 7
    Avinash Says:

    @Ravindra

    Use about a 20 cm antenna. The range is good. Signal is available through out a normal size house.

  8. 8
    Gonio Says:

    What is the maximum possible baud rate of the system,should it extendable

  9. 9
    raghu Says:

    thnks a lot dude………………….

  10. 10
    raghu Says:

    cant we use internal oscillator ………….?

  11. 11
    Shashank Says:

    Hi Avinash
    first of all nice work n thanks for the lovely tuts…

    I was trying to do the same thing ( which u did here ) with the same modules however not with AVR but NXP LPC2148 (ARMTDMI-S)… following are my questions…

    1. As ARMs work at 3v3 instead of 5v what transistor will be appropriate for the NOT gate?
    2. If you happen to have the experience with ARMs what are the considerations to be taken in designing the same.
    3. Is it necessary to use the NOT gate if some authentic and high quality RF transmitters and receivers are use instead of the usual ones?
    4. If you happen to know some other better methods or modules for wireless communications ( esp. for ARM microcontrollers ) please do tell…

    waiting for your reply…
    thanks in advance
    shashank

  12. 12
    karthik prasad Says:

    can i set the POL bit in the UART register rather than use an external inverter

  13. 13
    gaurav roy Says:

    hey avinash,
    no offence to you.i really like your site and i have bought some stuff from you earlier.i really feel you are talented.but isn’t it wrong to speak rudely to the ppl who are following your blogs so closely and really appreciate your work?i am referring to bittusrk first comment on this article.he was just asking the reason to invert the data.you should have told him what would happen if you dont invert the data.what are the pros and cons of doing so or if you really are busy you shudnt have replied.but i dont find that any way to reply to somebody.i know its your site.but isnt it running only because of the people visiting this site and the ppl buying your great products?if the only purpose of teaching the ppl is not served then what is the point of the site?
    i am still telling you i have no grudge against you and i really appreciate your talent in this field.thought of just telling you my opinion on this topic.sorry if it sounded rude.

  14. 14
    Avinash Says:

    @Gaurav,

    I got angry just because he (Bittu Sarkar) asked the question whose answer was already there in the article. You cannot know my problem as you cannot get the chance to see hundreds of silly comment posted here(because I delete them straightway). I try to give as much information as required to understand the concept and apply it practically without much trouble. If people don’t get basic stuff they shouldn’t be reading this at all!

    Some people ask “can you explain while command” while others are too smart and remove the pull up resistors from the I2C lines and say “It is NOT working”.

    The only thing I don’t like is the “approach” some people take to solve a given problem. I get angry on people whose approach I don’t like. While people which right approach gets as much help he/she wants.

    Other people who gets hard replies from me are the one with very poor common sense. I think no one can help them (so how can I).

    Imagine you just bought a 100 PIN fine SMD IC (say a very modern radio communication IC) that has a 600 page datasheet. And has very complicated communication mechanism and you don’t have much experience in professional programming. And if your setup does not works the first time. Then what would you do? Pick up the phone and call the dealer that his IC is not working! or Try to read/research more and debug the code?

    The first method is very easy BUT IT IS NOT GOING TO TAKE YOU ANY WHERE because 99% chances are that the IC is All Right and Only your setup is faulty. That is an example of WRONG approach to solve a problem.

  15. 15
    Arifuddin Says:

    @ Avinash

    i agree with you. People should read articles carefully before they post any comments or doubts.

  16. 16
    Arifuddin Says:

    @ avinash

    I forgot to ask you one question. What is the max baud rate for this setup?

  17. 17
    Shashi Jain Says:

    thanx thanx thanx….
    thanx for saving me from searching circuits for rf comm…..

  18. 18
    Shashi Jain Says:

    hey….plz help me out its urgent…i’ve made the circuit but my microcontroller is giving lots of error in programming….
    I’m using CODE VERSION AVR software….
    what should i do???plz help me avinash…

  19. 19
    Shashi Jain Says:

    ne more thing i forgot to mension on above post…m using atmega16L…

  20. 20
    Shashi Jain Says:

    its giving error that its not able to open the included file avr/io.h

  21. 21
    Avinash Says:

    @Shashi,

    Its written for avr-gcc how will it compile in Code Vision ???

    Please don’t use this site to show your foolishness to the world !

  22. 22
    Shashi Jain Says:

    den wats the solution sir???
    i dont knw much about uC…m just a biginer….
    m asking for help…i’ve been waiting for the 3rd part for long time sir…:(

  23. 23
    Niclas Says:

    @ Shashi
    I’ve been working with uC’s for a couple of weeks and been programming C since late January, i’ve followed avinash tutorial to understand it all, really informative and well written, then wrote my own code, both with polling and interrupts, works great with parallax transceivers! you have to understand that you NEED TO DO YOUR OWN RESEARCH, you can’t write on a forum and expect one answer to do all the work for you, there are ALOT of things you MUST figure out by yourself or im afrid you will never make it…

  24. 24
    Avinash Says:

    @Niclas

    you have to understand that you NEED TO DO YOUR OWN RESEARCH, you can’t write on a forum and expect one answer to do all the work for you, there are ALOT of things you MUST figure out by yourself or im afrid you will never make it…

    Thats what Most Indian* Student/ Hobbyist fail to understand. Please don’t expect any one is going to do your homework.

    I have to delete many such comment every day. Some even get hard replies from me.

    * No racism intended here as I am Indian too :)

  25. 25
    Shashi Jain Says:

    i just learned about I/P and O/P port in a workshop for obstacle detection using codevision, before 3 months…
    in these 3 moths i was having my semester…still studied/experimented a lot on uC…n nw i’ve reached to UART…
    ….
    still searching for avr-gcc…
    thanx avinash sir and niclas…

  26. 26
    Avinash Says:

    @Shashi,

    “Still Searching for avr-gcc”

    What does that means ???

    This whole site is about avr-gcc only !!!

    Did you forget to read this
    http://extremeelectronics.co.in/avr-tutorials/part-iv-the-hello-world-project/

    Not every one is as rich as you to purchase Code Vision Compiler. I can’t recommend then to Go and Buy Rs 10,000 Compiler.

  27. 27
    Niclas Says:

    im using AVR studio , AVR studio includes the gcc compiler, just run and compile!

    AVR studio can be found here:

    http://www.atmel.com/dyn/Products/tools_card.asp?tool_id=2725

  28. 28
    Niclas Says:

    and ofc WIN-AVR, google it and youll find it ^^

  29. 29
    Niclas Says:

    PS. you need both (and i suppose its WIN-AVR that includes the gcc not AVR-studio) DS.

  30. 30
    vivek Says:

    @Avinash
    “Not every one is as rich as you to purchase Code Vision Compiler. I can’t recommend then to Go and Buy Rs 10,000 Compiler”

    1st of all this is a very harsh and unnecessary comment. Nobody is foolish enough to “buy a Rs 10000 compiler”. People are smarter than you think. The crack for this software is not that difficult to obtain.

  31. 31
    Avinash Says:

    @Vivek

    That was not as Harsh as the comment your about to receive !

    People of No use like you uses the CRACKED software. No Company can use a CRACKED software. I never recommend any one to use CRACKED software. It like being a thief. The guys who developed CodeVision is Much Much Smarter than you are.

    If no one is foolish enough to buy then the CodeVision guy must be foolish. But this is not the case, this implies that you are foolish.

    Moral of the Story
    Please friends never use Pirated Software. If you cannot afford to buy then Please use free alternatives. Because when you develop an embedded application its ultimate form is a commercial product. If you have learned to use a Paid software (when you were in learning phase) then you have to use that software also for any of your commercial product. And if your company supplies product through out the world( ultimate aim of any company) and you use a Cracked software, Code Vision Guys will sue you in no time !!!

    So better use free software even at your learning phase(unless you are rich).

    If you can buy CodeVision then its good but if you cant buy it use avr-gcc.

  32. 32
    Shashi Jain Says:

    plz dont fight wid each other…k i accept my fault…
    neway m not a rich person or else i would have been using readymade remote control modules…n yes it was very harsh reply from ur side…
    i’m working with codevision avr becoz i got it in the workshop itself…in indore most of of the workshops r using this software…and i cant argue wid them to use low cost software so as to reduce workshop fee!!!…

    n one more thing i would like to say….
    not every one is as intelligent as u r!!!…

  33. 33
    Arifuddin Sheik Says:

    Sir, I build the whole setup as shown in circuit. I am using ATmega 32 and using your Programmer, I dumped the code. i adjusted fuse bits as given. I tested Tx pin using LED. its working. I think the problem is in receiving side. I used 16MHZ crystal. I am not getting the data in receiver. I tried all ways. I connected the tx pin from transmitter to rx pin of receiver. Please help me to find out the solution.

  34. 34
    Uttam Dutta Says:

    I want to know what has to be done for communication between 200 to 300 meters with different obstacales in between and other RF noise signal.
    regards,
    Uttam

  35. 35
    hbomb Says:

    Thanks avinash your tutorials are excellent and full of info for beginners like me

  36. 36
    Lieven Says:

    Hello

    don’t shoot me if this is a stupid question.
    But does it also work with a ATMEGA8535 (reciever) and ATMEGA16 (transmitter)??

    and is there a possible way to use the internal frequantie of the MCU?

    many thanks

  37. 37
    Avinash Says:

    @Lieven

    Hello,
    Yes it can work with ATmega8535. If you wanna use Internal Crystal Of MCU then you have to change the UBRR value according to the CPU Frequency and the baud rate required. All these are discussed here.

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

  38. 38
    Lieven Says:

    Hello

    I would like to use the same freq ,internal 16mhz and baudrate of 2400bps.

    In the USART.C i found this

    #include
    #include
    #include

    #include “usart.h”

    void USARTInit(uint16_t ubrrvalue)
    {
    //Setup q
    UQFront=UQEnd=-1;

    //Set Baud rate
    UBRRH=(unsigned char)(ubrrvalue>>8);
    UBRRL=(unsigned char)ubrrvalue;

    /*Set Frame Format

    Asynchronous mode
    No Parity
    1 StopBit
    char size 8

    */
    …….

    do i have to change this

    UBRRH=(unsigned char)(ubrrvalue>>8);
    UBRRL=(unsigned char)ubrrvalue;

    to this? from the formule ( ubrr = (Fosc/(16*baudrate))-1 )

    UBRRH=416;
    UBRRL=416;

    or do i have to change more?

    many thanks

  39. 39
    newbee Says:

    Hi Avinash,

    I am not able to download the following,
    Complete AVR Studio Project for Tx
    Complete AVR Studio Project for Rx

    Is there some error or thess projects is removed?? Where can i find them alternatively if not provided on the above links?

  40. 40
    Avinash Says:

    @Newbee
    The files are live and easily downloadable. I tried just now!

  41. 41
    Arifuddin Sheik Says:

    I finally got this working. [:)]

  42. 42
    tester Says:

    @ Arifuddin Sheik

    what was your fault? Becuase I can’t get this working. And the circuit is correct.

    thanks

  43. 43
    chinmay Says:

    hi avinash,
    well i want to change the Crystal freq to 12Mhz. n i m using 315Mhz RF module. is it good to work with 12Mhz crytal n 315Mhz RF module. i know for dat i hv change few lines in the code n i can do dat. just pls tell me weather ur codes r flexible for it or nt. otherwise i m comfort to work wid your specified instruction.

  44. 44
    BISWAJIT Says:

    i m using crystal of 12mhz and the ubrr value is 311. my rf modules are 433mh himark made rf modules.

    this is not working . tell me the prob is it mandatory to use 16mhz.and that 315 mhz rf modules?

  45. 45
    zhaem1988 Says:

    hi
    thanks for your wonderful tutorials
    are the tx &rx module in Proteus simulation program or any other program ?
    because i have project and i want to simulate it before doing it
    thanks

  46. 46
    priyanka Says:

    Hi,
    firstly hats off to u and thanx for explaining the rf communication so elaborately….m doing my last year engg nw n interested in rf… dis article really helped a lot in clearly some of the basic concepts which i ws stuck in…u cn say m a beginner in dis field n searchin for a project application using rf communication…
    thanks

  47. 47
    Akhila Says:

    Sir,

    I’m working on my final yr proj which is based on “Swarmbots”.I would like to employ RF communication for the same using a PIC microcontroller(16F877A).Is it possible to use the same logic as above with PIC too?..Kindly reply.

    Thank you,
    Akhila.

  48. 48
    Avinash Says:

    @Akhila

    Why NOT ?

  49. 49
    Akhila Says:

    thank you sir.

Leave a Reply

Comments

    • wlewis: @Av.. To my knowledge, practically all the non-ic humidity sensors are frequency dependant...
    • wlewis: @Av.. about ADC.. how about a tutorial that does a 10bit conversion?
    • wlewis: @Av I have a great idea for a tutorial… sensors which output are measured by frequency....
    • BoB: Hello… I’m still a newbie in this field..so, may I ask…this application used...
    • Hill: Why dont you mention in your URL which PICs these burner supports? It will help avoid guesswork...
    • victor: I have to rectify, that is not an issue, instead is a LCD’s McU limitation in locations...
    • wlewis: Avrdude code for atmega32 // 16mhz crystal // Jtag disabled. avrdude -p m32 -b 19200 -P COM3...

Video

  • Comments

    • wlewis: @Av.. To my knowledge, practically all the non-ic humidity sensors are frequency dependant...
    • wlewis: @Av.. about ADC.. how about a tutorial that does a 10bit conversion?
    • wlewis: @Av I have a great idea for a tutorial… sensors which output are measured by frequency....
    • BoB: Hello… I’m still a newbie in this field..so, may I ask…this application used...
    • Hill: Why dont you mention in your URL which PICs these burner supports? It will help avoid guesswork...
    • victor: I have to rectify, that is not an issue, instead is a LCD’s McU limitation in locations...
    • wlewis: Avrdude code for atmega32 // 16mhz crystal // Jtag disabled. avrdude -p m32 -b 19200 -P COM3...