Tuesday, March 19, 2019

nrf24l01 communication using arduino

transmitter code:


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


RF24 radio(7,8); // 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(1000);
}


receiver code:


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


RF24 radio(7,8); // CE, CSN
const byte address[6] = "00001";
boolean button_state = 0;
int led_pin = 5;

void setup() {
  pinMode(5, 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(5, HIGH);
      Serial.println(text);
    }
    else
    {
      digitalWrite(5, LOW);
      Serial.println(text);}
    }
    delay(5);
}

No comments:

Post a Comment

python class topic video