
Description:
So we need to include the basic SPI and the newly installed RF24 libraries and create an
RF24 object. The two arguments here are the CSN and CE pins.
1.RF24 radio(7, 8); // CE, CSN
Next we need to create a byte array which will represent the address, or the so called pipe
through which the two modules will communicate.
1.const byte address[6] = "00001";
We can change the value of this address to any 5 letter string and this enables to choose
to which receiver we will talk, so in our case we will have the same address at both the
receiver and the transmitter.
In the setup section we need to initialize the radio object and using the
radio.openWritingPipe() function we set the address of the receiver to which we will send
data, the 5 letter string we previously set.
1.radio.openWritingPipe(address);
On the other side, at the receiver, using the radio.setReadingPipe() function we set the
same address and in that way we enable the communication between the two modules.
1.radio.openReadingPipe(0, address);
Then using the radio.setPALevel() function we set the Power Amplifier level, in our case I
will set it to minimum as my modules are very close to each other.
1.radio.setPALevel(RF24_PA_MIN);
Note that if using a higher level it is recommended to use a bypass capacitors across GND
and 3.3V of the modules so that they have more stable voltage while operating.
Next we have the radio.stopListening() function which sets module as transmitter, and on
the other side, we have the radio.startListening() function which sets the module as
receiver.
1.// at the Transmitter
2.radio.stopListening();
1.// at the Receiver
2.radio.startListening();
In the loop section, at the transmitter, we create an array of characters to which we assign
the message “Hello World”. Using the radio.write() function we will send that message to
the receiver. The first argument here is the variable that we want to be sent.
1.void loop() {
2.const char text[] = "Hello World";
3.radio.write(&text, sizeof(text));
4.delay(1000);
5.}