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?
@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!
Nice project!
I am just about starting a similar RF project.
@Avinash
Cool down! My fault. Sorry
What is the maximum possible baud rate of the system,should it extendable,what is the tested range of praposed system.
Thanks Avinash
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.
@Ravindra
Use about a 20 cm antenna. The range is good. Signal is available through out a normal size house.
What is the maximum possible baud rate of the system,should it extendable
thnks a lot dude………………….
cant we use internal oscillator ………….?
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
can i set the POL bit in the UART register rather than use an external inverter
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.
@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.
@ Avinash
i agree with you. People should read articles carefully before they post any comments or doubts.
@ avinash
I forgot to ask you one question. What is the max baud rate for this setup?
thanx thanx thanx….
thanx 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….
I’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…
its giving error that its not able to open the included file avr/io.h
@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 !
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…:(
@ 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…
@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
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…
@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.
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
and ofc WIN-AVR, google it and youll find it ^^
PS. you need both (and i suppose its WIN-AVR that includes the gcc not AVR-studio) DS.
@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.
@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.
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!!!…
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.
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
Thanks avinash your tutorials are excellent and full of info for beginners like me
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
@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/
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
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?
@Newbee
The files are live and easily downloadable. I tried just now!
I finally got this working. [:)]
@ Arifuddin Sheik
what was your fault? Becuase I can’t get this working. And the circuit is correct.
thanks
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.
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?
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
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
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.
@Akhila
Why NOT ?
thank you sir.
Sir;
Your tutorial really helps me a lot. I have questions to ask you, it might be a simple questions but I really can’t figure it out.
The question if I want to transmit mp3 audio using transmiiter and the transmitter is controlled by the ATMEGA16, how I do I input the mp3 audio (let say if the mp3 audio is from laptop audio jack) to ATMEGA16 (which pin), do I need to use ADC?
I really grateful if you can teach me. Thank you for attention and time.
@Aini,
First clear out the basics. MP3 is a digital audio format in compressed form. That means it is only 0 and 1. The audio output from laptop can be directly feed to a headphone speakers. And since we know a speaker (in the headphone or earphone) has got no digital logics in it. This clears it out that the audio which is feed to it is NOT digital.
The mp3 files in the Harddisk is digital, which is read by the software (like Winamp) and decoded by CPU (which is No doubt digital). Finally we get an uncompressed digital output. But still the output is digital. The next step is done by the sound card which is really a good quality DAC (and also ADC for input). Now the CPU feeds this uncompressed digital data to the sound card which convert it to analog waveforms. Which is available at laptops audio jack.
So you need to feed this to AVR ADC and get converted it to digital form.
The whole concept cannot be explained in single comment.
Thank you for your reply. It helps me a lot.
Hi
Thanks for the helpful article. I’m confused about the way you use UQFront and UQEnd in usart.h,usart.c.
Could you please explain me how it helps the execution?
Thank you very much
@Prasad,
Its simple implementation of a FIFO Queue! May be you should take some Computer Science Class! Because it out of scope of the current discussion. If I were to discuss how to implement a Queue it would take another three articles.
Hi
thanks for the reply. I understood how you are implementing it but how did you manipulate RECEIVE_BUFF_SIZE is 64.
is that in data sheet?
Thanks
hye, can you tell me what is the suitable rf module for voice transmission? cause right now, im using 433MHz rf module which is can transmit and receive data at 4800bps (transmit 600 bytes per second), but there is someone said to me that i need at least 4kbyte per second for voice sampling however he don’t know any high speed module. is he right? if yes, can you suggest what is the suitable rf module? thanks
@Zaz,
To transmit a Sound which is limited to less the 4KHz frequency(Human Speech Band), you need at least a 8KBps (kilobyte per second) link. This is the worst quality sound that is just sufficient to carry only what human can produce from their mouth. Other method is to use a compression algorithm but they are hard to code and run on small micros.
You can easily transmit/receive sound via analog mode (say FM) technology.
you said 20cm should be enough for a house.the rf module which i bought has max range of 100m.to exploit max range what type of antenna should be used?
Tats really cool man.helps a lot
I got couple of questions for u.
-what is the max range achieved wit this system..??
-someone has asked this before n i guess u din reply to tat..-can i use a single reciever for multiple transmitters..??
thanks
God bless..
This project is really help me in making my wireless automation project . i have replaced the outputs to toggle and connected relays through ULN2803 darlington IC i get good range 100m line of sight..i hav tried it before also from science prog website.I have one doubt is it c\necessary to connect NOT gates to TX and RX.
I have seen similar projects on websites they have also used inverted RS232 communication.
hello
can i use the shy-j6122tr(http://www.ekt2.com/_files/141%20RF%20MODULE%20TXRX.pdf) rx-tx modules in this project because as far as i know these modules uses manchester encodind and does not support uart