PIR sensor + NRF24L01

Hi
My project is sending data wirelessly from using the PIR sensor as the INPUT and made the LED light on another side as OUTPUT and it is two ways communication. So I tried to edit the code from this site which using a button as INPUT but it still doesn't work.
Any suggestion or topic that I should read.

Appreciate your help.

// First Arduino
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses [][6] = {"00001", "00002"};  //Setting the two addresses. One for transmitting and one for receiving
int pir_pinA = 5;
int led_pinA = 13;
boolean valA = 0;
boolean valB = 0;

void setup() {
  pinMode(pir_pinA, INPUT);
  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. 
  Serial.begin(9600);
}

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

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(7, 8); // CE, CSN
const byte addresses [][6] = {"00002", "00001"};  //Setting the two addresses. One for transmitting and one for receiving
int pir_pinB = 5;
int led_pinB = 13;
boolean valA = 0;
boolean valB = 0;

void setup() {
  pinMode(pir_pinB, INPUT);
  pinMode(led_pinB, 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. 
  Serial.begin(9600);
}

void loop() 
{  
 radio.startListening();                    //This sets the module as receiver
  while(!radio.available());                     //Looking for incoming data
  radio.read(&valA, sizeof(valA));
    
    if(valA == LOW)
  {
     digitalWrite(led_pinB, HIGH);
  }
  else
  {
     digitalWrite(led_pinB, LOW);
  }
  delay(5);
  
  radio.stopListening();                           //This sets the module as transmitter
  radio.write(&valB, sizeof(valB));    //Sending the data
  }
1 Like

You send valB without ever changing it...

You transmit side never progresses until receiving a packet - why would it ever receive a packet, its supposed to transmit...

Did you mean that I have to put valB = digitalRead(pir_pinB); in the second code right?

I try to made the two ways communication so both side have to be transmitter and receiver
if it what you mean

Thanks

Just don't.

Use the built-in autoack to make sure your message was received.

Your blocking approach is not the way to go.

Put more simply change:

to

  if (radio.available())
  {
    // process incoming packet only if there is one, no busy-waiting.
  }

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.