problem with communication within a group of nrf24l01 radio modules

Hello :slight_smile: ,

I try to make 4 arduino uno boards communicate by nrf24l01 radio modules.

Every board is wired with an LED as well as a button. By pressing the button the radio module should change from receive mode to transmit mode and make the LEDs of the other boards flash.

Since other codes I wrote work well, I know that the LEDs, the buttons and the radio modules are wired correctly.

My code looks like that:

#include  <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9, 10);
const uint64_t pipe1 = 0xE8E8F0F0E1LL;
const uint64_t pipe2 = 0xF0F0F0F0AA;
const uint64_t pipe3 = 0xF0F0F0F066;
const uint64_t pipe4 = 0xF0F0F0F0F0;
int SW1 = 7;
int LED1 = 3;
int interruptPin = 0;
int radionumber = 1;


void leuchten() {
  if (radio.available()) {
    radio.read(msg, 1);
    digitalWrite(LED1, HIGH);
  }

  if (!radio.available()) {
    digitalWrite(LED1, LOW);
    msg[0] = 0;
  }

}


void setup(void) {
  attachInterrupt(interruptPin, leuchten, FALLING);
  Serial.begin(115200);
  radio.begin();
  //radio.openWritingPipe(pipe);
  //radio.openReadingPipe(1, pipe);
  pinMode(LED1, OUTPUT);

  if (radionumber == 1) {
    radio.openWritingPipe(pipe1);
    radio.openReadingPipe(1, pipe2);
    radio.openReadingPipe(2, pipe3);
    radio.openReadingPipe(3, pipe4);
  }

  if (radionumber == 2) {
    radio.openWritingPipe(pipe2);
    radio.openReadingPipe(1, pipe1);
    radio.openReadingPipe(2, pipe3);
    radio.openReadingPipe(3, pipe4);
  }

  if (radionumber == 3) {
    radio.openWritingPipe(pipe3);
    radio.openReadingPipe(1, pipe1);
    radio.openReadingPipe(2, pipe2);
    radio.openReadingPipe(3, pipe4);
  }

  if (radionumber == 4) {
    radio.openWritingPipe(pipe4);
    radio.openReadingPipe(1, pipe1);
    radio.openReadingPipe(2, pipe2);
    radio.openReadingPipe(3, pipe3);
  }

}


void loop(void) {
  if (digitalRead(SW1) == HIGH) {
    radio.stopListening();
    Serial.println("send");
    msg[0] = 1;
    radio.write(msg, 1);
  }

  else {
    radio.startListening();
    Serial.println("receive");
    Serial.println(msg[0]);

  }
}

I adapt the variable radionumber for every board of course.

The serial monitor displays "receive" and "0". Pressing the button causes the monitor to display "send" several times, but then nothing more happens. The program seems to abort. No LED flashes.

Does anybody notice a mistake in my setup or code?

You should probably check the return value to see if radio.write() was successful.

  if (digitalRead(SW1) == HIGH) {
    radio.stopListening();
    Serial.print("send: ");
    msg[0] = 1;
    if (radio.write(msg, 1))
        Serial.println("Success");
    else
        Serial.println("Failure");
  }

Somehow this seems to be the problem. :confused:

radio.write() fails every time but the LED of the same Arduino shortly flashes with every failed operation.

Additionally the board still seems to abort after some seconds.

Any idea why radio.write(msg, 1) fails? radio.write(msg[0], 1) doesn't work either?

By setting some "Serial.println"s I found out that the board jumps into the interrupt function every time it tries to send.

Is it possible that "radio.stopListening" isn't working and the radio module somehow listens to itself?

Maybe the examples in this Simple nRF24L01+ Tutorial will help.

I suggest getting your project to work with two devices first to keep the debugging as simple as possible.

In your final project will it be necessary for each device to initiate communication?

IMHO it will be much easier if one device acts as master and the others as slaves. The second example in my tutorial can easily be extended to work with several slaves.

...R

The interrupt signal happens on send success and send fail as well as received data available. That is why there is a .whatHappened() function that you are supposed to call in your interrupt handler to see what caused the interrupt.