Working with the Nrf24l01

Hello everyone

I am trying to transfer data with the Nrf24l01. I’m working with an Arduino Mega for shipping and an Arduino Uno for reading. I have never been able to create or find a working program on the internet. I’ve done everything as stated in this topic: Simple nRF24L01+ 2.4GHz transceiver demo.

I don’t know the Arduino Mega is properly connected.
VCC - 3.3V
GND-GND
CE - PWMpin 10
CSN - PWMpin 9
SCK - DIGITALpin 52
MOSI - DIGITALpin 51
MISO - DIGITALpin 50

Message printed at the sender: “Tx failed”
Message printed to the reader: “SimpleRx Starting”

program transmitter (MEGA)

// SimpleTx - the master or the transmitter

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>


#define CE_PIN   9
#define CSN_PIN 10

const byte slaveAddress[5] = {'R','x','A','A','A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataToSend[10] = "Message 0";
char txNum = '0';


unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SimpleTx Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
}

//====================

void loop() {
    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
        prevMillis = millis();
    }
}

//====================

void send() {

    bool rslt;
    rslt = radio.write( &dataToSend, sizeof(dataToSend) );
        // Always use sizeof() as it gives the size as the number of bytes.
        // For example if dataToSend was an int sizeof() would correctly return 2

    Serial.print("Data Sent ");
    Serial.print(dataToSend);
    if (rslt) {
        Serial.println("  Acknowledge received");
        updateMessage();
    }
    else {
        Serial.println("  Tx failed"); 
    }
}

//================

void updateMessage() {
        // so you can see that new data is being sent
    txNum += 1;
    if (txNum > '9') {
        txNum = '0';
    }
    dataToSend[8] = txNum;
}

program reader

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define CE_PIN   9
#define CSN_PIN 10

const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);

char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

//=============

void loop() {
    getData();
    showData();
}

//==============

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        newData = false;
    }
}

Thanks for helping me.
(ps: sorry for the grammar, I used Google-Translate :grin:)

I attached a rf24 module to my Mega and another to an Uno wired according to your code. I uploaded your code, without any changes, to the Mega and Uno. The code works fine on my setup.

The transmitter (Mega) prints in serial monitor:

Data Sent Message 0 Acknowledge received
Data Sent Message 1 Acknowledge received
Data Sent Message 2 Acknowledge received
Data Sent Message 3 Acknowledge received
Data Sent Message 4 Acknowledge received

From the reciever serial monitor prints:

Data received Message 0
Data received Message 1
Data received Message 2
Data received Message 3
Data received Message 4

So the code looks OK.

Looks like you may have miswired the CE and CSN pins?
In the text of your post (Mega wiring):

CE - PWMpin 10
CSN - PWMpin 9

In your code:

#define CE_PIN   9
#define CSN_PIN 10

Reply #30 in the simple rf24 tutorial has a program to test the wired connection between the processor and the radio module.

Well, except that there is no code to perform any multiplexing, or light any LEDs… :wink:

Sorry, multiplexing what and lighting which LEDs? You seem to be a better mind reader than I.

OK I got it. More coffee, please. I flagged the post and maybe it will be moved to a more appropriate section.

@wildplasser, your topic has been moved to a more suitable location.

I often wonder why we don’t rename the sections “Post WTF you like #1” to “Post WTF you like #9” ! :rofl:

1 Like

Is 3.3 volt good enough for a Mega?

@ Railroader has a good point. The number one problem with using the rf24 modules is adequate power as pointed out in Robin2’s simplerf24 tutorial. My rf24 modules have their own 3.3V regulators (LM1117 3.3) powered from the Arduino 5V. Like these adapters.

I have checked the wirering of the Arduino with this program (#30)

The Serial monitor prints this, but I don't know what it means:

CheckConnection Starting

FIRST WITH THE DEFAULT ADDRESSES after power on
Note that RF24 does NOT reset when Arduino resets - only when power is removed
If the numbers are mostly 0x00 or 0xff it means that the Arduino is not
communicating with the nRF24

SPI Speedz = 10 Mhz
STATUS = 0x00 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=0 TX_FULL=0
RX_ADDR_P0-1 = 0x0000000000 0x0000000000
RX_ADDR_P2-5 = 0x00 0x00 0x00 0x00
TX_ADDR = 0x0000000000
RX_PW_P0-6 = 0x00 0x00 0x00 0x00 0x00 0x00
EN_AA = 0x00
EN_RXADDR = 0x00
RF_CH = 0x00
RF_SETUP = 0x00
CONFIG = 0x00
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1 MBPS
Model = nRF24L01+
CRC Length = Disabled
PA Power = PA_MIN
ARC = 0

AND NOW WITH ADDRESS AAAxR 0x41 41 41 78 52 ON P1
and 250KBPS data rate

SPI Speedz = 10 Mhz
STATUS = 0x00 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=0 TX_FULL=0
RX_ADDR_P0-1 = 0x0000000000 0x0000000000
RX_ADDR_P2-5 = 0x00 0x00 0x00 0x00
TX_ADDR = 0x0000000000
RX_PW_P0-6 = 0x00 0x00 0x00 0x00 0x00 0x00
EN_AA = 0x00
EN_RXADDR = 0x00
RF_CH = 0x00
RF_SETUP = 0x00
CONFIG = 0x00
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1 MBPS
Model = nRF24L01+
CRC Length = Disabled
PA Power = PA_MIN
ARC = 0

I would say that there is a problem with communication between the processor and the radio module. Like Robin says in the post, if there are all zeros, there is a problem. Either bad module(s) or wiring. I actually got a batch of bad modules a couple of years ago. They would send but not receive ( or vice-versa, I don't remember).

Post clear photos of the wiring of your modules. Make sure that we can see where each wire goes. A photo of jumbled wiring has no value.

Did you see the issue with the CE and CSN pins that I pointed out in reply #2?