Interfacing RF Shield/Module In Arduino
Simplified Block Diagram Of A Radio Frequency Communication System |
A simplified Radio Frequency communication block diagram is shown above. Since most of
the encoders/decoders/microcontrollers are TTL compatible, most of the
inputs by the user will be given in TTL logic level. Thus, this TTL
input is to be converted into serial data input using an encoder or a
microcontroller. This serial data can be directly read using the RF
Transmitter, which then performs ASK (in some cases FSK or PSK) modulation on
it and transmit the data through the antenna.
In the receiver side, the RF Receiver receives the modulated signal
through the antenna, performs all kinds of processing, filtering,
demodulation, etc and gives out a serial data. This serial data is then
converted to a TTL level logic data, which is the same data that the
user has input.
An RF Module (Radio
Frequency Module) is a small electronic circuit used to transmit and/or
receive radio signals on one of a number of carrier frequencies. RF
Modules are widely used in electronic design owing to the difficulty of
designing radio circuitry. RF Modules are most often used in medium and
low volume products for consumer applications such as garage door
openers, wireless alarm systems, industrial remote controls, smart
sensor applications, and wireless home automation systems. This has single channel for data transfer, thus serial data communication is used.
The operating frequency of RF module can be
varied. For short range wireless control applications, an ASK RF
Transmitter-Receiver Module of frequency 315 MHz or 433 MHz is most
suitable. They are quite compact and cheap! You can buy them from various internet stores.
RF Transmitter-Receiver Module |
RF Transmitter-Receiver Module |
Tx Module Pin description:
Pin No
|
Function
|
Name
|
1
|
Antenna
|
ANT
|
2
|
Serial data input pin
|
Output
|
3
|
Ground(0V)
|
Ground
|
4
|
Supply voltage(3V-12V)
|
Vcc
|
Rx module Pin description:
Pin No
|
Function
|
Name
|
1
|
Ground(0V)
|
Ground
|
2-3
|
Serial data output pin
|
Input
|
4
|
Supply voltage(3.3V-6V)
|
Vcc
|
5
|
Antenna
|
Ant
|
Connection of RF module and Arduino is very simple, in market you can find many one that are compatible with Arduino.
Rx/Tx Connections |
Transmitter Schematic |
In schematic you can find Arduino Board, which is denoted by U1. You can
see some buttons at the right side of the Schematic. This button is
used for general purpose. There are 5
switches, you can use it like up, down, left, right and enter. And
according to these buttons you need to send data through Tx module. Here
Tx module connector is denoted by TX. At the upper left side of the
schematic you can notice that we have put a seven segment display which
is CC(Common Cathode)type. When a switch is pressed,the corresponding
switch number (1,2,3,4,5) will be shown in seven segment display. For
controlling seven segment displays we have used 7447 BCD to Seven
segment drivers IC. For 7447 datasheet you can click this link. Datasheet of 7447.
Receiver Schematic |
In schematic you can find Arduino board which is U1, RF receiver
connector which is mention as RF RECEIVER. Here I have connected two
relays for other works. Suppose you want to control two electrical loads
by RF transmitter (Ex: RF Remote control). Here you can notice that D9
& D10 are mentioned as Relay1 and Relay2. By these two pins you can
control the relays. The main theme is you get data from RF receiver
then you need to check it with your predefined data. If it matches, then
you can do anything you want.
Connection procedure
- First you need to connect your Transmitter shield with an Arduino board and also a Receiver shield with another Arduino Board.
- Plug 12V Dc with both of the Arduino Boards so that Arduino can start working.
- If you think, you don’t want to use our Rx, Tx module then you need to connect wire properly between Tx, Rx module and also shield. You don’t need to be worried about this. By shield you also can connect other Tx, Rx module with Arduino. For Schematic please check the link:
- Now your circuit setup is complete.
- For coding you need to plug Usb cable with your Arduino.
- Now start your code and debug. Enjoy!!!
To send data you need to synchronize your Tx and Rx module. For this you need to send 0’b01010101 or ‘U’ which has a binary value 01010101. This is called pre-amble. This pre-amble byte is usually sent 2 to 5 times in a row to synchronize sender and receiver,
BUT NOT ALWAYS! Once you have synchronized your chip to the RF receiver
you can sample the bits “in the middle” of your data line. In your data
you also need to attach starting bit and ending bit, without these two
bits you can’t understand when the main data is started and when
finished. Data pattern can be like this: "UUU/Arduino Rocks#@". In
this data pattern you can notice that at first I have sent ‘U’ 3 times
for synchronization and after that I have put the start bit by ‘/’ and then sent data which is "Arduino Rocks" and finally sending finish bit by "#@ ".
Code for Receiver module
#include <SoftwareSerial.h> // Include Software Serial libary SoftwareSerial mySerial(4,6); // Rx,Tx //===========Scan function================================= void scan() { //=================Variable Initialization================// char tmp[20]; char data[20]; for ( int i=0;i<20;i++) // Every Index Initialize by 0 { tmp[i]=0; } byte len=0; //========================================================// //===============Algorithm Start=========================// if (mySerial.available()) // If Software Serial detect any data in it's buffer { mySerial.setTimeout(10000); // Set timeout for 10 sec mySerial.flush(); // Flush MySerial buffer // This is build in function of Software serial // findUntil() reads data from the stream until the target string // of given length or terminator string is found. // The function returns true if target string is found, false if timed out // Syntax: stream.findUntil(target, terminal) // Sending Data pattern would be in this example: UUU/PiLabs*@ // Sending Data pattern must be: /USER DEFGINED DATA*@ if (mySerial.findUntil( "@" , "@" )) //checking the condition { // readBytesUntil() read characters from a stream into a buffer. The function // terminates if the terminator character is detected, the determined length has been read, // or it times out. // readBytesUntil() returns the number of characters placed in the buffer. A 0 means no valid data was found. // Syntax: stream.readBytesUntil(character, buffer, length) len= mySerial.readBytesUntil( '@' ,tmp,50); // From this instruction we can get length of the string // Also saved the string in tmp Serial.println(tmp); // For Debugging we print tmp string in serial port Serial.println(len); // For debugging we print the length of string int i=0; // Initialize variable i for (i=0;i<20;i++) // in here data's every index initialize by 0 { data[i]=0; } // ===Algorithm for Sorting main data from Total String====== while (tmp[i]!= '*' ) // Searching for * Because Main data start after * { i++; // Increamented index by 1 } i++; // When * is found index increase 1 bcoz main data start // after * int j=0; // Initialize j by 0 while (tmp[i]!= '/' ) // Now its searching for '/' And till '/' its stored data in data variable { data[j]=tmp[i]; // Stored data in data variable from string i++; // Increament index j++; // Increament index } Serial.println(data); // Serial data print in serial terminal // strcmp is a function which return 0 when two string match if ( strcmp ( "PiLabs,Rock" ,data)==0) { // And in here it checked by strcmp function Serial.println( "GOTCHA" ); // Serial Printout } } } } //================Setup function============================================= void setup() { Serial.begin(9600); // Serial Port initialization by 9600 Bps mySerial.begin(1200); //SoftwareSerial port initialization by 1200 bps mySerial.setTimeout(10000); // Set time out 10 sec mySerial.flush(); // Software serial buffer flush } //=================loop Function==================================== void loop() { scan(); // Called scan function }
Code for Transmitter module
|