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 !
By
Avinash Gupta
me@avinashgupta.com
My Facebook Profile




Avinash ji,
Thanks for the hard work.
Your A,A,D,~D, Z is an excellent idea, that helped me.
In fact i changed the 315 tx-rx set 433mhz, thinking its is not working, when physical line was replaced by rf.
Today i was just searching for an appropriate inverter as the always high (mark) was consuming a lot of current at the remote.
The receiver spec says 10 micaro amp load, which may not drive an op-isolator. needing amplifier to invert.
I used earlier bc107as simple inv. but not sure with 10mu amp base crt.
Your saying “Do as I say” is the right thing, else u’d have to explain this much, with no certainty that young man will understand.
Its a great pleasure to see ur solution.
I can complete the proj in this calender year itself!!
Cant wish you a Happy (Invader’s ?)New Year .
Have a nice season.
regards.
K
avinash ur website is awesome….by reading your blog i learned about rf modules which i have been trying from 1month
Can both RF modules be integrated on the same board, in order to have bidirectional communication?
10x
@AKILA
HAVE U COMPLETED THE PROJECT?
i would want to know whether we are required to construct the same power supply as u have provided or can we use another external power supply in the form of batteries etc plz suggest as our project is a radio frequency beacon tracker and we need it to be portable
also plz suggest modifications required for our project in terms of the circuit provided by you i am working at 433mhz frequency band
@bosco
Yes you an use batteries.
but a 9v batteries would do
with modification
do you know of rx-tx rf pair at 433mhz with a rssi for power levels
Hi. I am using 315MHz ASK modules to communicate with two ATMega8 boards. The master board has the transmitter and the slave has the receiver, with BC107 inverter. The master sends “AA 55 FF 30″ (without quotes). The receiver checks the string and then puts on an LED. To see if the string is received properly, I programmed so that the received byte is tramsmitted again on the RS 232. This works well with a cable on RS232 interface.
The code is simple:
ISR (USART_RX_vect)
temp = UDR;
while (!(UCSRA =(1<<UDRE)
;
UDR=temp;
With RF, at the receiver, after the inverter, i.e., at the ATMega Rx pin I get the string properly, seen on GTK term. However, the MC transmits back "AB 55 01 95" consistently.
why does the MC change the data? It is because of this that the program does not recognise the correct data. This is not noise, because it this consistent. I tried otehr data, but similar issue occurs, the data at the Rx point is OK but the MC transmits converted data.
Can someone help?
hey i want to now where would i get the sm-tx 01 and sm-rx 02 i searchd at lamington mumbai i didnt find it do u now of an outlet where i could purchase it in mumbai
You will get these at Visha Electronics, opposite police station, first floor, Lamington Road. They give components at proper prices too.
Hi. I posted a problem i am facing on this expert forum regarding the rf communication between two atmega8 boards on 22 march above. I ahve seen some other circuits where the transistor inverter is not used. But when i directly connect the idle low output of the rx module to the idle high rx of the MCU, the reception stops. I have not used the transistor inverter on teh transmitter. Can someone help?
Hey avinash,
That was a really helpful tutorial. I tried it with a new micro-controller called mbed and it worked out perfectly fine. Now , I wanted to have a two way communication . So i bought 2 different pairs of transmitter and receivers working at different frequencies. One pair works at 315 Mhz and other at 433 Mhz. But there still seems to be interference caused by each other. Do you know what could be done to prevent this?
Same problem. Did you find a solution? I’m guessing i need to put a filter o the antenna of the receiving modules.
hey avinash
In your usart.c library file
in USARTInit(); function you have not enabled RXEN..
Isnt it necessary?!
Although i tried this project and it worked awesomely fine
Sorry dude!!
Didnt see library properly!!
My mistake!
hey avinash !!
this was realy a great tutorial.
it cleared many of my concepts.
i want to know will the settings in the usart.c and usart.h files work for atmega8 as well…
or i will have to make some changes in the codes…
can i simulate this project in proteus ??bcoz ur schematic seems to be image from proteus …how to simulate tx and rx bcoz there are no library of them in proteus???
Hello Avinash,
First of all, your tutorials have helped me immensely, thank you so much.
I have been implementing many of the projects in your tutorials using Atmega328p MCU’s and am trying to do the same here. For all of the registers, I have compared the Atmega16 and Atmega328p datasheets to implement the same functionality on the 328p. The problem I cannot figure out, is the USART interrupt vectors. When I compile the code, I get a warning: “‘USART_RXC_vector’ appears to be a misspelled signal handler.”
In both datasheets there is mention of “USART_RXC” in the ISR sections, however, I do not understand why USART_RXC_VECTOR is accepted by the compiler for the m16 and not the m328p. Can you please help me?
(I do see that in “USART.h” there is the line “#define USART_RXC_VECT USART_RXC_vect” which is noted to change for each MCU, but I dont know what it would change to for the 328p, if at all because the “USART_RXC” ISR is listed as the same thing for both MCU’s)
Thank You
@Jake
Please use this page to find out the name of ISRs
http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html
For ATmega328p the name for USART receive complete interrupt is “USART_RX_vect ”
Hope it helps.
Dear Avinash Sir,
I am planning to do a wireless communications project using AT89S51 and these RF pairs…would you please suggest me what type of antenna should i use for it.As my PC will be the master and robot will be the slave..how should i interface my PC to the Tx. Thank you for all the hard works you put in to help us new robot makers.Thank u once again.
Avinash,
The page on interrupts did help, thank you. I’ve had to delay working on this specific tutorial for a while since the school semester started, but I have been working on an RFID system for my senior design project using many of the principles I’ve learned from your tutorials. I should finish it by December and I’ll try to post it on youtube so others can learn from it too.
Thanks,
Jake
dear Avinash
this was realy a great work!thank you so much.
would you mind to put any timing diagram or datasheet of this kind of RF module here,I tried it but never found any thing!
thanks again
Dear Avinash sir,
Firstly loads of thanks for such a great hard work and making things easy for begineer’s like me:):)
Sorry if my question is silly, I hope you will answer
I am trying to experiment your RF module using Atmega32 microcontroller. But I saw in some internet pages that USART can be used only for wireless communication and even from my friends. Is it so?. The code available in this RF module Part III is for RF wireless communication only right??
Thanks in advance very much sir. Looking forward for your valuable reply asap.
Hello avinash sir…
i m a Ist year eng.. student…
Your tutorials hv cleared lot of my problems.
Now.I am trying to implement your project of tx and receiving data..
but i m using atmega8 at the transmitting end..and atmega32 at the receiving end..
plz sir,kindly tell me is this really possible..
if yes, where do i need to make the changes in the codes..
Sorry..if i m asking smthing foolish..
bt plz help me..
have you written these codes for atmega16..????
Thank you sir..
geek..
Yes. Why not ?
It is very clear from the above article!
thanx..sir for your reply…
but … here i have another issue…
plz help me out..
sir..kindly tell me ..that do we need to use only sm-rx-02 ask & sm tx 01 ask rfmodules…only
or i can use any other rf module also….
i have purchsed a Rs.250 rx tx pair…
but dont know whether is it…the same which u have mentioned here…
thanx..
geek
Thanks sr. I would like to use this tutorial to make a simple project but I need to know:
How much is the maximun distance between the Tx and the Rx?
Cause I need 90 meters without line of sight
Hi Avinash, your tuto is awesome, i’m from mexico and this help me a lot, but i have a question and some doubts, i was trying to send data from my pc to the TX uC but what i want is to receive it in the RX uC and show the characters sended in a LCD display 16×2, with your USART library is posible using the UWriteString function? and redirecting the data to the lcd display with your LCD library? I hope you can help me with this
Avinash me again I hope you can help me, I implement your code but anything seems to happen, i have to make some changes to the library cause I’m using atmega48 and the name of the registers is not the same like this
rxen= atmega16
rxen0= atmega484
and so on with the others, i changed this and it compile without and error, i also have to change the void main() to int main() cause when compiling that part caused a warning so i changed it.
My baud rate is also 2400bps but i’m running with 20MHz crystal and because of that according to my datasheet table of UDRR the value must be 520 for both TX and RX obviously.
But sadly nothing occurs int the receiver, the leds don’t glow, when i measure the voltage in the RX pin of the uC, and i press a button in th TX module a quick change of voltage occurs, so i think the uC RX is receiving data but it doesn’t send it to the portc.
If i measure voltage in the pin TX of the TX uC or in the data pin of the TX RF module nothing happends when i press the buttons… as i told you before just happen in the RX pin of the RX uC of RF module.
I don’t know what to do i all the code is the same i check carefully the name of the usart registers to make the modification to your library i don’t think that it could be a problem all seems to compile ok without errors and warnings.
i want t buy the RF rx-tx pair …plz menion the price of a pair
Hello Sir,
I am already using the USART of one of my atmega32 to communicate with the PC so i cannot use its TxD and RxD pins. Can i use other pins (except txd and rxd) for communication between two atmega32 microcontrollers? Please reply as soon as possible. My email id is : kartik_8582@yahoo.co.in
Thanking you
Hi Avinash,
I’ve been reading these articles about RF communication, and they are very useful (Thanks a lot for your har work).
I have two boards that include both of the RF Modules. What shoul I do with he anntenas? Do I need two? or can I use one antenna?
If you have some time, please email me.:)
Hi,My project is based on visible light LED. My transmitter and receiver is ready and struggling to interface with microcontroller to send real data . I can send signal(analog/digital pulse) upto 1m which is enough for me .My qestion is can I connect USART TX pin to my transmitter in one side and Rx pin of another microcontroller to other side since my transmission medium is light? Alternatively what can I use? I really need some help and looking forward to hear from you.
Hey Himel, For my project, i need to transmit analog signal wireless. I am able to transmit digital signal between two atmega32 micro-controllers. What do i need to change to transmit Analog signal. which ICs are suitable for ADC and DAC ? Kindly reply ASAP. Thanks in advance!
Have you connected two microcontroller wirelessly? Can you please explain your physical connection?
hi,
the below mentioned website is copying your articles and publishing them in their site.Being a fan of your website thought its my duty to let you know.
http://www.theengineeringprojects.com/2012/01/wireless-communication-between_1500.html
@Mr. Vinay,
Thanks for reporting!
what are the application of this project………
hello Avinash sir
Thanks for your tutorials..
That Not gate idea was Awesome..Not only it did logical inversion of data,but also converted it as per the RS232 standards.
Without wasting much of your time,I have a conceptual doubt.Sorry if it is silly.
You connected Rf 434 receiver with 9 volts/12 volts,directly while the datasheet says the maximum operating voltage is 4.5 to 6 volts.When ever I have worked on Rf 434 I always give it 5 volts Vcc,and it does work.
sir , i want to transmit adc data through usart ,
i have try , but the data sent is only 0-255 , that is rx side the adc data displayed is only 0-255,
only 8bit data is transmitted.
so how can i modify the usart.c and usart.h to transmit 16bit data.
The series of 3 articles about RF module was very helpful for us to do our project. I’d ordered RF module from extreme electronics store. We are obscure about Vcc to be supplied to Tx & Rx. Its indicated in Rx Vcc=3V but there is no such label on Tx. Does Tx require 5V and
or does it work with 3V only?
Hi Avinash,
I’m working on a RF remote to control a bunch of relays and triacs. My design’s sort of similar except that I didn’t use the inverter transistors. The remote is battery powered for which I’ve tried to keep the power consumption minimal through the use of sleep modes. To transmit a signal I power on the Tx, send the synchronization byte, the data byte and then the 8-bit crc, power off the Tx and finally go back to sleep mode. This series of events is interrupt-triggered. The problem is when my Tx is inactive, I keep receiving noise from the Rx. How can I deal with this without the use of any special encoders/decoders (such as HT12E/HT12D)?
can i simulate this circuit at any software?
@Juwel,
Why you want to do that?
@ juwel no you can’t.
The series of 3 articles about RF module was very helpful for us to do our project. I’d ordered RF module from extreme electronics store. We are obscure about Vcc to be supplied to Tx & Rx. Its indicated in Rx Vcc=3V but there is no such label on Tx. Does Tx require 5V and
or does it work with 3V only?
This is pure gold man, great work!!! this is going to help us very much. Thanks a lot!!!
sir we were trying to wirelessly communicate between two mcu using usart (n rf 434 module) but we were getting negative value on the data pin of register what should we do?????
If you expect any reply then please use proper English! Why you have used multiple question marks?
Hello Avinash,
Other than maintain RS-232 standard or buffering the signal, Is there any need for the inversion of the signal? Because, when TX pin on MCU#1 is high TX module input is Low and Rx module output will become low and finally MCU#2 RX will go high.
If we connect the Modules directly without inverting, when MCU#1 TX goes high MCU#2 RX will become high. So, the net effect is same.
Hey,
I am doing a similar project but using Pic18f4620 and Microchip Zigbee module.I am facing problem in the programming part of it.
Can you please help me out?I have gone through various documents but no use and the deadline date is nearing.
Please help!!
@Samidha, how much time you expect it would take to help you? and what you expect, how should I help you?
I am not sure as to how the send and receive functions are framed out.SSo if you could help me with some link or document which would guide me through the programming part of it..or maybe some sample program of similar kind…
I need to read temperature from LM35 sensor and transmit it to the coordinator where it will be compared with a threshold level and a msg will be transmitted back to switch on/off a fan at the end point..
I need to complete the programming part within 10 days…
and I have no clue..
Can you help me out in any way??
Hi, Your tutorials are really great
We’re using Atmega8 with internal crsytal 1MHz. Chosen baud of 2400.
I’ve question about external antenna design. What should be its shape and length and any other specification required?
Also, do I need to increase signal strength?
It works for about 2 meters range only with using a straight wire of 20cm as an antenna..
Thank You , hope for your reply.
Suket
umm hi, you know I have been trying to send analog data through rf, and I did accordingly whatever you wrote. Yup I got results, in the portc, the digitalize analog data.:p…. Credit goes to you. But I want to show the data on hyperterm from the receiver mcu . So using ‘utoa/ is enough right? Just a lil help required cause m not getting the data in the hyperterm. But as I can see everything is working awesome.
Thanx in advance. U deserve that, many thanx.