NRF PUSH BUTTON CODE







For transmitter:-







#include "nRF24L01.h" 
#include "RF24.h"
#include "SPI.h"

#define SwitchPin 2

int SentMessage[1] = {000};
RF24 radio(9, 10); // NRF24L01 used SPI pins + Pin 9 and 10 on the NANO

const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01

void setup()
{
  pinMode(SwitchPin, INPUT_PULLUP);
  digitalWrite(SwitchPin, HIGH);

  radio.begin(); // Start the NRF24L01
  radio.openWritingPipe(pipe); // Get NRF24L01 ready to transmit
}

void loop()
{
  if (digitalRead(SwitchPin) == LOW)    // If switch is pressed
  {
    SentMessage[0] = 111;
    radio.write(SentMessage, 1);      // Send pressed data to NRF24L01
  }
  else
  {
    SentMessage[0] = 000;
    radio.write(SentMessage, 1);      // Send idle data to NRF24L01
  }
}



For Receiver :-





#include "nRF24L01.h" 
#include "RF24.h"
#include "SPI.h"

#define LED_PIN 2

int ReceivedMessage[1] = {000}; // Used to store value received by the NRF24L01
RF24 radio(9,10); // NRF24L01 SPI pins. Pin 9 and 10 on the Nano

const uint64_t pipe = 0xE6E6E6E6E6E6; // Needs to be the same for communicating between 2 NRF24L01 

void setup(void)
{
  radio.begin(); // Start the NRF24L01
  
  radio.openReadingPipe(1,pipe); // Get NRF24L01 ready to receive
  
  radio.startListening(); // Listen to see if information received
  
  pinMode(LED_PIN, OUTPUT); 
}

void loop(void)
{
  while (radio.available())
  {
    radio.read(ReceivedMessage, 1); // Read information from the NRF24L01

    if (ReceivedMessage[0] == 111) // Indicates switch is pressed
    {
      digitalWrite(LED_PIN, HIGH);
    }
    else
    {
       digitalWrite(LED_PIN, LOW);
    }
    delay(2);
   }
}



✅✅✅✅✅✅✅✅✅✅✅✅\

Default Connections :

MOSI MISO SCK Arduino Uno = 11 12 13 Arduino Nano = 11 12 13 Arduino Mega = 51 50 52













Comments

Popular posts from this blog

LoRa Module HC12 Demo Code (push button)