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.
- RF Communication Between Microcontrollers – Part I : Introduction to RF Communication and Modules.
- RF Communication Between Microcontrollers – Part II : Algorithm and general description of data transfer.
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 Tx + AVR ATmega16 |
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
- Power PINs of MCU are not shown but must be connected properly.
- 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.
- 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.
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
- Complete AVR Studio Project for Tx
- Complete AVR Studio Project for Rx
- HEX File for Tx
- HEX File for Rx
Videos
Comming soon !


is it really necessary to invert the data?
January 17th, 2010 at 5:08 pm@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!
January 17th, 2010 at 5:48 pmNice project!
I am just about starting a similar RF project.
January 17th, 2010 at 8:20 pm@Avinash
Cool down! My fault. Sorry
January 17th, 2010 at 8:53 pmWhat is the maximum possible baud rate of the system,should it extendable,what is the tested range of praposed system.
January 18th, 2010 at 11:18 amThanks Avinash
I want to know about antenna whether it will be used for a distance of 3meter or not.
January 18th, 2010 at 2:42 pmYour tutorials are just best and you have done nice job for the students.
Thanx in advance.
@Ravindra
Use about a 20 cm antenna. The range is good. Signal is available through out a normal size house.
January 18th, 2010 at 5:20 pmWhat is the maximum possible baud rate of the system,should it extendable
January 21st, 2010 at 2:50 pmthnks a lot dude………………….
January 29th, 2010 at 9:44 pmcant we use internal oscillator ………….?
January 29th, 2010 at 9:59 pmHi 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…
February 5th, 2010 at 11:32 pmthanks in advance
shashank
can i set the POL bit in the UART register rather than use an external inverter
February 7th, 2010 at 7:58 amhey avinash,
February 10th, 2010 at 12:29 amno 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.
@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.
February 10th, 2010 at 9:38 am@ Avinash
i agree with you. People should read articles carefully before they post any comments or doubts.
February 21st, 2010 at 2:40 pm@ avinash
I forgot to ask you one question. What is the max baud rate for this setup?
February 21st, 2010 at 2:41 pmthanx thanx thanx….
March 2nd, 2010 at 7:40 pmthanx for saving me from searching circuits for rf comm…..
hey….plz help me out its urgent…i’ve made the circuit but my microcontroller is giving lots of error in programming….
March 3rd, 2010 at 8:32 pmI’m using CODE VERSION AVR software….
what should i do???plz help me avinash…
ne more thing i forgot to mension on above post…m using atmega16L…
March 3rd, 2010 at 8:51 pmits giving error that its not able to open the included file avr/io.h
March 3rd, 2010 at 9:31 pm@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 !
March 4th, 2010 at 9:32 amden wats the solution sir???
March 4th, 2010 at 7:54 pmi 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…:(
@ Shashi
March 4th, 2010 at 10:32 pmI’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…
@Niclas
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
March 5th, 2010 at 12:33 pmi just learned about I/P and O/P port in a workshop for obstacle detection using codevision, before 3 months…
March 5th, 2010 at 7:04 pmin 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…
@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.
March 6th, 2010 at 9:32 amim 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
March 6th, 2010 at 6:34 pmand ofc WIN-AVR, google it and youll find it ^^
March 6th, 2010 at 6:36 pmPS. you need both (and i suppose its WIN-AVR that includes the gcc not AVR-studio) DS.
March 6th, 2010 at 6:37 pm@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.
March 6th, 2010 at 7:33 pm@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.
March 6th, 2010 at 8:55 pmplz 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….
March 7th, 2010 at 2:06 amnot every one is as intelligent as u r!!!…
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.
March 15th, 2010 at 1:11 pmI 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.
April 4th, 2010 at 2:34 pmregards,
Uttam
Thanks avinash your tutorials are excellent and full of info for beginners like me
April 15th, 2010 at 6:23 amHello
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
April 27th, 2010 at 7:06 pm@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/
April 27th, 2010 at 7:20 pmHello
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
April 27th, 2010 at 9:36 pmHi 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?
April 28th, 2010 at 3:08 pm@Newbee
April 28th, 2010 at 3:49 pmThe files are live and easily downloadable. I tried just now!
I finally got this working. [:)]
May 2nd, 2010 at 5:33 pm@ Arifuddin Sheik
what was your fault? Becuase I can’t get this working. And the circuit is correct.
thanks
May 11th, 2010 at 7:42 pmhi avinash,
May 27th, 2010 at 4:44 pmwell 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.
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?
May 27th, 2010 at 6:03 pmhi
May 28th, 2010 at 4:01 amthanks 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
Hi,
July 23rd, 2010 at 8:17 amfirstly 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
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,
August 29th, 2010 at 10:14 pmAkhila.
@Akhila
Why NOT ?
August 30th, 2010 at 7:53 amthank you sir.
September 1st, 2010 at 8:00 pm