Interfacing RF Shield/Module In Arduino

Simplified Block Diagram Of A Radio Frequency Communication System
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.

RF module
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
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
Rx/Tx Connections
Transmitter Schematic
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
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!!!
Coding
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

#include <SoftwareSerial.h>      // Include Software Serial libary
SoftwareSerial mySerial(6,7);    // Rx,Tx
//=======================================================
//============show__segment function =========================
//=======================================================
void show__segment(int button__no)// Show__segment function start
{
if(button__no==0)// if button no 0 is pressed
{
digitalWrite(2,LOW);// 0|
digitalWrite(3,LOW);// 0|
digitalWrite(4,LOW);// 0|so no is 0
digitalWrite(5,LOW);// 0|
mySerial.println("UUU/PiLabsON0*@");// Data send via mySerial
}
else if(button__no==1) //if button no 1 is pressed
{
digitalWrite(2,HIGH);// 1|
digitalWrite(3,LOW);//  0|
digitalWrite(4,LOW);//  0|so no is 1
digitalWrite(5,LOW);//  0|
mySerial.println("UUU/PiLabsON1*@");// Data send via mySerial
}
else if(button__no==2)//if button no 2 is pressed
{
digitalWrite(2,LOW);//  0|
digitalWrite(3,HIGH);// 1|
digitalWrite(4,LOW);//  0|so no is 2
digitalWrite(5,LOW);//  0|
mySerial.println("UUU/PiLabsON2*@");// Data send via mySerial
}
else if(button__no==3)//if button no 3 is pressed
{
digitalWrite(2,HIGH);// 1
digitalWrite(3,HIGH);// 1
digitalWrite(4,LOW);//  0 So no is 3
digitalWrite(5,LOW);//  0
mySerial.println("UUU/PiLabsON3*@");// Data send via mySerial
}
else if(button__no==4)//if button no 4 is pressed
{
digitalWrite(2,LOW);//  0|
digitalWrite(3,LOW);//  0|
digitalWrite(4,HIGH);// 1|so no is 4
digitalWrite(5,LOW);//  0|
mySerial.println("UUU/PiLabsON4*@");// Data send via mySerial
}
}
//=======================================================
//=============== Scan__button function =======================
//=======================================================
void scan__button()// Scan__button function start
{
if(digitalRead(12)== LOW)// Checking 12 no pin for button press
{
show__segment(1);  // show__segment function call for no:1
}
else if(digitalRead(10)== LOW)// Checking 10 no pin for button press
{
show__segment(2);// show__segment function call for no:2
}
else if(digitalRead(9)== LOW)// Checking 9 no pin for button press
{
show__segment(3);// show__segment function call for no:3
}
else if(digitalRead(8)== LOW)// Checking 8 no pin for button press
{
show__segment(4); // show__segment function call for no:4
}
}
//=========================================================
//=================== Setup==================================
//============================================================
void setup()
{
pinMode(2,OUTPUT);//Define output, this pin is for A of 7447
pinMode(3,OUTPUT);//Define output, this pin is for B of 7447
pinMode(4,OUTPUT);//Define output, this pin is for C of 7447
pinMode(5,OUTPUT);//Define output, this pin is for D of 7447
pinMode(8,INPUT);//Define input
digitalWrite(8, HIGH);// Pull Up enable
pinMode(9,INPUT);//Define input
digitalWrite(9, HIGH);//Pull Up enable
pinMode(10,INPUT);//Define input
digitalWrite(10, HIGH);//Pull Up enable
pinMode(11,INPUT);//Define input
digitalWrite(11, HIGH);//Pull Up enable
pinMode(12,INPUT);//Define input
digitalWrite(12, HIGH);//Pull Up enable
Serial.begin(9600);                        // Serial Port initialization by 9600 Bps
jgkc
mySerial.begin(1200);                      // Software Serial port initialization by 1200 bps
mySerial.setTimeout(10000);                // Set time out 10 sec
mySerial.flush();                          // Software serial buffer flush
show__segment(0); // By default it shows 0
}
//========================================================
//==================== loop=================================
//========================================================
void loop()
{
scan__button();// scan__button function call
}

In above code we used some Arduino’s special function which are:
  • findUntil()
  • readBytesUntil()


findUntil()

 

Description

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
This function is part of the Stream class, and is called by any class that inherits from it (Wire, Serial, etc). See the Stream class page for more information.

Syntax

stream.findUntil(target, terminal)

Parameters

stream : an instance of a class that inherits from Stream.
target : the string to search for (char)
terminal : the terminal string in the search (char)

Returns

boolean

readBytesUntil()

 

Description

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 (see setTimeout()).
readBytesUntil() returns the number of characters placed in the buffer. A 0 means no valid data was found.
This function is part of the Stream class, and is called by any class that inherits from it (Wire, Serial, etc). See the Stream class page for more information.

Syntax

stream.readBytesUntil(character, buffer, length)

Parameters

stream : an instance of a class that inherits from Stream.
character : the character to search for (char)
buffer: the buffer to store the bytes in (char[] or byte[])
length : the number of bytes to read (int)

Returns

byte