NRF24L01 transceivers do not communicate problem

Hi all,

I have been working on the NRF24L01 transceivers with the duck antennae.
My purpose is to light the LED of the opposite transceiver by pressing the push button.
I connected the pins of the transceiver to the Arduino Nano respectively for the pair. And included a capacitor between the V+ and GND.

V+ to 3.3V
GND to GND
CSN to D7
CE to D8
MOSI to D11
MISO to D12
SCK to D13

I connected the push button to D2 and the LED to D9 of the Arduino Nano.

Below is my code for the transceiver 1

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define button 2
#define led 9
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
int buttonstate = 0;
int receivestate = 0;

void setup()
{
  pinMode(2, INPUT);
  pinMode(9, OUTPUT);
  radio.begin();
  radio.openWritingPipe(addresses[1]); // 00002
  radio.openReadingPipe(1, addresses[0]); // 00001
  radio.setPALevel(RF24_PA_MAX);
}

void loop()
{
  delay(5);
  radio.startListening();
  if ( radio.available())
  {
    while (radio.available())
    {
      radio.read(&receivestate, sizeof(receivestate));
      if (receivestate == HIGH)
      {
         digitalWrite(led, HIGH);
      }
      if (receivestate == LOW)
      {
         digitalWrite(led, LOW);
      }
    }
    delay(5);
    radio.stopListening();
    buttonstate = digitalRead(button);
    radio.write(&buttonstate, sizeof(buttonstate));
  }
}

Below is the code for transceiver 2

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define button 2
#define led 9
RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
int buttonstate = 0;
int receivestate = 0;

void setup()
{
  pinMode(2, INPUT);
  pinMode(9, OUTPUT);
  radio.begin();
  radio.openWritingPipe(addresses[0]); // 00001
  radio.openReadingPipe(1, addresses[1]); // 00002
  radio.setPALevel(RF24_PA_MAX);
}

void loop()
{
  delay(5);
  radio.startListening();
  if ( radio.available())
  {
    while (radio.available())
    {
      radio.read(&receivestate, sizeof(receivestate));
      if (receivestate == HIGH)
      {
         digitalWrite(led, HIGH);
      }
      if (receivestate == LOW)
      {
         digitalWrite(led, LOW);
      }
    }
    delay(5);
    radio.stopListening();
    buttonstate = digitalRead(button);
    radio.write(&buttonstate, sizeof(buttonstate));
  }
}

The 2 codes are the same in the void loop().

The result I got is that the LEDs did not follow the push button command, they blink randomly.
Please help.

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

There is also a connection test program to check that the Arduino can talk to the nRF24 it is connected to.

A common problem with nRF24 modules is insufficient 3.3v current from the Arduino 3.3v pin. The high-power nRF24s (with the external antenna) will definitely need an external power supply. At least for testing try powering the nRF24 with a pair of AA alkaline cells (3v) with the battery GND connected to the Arduino GND.

If you are using the high-power nRF24s (with the external antenna) make sure there is sufficient distance between the two nRF24 so that the signal does not overwhelm the receiver - try 3 metres separation. If you are new to nRF24s it may be better to start with a pair of low power modules with the pcb antenna.

...R