Hello All,

In this next part of the communication-series, we will be covering standard RF communication over the 2.4GHz spectrum.

Introduction

The 2.4GHz Spectrum is called the ISM(Industrial, Scientific and Medical) band.It is the most widely used band for wireless communication and is used in Wi-Fi, Bluetooth and most other commercial wireless solutions.

In this tutorial, we will be using the nRF24L01 module by Nordic Semiconductor. This is a very low cost (<$5) ultra low power RF transceiver capable of data rates up to 2Mbps. The module I will use is an SPI module making it compatible with any MCU with SPI. In this example I will wirelessly communicate between 2 Arduinos to turn an LED on or off.

Things Needed

  • 2 Arduinos
  • An LED or any device that can show output from an Arduino

Wiring it Up

To get setup, we will wire up 2 Arduino's Identically.

We shall connect the SPI pins(MOSI, MISO and SCK/SCLK) from the module to the SPI pins on the Arduino, and connect 2 additional pins for CE(Chip Enable) and CSN(Slave Select). We should also wire up the GND to GND and VCC to 3.3V on the Arduino. The pin connections should be:

nRF24L01 Arduino Description
1 GND GND
2 3v3 VCC
3 9 CE
4 10 CSN
5 13 SCK
6 11 MOSI
7 12 MISO
8 NC IRQ

The Schematic is:

My Setup looks like:

For the 2nd Arduino, we must also connect an LED to Pin 2.

The Schematic becomes:

My Setup looks like:

Code

The code involves initializing the Radio using the RF24 library. We also initialize the Serial Port so that we can use the Serial monitor or a Serial terminal software like CoolTerm. We must also define a Pipe, this is the channel to communicate on, all radio's listening on this pipe will receive all transmissions sent on it.

Sender

The sender will take an input from the serial console and send it out through the radio to the receiver. The code is:

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

#define CE_PIN   9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL;

char input;

RF24 radio(CE_PIN, CSN_PIN);

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

void loop()
{
  if (Serial.available())
  {
    input = Serial.read();
  }
  if(input != '\0') {
    Serial.print(F("\nSending: "));
    Serial.println((int)input);
    if (!radio.write( &input, sizeof(input))){
       Serial.println(F("failed"));
     } else {
       Serial.println(F("Sent"));
     }
     input = '\0';
  }   
}

Receiver

The receiver listens for inputs on the pipe. When it gets the keywords it is programmed to look for(in this 0 or 1), it then turns the LED on or off.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10
#define LED_PIN 2

const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
char got_data;
char last_data;

RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
void setup() {
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();

}

void loop() {
  if ( radio.available() )
  {
    if ( radio.available()) {                                                     
      while (radio.available()) {
        radio.read( &got_data , sizeof(got_data) );
      }
      Serial.print(F("Received: "));
      Serial.println(got_data);     
      if(got_data == '0') {
        if(last_data != '0') {
          Serial.println(F("Turning LED off"));
          digitalWrite(LED_PIN, 0);
          last_data = got_data;
        }
      } else if(got_data == '1') {
        if(last_data != '1') {
          Serial.println(F("Turning LED on"));
          digitalWrite(LED_PIN, 1);
          last_data = got_data;
        }
      }
    }
  }
}

Usage

Once you upload these sketches to your Arduinos, you can use a serial terminal to send a requisite command from the Sender to the Receiver, and viola you will see the LED turn on and off based on your commands.

Conclusion

This is a very convenient and cost effective way to wirelessly transfer data between devices, I have used these radios to control everything from light switches to robotic arms, and they have worked like a charm. One point to note is that these radios do not come built in with any security, so if someone with RF knowledge were to sniff out our data packets, they could easily hijack your pipe and compromise anything that you control! If you you want to use this for security sensitive applications, I would highly recommend increasing security by using some sort of encryption.

I would love to hear about what you used these radios for.