Help with NRF24 boards

I have two NRF 24 boards that I am trying to get to communicate. The goal is to have an ultrasonic sensor on one board report it's data to another board via the nrf board communications. Then have an led on the other board go off when the sensor reads below a value of 30. Here are the codes for the transmitter and receiver.

Receiver:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 13

RF24 radio(7, 8); // CE, CSN
byte address[6] = "00001";

boolean distance = 0;


void setup() { 
  Serial.begin(115200);
  pinMode(led,OUTPUT);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  
}

void loop() {
  
  while (!radio.available())
  radio.read(&distance, sizeof(distance));
  if (distance <= 30) {
    digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(led, LOW);
  }
  
}

Transmitter:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 13
#define trigPin 2
#define echoPin 3

int sound = 250;

RF24 radio(7, 8); // CE, CSN
byte address[6] = "00001";

boolean distance = 0;


void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  radio.write(&distance, sizeof(distance));

}

the program is not working at the moment, any help would be appreciated.

After trying seveal other tutorials I found Robin2's simple rf24 demo tutorial. There is a lot of good information there including advice about powering the radios. The example code worked right off.

  while (!radio.available())
    radio.read(&distance, sizeof(distance));

While there is no packet, read it.

That is the least sensible NRF24L01+ approach I've seen for a long time.

I loaded your code to 2 Unos with rf24 radios and connected a HCSR04 to the transmitter. I added serial prints so that you can see what is sent and received. My setup works with the revised code (below).

In the transmit code I added a timer because you really can't range that fast.

Transmit code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 13
#define trigPin 2
#define echoPin 3

int sound = 250;

RF24 radio(7, 8); // 
byte address[6] = "00001";

void setup()
{
   Serial.begin(115200);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   radio.begin();
   radio.openWritingPipe(address);
   radio.setPALevel(RF24_PA_MIN);
   radio.stopListening();
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 500;
   if (millis() - timer >= interval)
   {
      timer = millis();
      long duration, distance;
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      Serial.println(distance);
      radio.write(&distance, sizeof(distance));
   }
}

in the receive code, see changes in comments.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 4  // ********* pin 13 is SPI SCK used by rf24

RF24 radio(7, 8); // CE, CSN
byte address[6] = "00001";

long distance = 0;  // ***** should be long not boolean

void setup()
{
   Serial.begin(115200);
   pinMode(led, OUTPUT);
   radio.begin();
   radio.openReadingPipe(0, address);
   radio.setPALevel(RF24_PA_MIN);
   radio.startListening();
}

void loop()
{
   if (radio.available())  // *****  Changed to if and removed the !
   { // *********  added { to put following statements in if block
      radio.read(&distance, sizeof(distance));
      Serial.print("distance = ");
      Serial.println(distance);
      if (distance <= 30)
      {
         digitalWrite(led, HIGH);
      }
      else
      {
         digitalWrite(led, LOW);
      }
   }  // *********** added } to close if block
}

groundFungus:
I loaded your code to 2 Unos with rf24 radios and connected a HCSR04 to the transmitter. I added serial prints so that you can see what is sent and received. My setup works with the revised code (below).

In the transmit code I added a timer because you really can't range that fast.

Transmit code:

#include <SPI.h>

#include <nRF24L01.h>
#include <RF24.h>
#define led 13
#define trigPin 2
#define echoPin 3

int sound = 250;

RF24 radio(7, 8); //
byte address[6] = "00001";

