Transmitting radio frequency waves

After installing the RF library, we are now all set to start writing the sketches. In this section, we will learn how to transmit radio frequency waves. We will use the following C sketch for transmitting radio frequency waves as shown below :

#include <SPI.h> 
#include "RF24.h"

int msg[1];// use this for transmitting integers
//char msg[1];// use this for transmitting characters

RF24 radio(8,7);
const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup(void)
{
radio.begin();
radio.openWritingPipe(pipe);
}

void loop(void)
{
msg[0] = 1; // integer will be sent
//msg[0] = 'b'; // character will be sent
radio.write(msg, 1);
}

Now let us try to understand the preceding program in detail. This sketch uses two Arduino header files and includes them at the beginning of the sketch:

#include <SPI.h> 
#include "RF24.h"

The SPI.h is the Arduino header file that is used for serial communications between microcontroller chips. The purpose of using this library is to write bytes onto the nRF24L01 chip from Arduino.

The second Arduino library, RF24.h contains all the low-level code that is necessary to transmit radio frequency waves. We are using the functions in this library to interface our Arduino sketch with the nRF24L01 chip for transmitting RF signals.

The next step is to declare the object for the RF chip. This line of code is used to create an object of type RF24. The RF24 type is defined in the Arduino library. The arguments 8 and 9 are Arduino's digital pins that will be connected to the nRF24L01 chip:

RF24 radio(8,7); 

In the next line of code, we will declare an address which will serve as a communication pipe. This address space will be used as a buffer to store the data to be transmitted:

const uint64_t pipe = 0xE8E8F0F0E1LL; 

The radio device object is initialized in the setup() function by beginning the communication channel and then issuing a command to open a pipe for writing data to the RF chip:

radio.begin();// start radio device object 
radio.openWritingPipe(pipe);// open data transfer pipe

The main logic of the program is very simple and straightforward. First, we must declare a variable to hold a piece of information:

msg[0] = 1;//integer value 1 will be sent 

In the preceding line of code, an integer type array variable is being initialized with the integer value 1 as the data. We are going to transmit this information via radio frequency by issuing the following write command to the serially attached nRF24L01 chip:

radio.write(msg, 1);// data gets transmitted via radio waves
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.218.230