Arduino Mega + Nrf24L01+

I've been trying to get the Nrf24L01 to work for several days now without much success.
I want to send data of a joystick from one Arduino to another one. I also tried adding a 100uF capacitor on each arduino. The only thing I'm receiving are a bunch of zeros or wrong numbers.

Transmitter Code:

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

RF24 radio(9,10);
byte addresses[][6] = {"1Node","2Node"};

int joystick[2];

void setup()
{
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1,addresses[0]);
radio.stopListening();
}

void loop()
{
  joystick[0] = analogRead(A0);
  joystick[1] = analogRead(A1);
  Serial.print("X = ");
  Serial.println(joystick[0]);
  Serial.print("Y = ");
  Serial.println(joystick[1]); 
  radio.write(joystick,sizeof(joystick));
  delay(500);
}

Receiver Code:

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

RF24 radio(9,10);
byte addresses[][6] = {"1Node","2Node"};

int joystick[2];

void setup()
{
Serial.begin(9600);
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1,addresses[1]);
radio.startListening();
}

void loop()
{
while(radio.available())
{
radio.read(joystick, sizeof(joystick));
Serial.print("X = ");
Serial.println(joystick[0]);
Serial.print("Y = ");
Serial.println(joystick[1]);
}
}

Which RF24 library did you pick?

You have two Megas?

How did you connect the NRFs?

I miss a pinMode(53, OUTPUT) for the Mega(s).

On the Arduino Duemilanove and other ATmega168 / 328-based boards, the SPI bus uses pins 10 (SS), 11 (MOSI), 12 (MISO), and 13 (SCK). On the Arduino Mega, this is 50 (MISO), 51 (MOSI), 52 (SCK), and 53 (SS). Note that even if you're not using the SS pin, it must remain set as an output; otherwise, the SPI interface can be put into slave mode, rendering the library inoperative.

This Simple nRF24L01+ Tutorial may be of interest.

However as @Whandall has pointed out you will need to use the correct pins for the Mega.

...R

I have set the ce pin as pin 9 and csn pin as pin 53 and defined pin 53 as output but it still doesnt work

DE67:
I have set the ce pin as pin 9 and csn pin as pin 53 and defined pin 53 as output but it still doesnt work

You have not told us what other pins on the Mega the nRF24 is connected to. The pins 11, 12 and 13 that are used on an Uno will NOT work on a Mega.

...R

Of course they are connected on pins 50 to 53 like they are supposed to be

Have you tried my examples? They worked for me on my Mega. Debugging wireless can be very difficult and it is easier to help when you are using code I am familiar with.

...R

I tried your code. I also added an external 3.3V regualtor. Unfortanetly your code doesn't seem to work for me. The transmitter tries to transmit but always fails and the Receiver doesn't show anything except for simple rx starting. The changes I made in your code is that is set ce to 9 and csn to 53. I also tried to set csn pin as output.

DE67:
I tried your code. I also added an external 3.3V regualtor. Unfortanetly your code doesn't seem to work for me. The transmitter tries to transmit but always fails and the Receiver doesn't show anything except for simple rx starting.

If you post the exact code (both programs) that YOU uploaded to your Megas (are you using 2 Megas?) and examples of the output from the two programs I will try to help.

...R

Yes I'm using two Megas.

Transmitter:

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


#define CE_PIN   9
#define CSN_PIN 53

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);
    pinMode(53,OUTPUT);
    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;
}

Receiver:

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

#define CE_PIN   9
#define CSN_PIN 53

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);
    pinMode(53,OUTPUT);
    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;
    }
}

And what output do you see in the Serial Monitor from the two Megas?

It is best if you can monitor both at the same time, either with two instances of the Arduino IDE or with another terminal program such as puTTY

I will check your code with my Mega later today or tomorrow.

...R

Sorry I forgot to post the output.

On the Transmitter I get a bunch of : Data Sent Message 0 Tx failed
On the Receiver I only see : SimpleRx Starting but only once nothing else

Hey there,

Maybe this video can help you out a bit, as it uses a mega and the NRF2401 + joystick modules too, hope it helps.

I have now tried your programs on my Mega and Uno and they work fine, regardless of which of them is the TX.

One thing that is worth doing is disconnecting and reconnecting power to the Arduino after uploading the program just to make sure the nRF24 resets.

If that does not help then I suggest you check your connections again or consider the possibility that one of your nRF24s is faulty.

...R

Yes after trying the code from the video I guess something is broken so I'm rebuying the modules and hopefully this will help