void setup()
{
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop()
{
  static unsigned long timer = 0;
  unsigned long interval = 500;
  if (millis() - timer >= interval)
  {
      timer = millis();
      long duration, distance;
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      Serial.println(distance);
      radio.write(&distance, sizeof(distance));
  }
}





in the receive code, see changes in comments.



#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 4  // ********* pin 13 is SPI SCK used by rf24

RF24 radio(7, 8); // CE, CSN
byte address[6] = "00001";

long distance = 0;  // ***** should be long not boolean

void setup()
{
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop()
{
  if (radio.available())  // *****  Changed to if and removed the !
  { // *********  added { to put following statements in if block
      radio.read(&distance, sizeof(distance));
      Serial.print("distance = ");
      Serial.println(distance);
      if (distance <= 30)
      {
        digitalWrite(led, HIGH);
      }
      else
      {
        digitalWrite(led, LOW);
      }
  }  // *********** added } to close if block
}

Thankyou so much for the help so far, I am using these boards: https://www.amazon.com/Emakefun-RF-Nano-Board-ATmega328P-Micro-Controller-nRF24L01/dp/B07PQZJDW4/ref=sr_1_1?keywords=arduino%2Bnano%2Bnrf24&qid=1581447583&sr=8-1&th=1

AI am still having issues. I can get the ultrasonic sensor to turn on the led all on the same board on a separate
program, but I still cannot get this program to work. I copied you code and uploaded it into both of the boards and plugged the led in to D5 but still no luck. I know I have the correct board and processor selected. Also, whenever I hit the physical button on the board right below RF-NANO the led flashes. How can I solve this?

#define led 4  // ********* pin 13 is SPI SCK used by rf24

plugged the led in to D5

The LED should be connected to pin 4.

Can you post samples of the output from the serial prints in the transmit and receive programs.

I am not at all familiar with the RF_Nano platform. I will look at it to see how closely my setup will duplicate the RF_Nano.

OK, I found a schematic for the RF_Nano board showing the connections to the rf24 from the mega328. The CE and CSN pins are different from the ones in my posted code (and different from the pins in your OP).

i have duplicated your hardware as closely as i can and modified the code. Here is the revised and tested, successfully, code. Serial prints show the distance that is sent is received and the LED behaves according to the program. I changed the distance threshold to 10 because i don't have that much room.

Transmit code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
//#define led 13  // not used pin 13 is SPI SCK
#define trigPin 2
#define echoPin 3

//int sound = 250; // what is this?

RF24 radio(10, 9); //
byte address[6] = "00001";

void setup()
{
   Serial.begin(115200);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   radio.begin();
   radio.openWritingPipe(address);
   radio.setPALevel(RF24_PA_MIN);
   radio.stopListening();
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 500;
   if (millis() - timer >= interval)
   {
      timer = millis();
      long duration, distance;
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      Serial.println(distance);
      radio.write(&distance, sizeof(distance));
   }
}

Receive code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 4  // ********* pin 13 is SPI SCK used by rf24

RF24 radio(10, 9); // CE, CSN for rf_nano
byte address[6] = "00001";

long distance = 0;  // ***** should be long not boolean

void setup()
{
   Serial.begin(115200);
   pinMode(led, OUTPUT);
   radio.begin();
   radio.openReadingPipe(0, address);
   radio.setPALevel(RF24_PA_MIN);
   radio.startListening();
}

void loop()
{
   if (radio.available())  // *****  Changed to if and removed the !
   { // *********  added { to put following statements in if block
      radio.read(&distance, sizeof(distance));
      Serial.print("distance = ");
      Serial.println(distance);
      if (distance <= 10)
      {
         digitalWrite(led, HIGH);
      }
      else
      {
         digitalWrite(led, LOW);
      }
   }  // *********** added } to close if block
}

groundFungus:
OK, I found a schematic for the RF_Nano board showing the connections to the rf24 from the mega328. The CE and CSN pins are different from the ones in my posted code (and different from the pins in your OP).

i have duplicated your hardware as closely as i can and modified the code. Here is the revised and tested, successfully, code. Serial prints show the distance that is sent is received and the LED behaves according to the program. I changed the distance threshold to 10 because i don't have that much room.

Transmit code:

#include <SPI.h>

#include <nRF24L01.h>
#include <RF24.h>
//#define led 13  // not used pin 13 is SPI SCK
#define trigPin 2
#define echoPin 3

//int sound = 250; // what is this?

RF24 radio(10, 9); //
byte address[6] = "00001";

void setup()
{
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop()
{
  static unsigned long timer = 0;
  unsigned long interval = 500;
  if (millis() - timer >= interval)
  {
      timer = millis();
      long duration, distance;
      digitalWrite(trigPin, LOW);
      delayMicroseconds(2);
      digitalWrite(trigPin, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigPin, LOW);
      duration = pulseIn(echoPin, HIGH);
      distance = (duration / 2) / 29.1;
      Serial.println(distance);
      radio.write(&distance, sizeof(distance));
  }
}




Receive code:


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define led 4  // ********* pin 13 is SPI SCK used by rf24

RF24 radio(10, 9); // CE, CSN for rf_nano
byte address[6] = "00001";

long distance = 0;  // ***** should be long not boolean

void setup()
{
  Serial.begin(115200);
  pinMode(led, OUTPUT);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop()
{
  if (radio.available())  // *****  Changed to if and removed the !
  { // *********  added { to put following statements in if block
      radio.read(&distance, sizeof(distance));
      Serial.print("distance = ");
      Serial.println(distance);
      if (distance <= 10)
      {
        digitalWrite(led, HIGH);
      }
      else
      {
        digitalWrite(led, LOW);
      }
  }  // *********** added } to close if block
}

AWSOME! I had the pin changed to 5 when it didn't work the first time, that's why it was different. Thankyou so much for your help with this!