lightthedreams
Published © LGPL

nRF24L01 for communication 1 way and 2 way

In this tutorial you will learn how to connect nRF24L01 for communication 1 way and 2 way

BeginnerProtip11,953
nRF24L01 for communication 1 way and 2 way

Things used in this project

Story

Read more

Schematics

1 way communication (transmitter)

1 way communication (receiver)

2 way communication (Arduino A)

2 way communication (Arduino B)

Code

1 way communication (transmitter)

Arduino
//communication 1 way
//firstly download library https://github.com/nRF24/RF24

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN         
const byte address[6] = "00001"; //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.
int button_pin = 2;
boolean button_state = 0;

void setup() {
pinMode(button_pin, INPUT);
radio.begin();                  //Starting the Wireless communication
radio.openWritingPipe(address); //Setting the address where we will send the data
radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
radio.stopListening();          //This sets the module as transmitter
}

void loop()
{
button_state = digitalRead(button_pin);
if(button_state == HIGH)
{
const char text[] = "Your Button State is HIGH";
radio.write(&text, sizeof(text));                  //Sending the message to receiver
}
else
{
const char text[] = "Your Button State is LOW";
radio.write(&text, sizeof(text));                  //Sending the message to receiver 
}
radio.write(&button_state, sizeof(button_state));  //Sending the message to receiver 
delay(10);
}

1 way communication (receiver)

Arduino
//communication 1 way
//firstly download library https://github.com/nRF24/RF24

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
int led_pin = 3;
boolean button_state = 0;

void setup() {
pinMode(led_pin, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data
radio.setPALevel(RF24_PA_MIN);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
radio.startListening();              //This sets the module as receiver
}

void loop()
{
if (radio.available())              //Looking for the data.
{
char text[32] = "";                 //Saving the incoming data
radio.read(&text, sizeof(text));    //Reading the data
radio.read(&button_state, sizeof(button_state));    //Reading the data
if(button_state == HIGH)
{
digitalWrite(led_pin, HIGH);
Serial.println(text);
}
else
{
digitalWrite(led_pin, LOW);
Serial.println(text);}
}
delay(5);
}

2 way communication (Arduino A)

Arduino
//nRF24L01 communication 2 ways Arduino A

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"};  //Setting the two addresses. One for transmitting and one for receiving
int button_pinA = 4;
int led_pinA = 3;
boolean button_stateA = 0;
boolean button_stateB = 0;

void setup() {
  pinMode(button_pinA, INPUT_PULLUP);
  pinMode(led_pinA, OUTPUT);
  radio.begin();                           //Starting the radio communication
  radio.openWritingPipe(addresses[1]);     //Setting the address at which we will send the data
  radio.openReadingPipe(1, addresses[0]);  //Setting the address at which we will receive the data
  radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. 
}

void loop() 
{  
  delay(5);
  radio.stopListening();                             //This sets the module as transmitter
  button_stateA = digitalRead(button_pinA);
  radio.write(&button_stateA, sizeof(button_stateA));  //Sending the data
  delay(5);
  
  radio.startListening();                            //This sets the module as receiver
  while(!radio.available());                         //Looking for incoming data
  radio.read(&button_stateB, sizeof(button_stateB)); //Reading the data
  if (button_stateB == LOW)
  {
    digitalWrite(led_pinA, HIGH);
  }
  else
  {
    digitalWrite(led_pinA, LOW);
  }
}

2 way communication (Arduino B)

Arduino
//nRF24L01 communication 2 ways Arduino B

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"};    //Setting the two addresses. One for transmitting and one for receiving
int button_pinB = 6;
int led_pinB = 5;
boolean button_stateA = 0;
boolean button_stateB = 0;

void setup() {
  pinMode(button_pinB, INPUT_PULLUP);
  pinMode(led_pinB, OUTPUT);
  radio.begin();                            //Starting the radio communication
  radio.openWritingPipe(addresses[0]);      //Setting the address at which we will send the data
  radio.openReadingPipe(1, addresses[1]);   //Setting the address at which we will receive the data
  radio.setPALevel(RF24_PA_MIN);            //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.
}

void loop() 
{
  delay(5);
  radio.startListening();                    //This sets the module as receiver
  if (radio.available())                     //Looking for incoming data
  {
    radio.read(&button_stateA, sizeof(button_stateA));
    if(button_stateA == LOW)
  {
     digitalWrite(led_pinB, HIGH);
  }
  else
  {
     digitalWrite(led_pinB, LOW);
  }
  delay(5);
  
  radio.stopListening();                           //This sets the module as transmitter
  button_stateB = digitalRead(button_pinB);
  radio.write(&button_stateB, sizeof(button_stateB));   //Sending the data
  }
}

Credits

lightthedreams

lightthedreams

8 projects • 12 followers
learn everyday from anywhere

Comments