Problems with nrf24l01 & Nano/Mega

I’m trying to Connect a Nano to a Mega, Via. the Nrf24l01 modules, and Keep getting error message from the Mega “init failed

I think the problem may be in that I may have to connect the nrf24l01 to different pins on the mega then On the nano, but this is my first time using a Mega and I’m still relatively new to most of this.

It may have something to do with the Mosi, ss, and miso pins on the Mega being different. also a conflict in the future where I may need 2 as the DFplayer mini uses them as well, but I don’t really understand any of that quite yet.

(Ultimately I’m trying to create a remote control using a series of buttons and a joystick module on the nano to control several functions, leds, a DFplayermini, and motors on the Mega)

For learning purposes I was trying to follow a video online and did the following:

I’m using the Radiohead Server and client examples to test and get started.

I have the Nrf24l01(s) connected to both boards Via. the 5v adapter modules

Pins are as follows for BOTH:

ce 8
csn 10
sck 13
mo 11
mi 12
iro xx (not used)

I have the Mega running the “server” example
and the Nano (remote control) running the “client” example.

Sketches:

// nrf24_server.pde
// -- mode: C++ --
// Example sketch showing how to create a simple messageing server
// with the RH_NRF24 class. RH_NRF24 class does not provide for addressing or
// reliability, so you should only use RH_NRF24 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf24_client
// Tested on Uno with Sparkfun NRF25L01 module
// Tested on Anarduino Mini (Anarduino Mini Details) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup()
{
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
if (!nrf24.init())
Serial.println(“init failed”);
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println(“setChannel failed”);
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println(“setRF failed”);
}

void loop()
{
if (nrf24.available())
{
// Should be a message for us now
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len))
{
// NRF24::printBuffer("request: ", buf, len);
Serial.print("got request: ");
Serial.println((char*)buf);

// Send a reply
uint8_t data = “And hello back to you”;
nrf24.send(data, sizeof(data));
nrf24.waitPacketSent();
Serial.println(“Sent a reply”);
}
else
{
Serial.println(“recv failed”);
}
}
}

// nrf24_client.pde
// -- mode: C++ --
// Example sketch showing how to create a simple messageing client
// with the RH_NRF24 class. RH_NRF24 class does not provide for addressing or
// reliability, so you should only use RH_NRF24 if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example nrf24_server.
// Tested on Uno with Sparkfun NRF25L01 module
// Tested on Anarduino Mini (Anarduino Mini Details) with RFM73 module
// Tested on Arduino Mega with Sparkfun WRL-00691 NRF25L01 module

#include <SPI.h>
#include <RH_NRF24.h>

// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini

void setup()
{
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
if (!nrf24.init())
Serial.println(“init failed”);
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println(“setChannel failed”);
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println(“setRF failed”);
}

void loop()
{
Serial.println(“Sending to nrf24_server”);
// Send a message to nrf24_server
uint8_t data = “Hello World!”;
nrf24.send(data, sizeof(data));

nrf24.waitPacketSent();
// Now wait for a reply
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);

if (nrf24.waitAvailableTimeout(500))
{
// Should be a reply message for us now
if (nrf24.recv(buf, &len))
{
Serial.print("got reply: ");
Serial.println((char*)buf);
}
else
{
Serial.println(“recv failed”);
}
}
else
{
Serial.println(“No reply, is nrf24_server running?”);
}
delay(400);
}

Can anyone help explain what Im doing wrong, if I’m on the right track, and/or if there is a simpler way to understand what I’m trying to do?

Thank you!

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.

The programs work fine with a Mega as long as you use the correct pins for SPI.

...R

The 3v was not my problem, as I mentioned before I am using the 5v adapter module with the nrf24’s

I was however able to get things working using this tutorial howtomechatronics.com, and the nrf24 library
The problem was as I suspected, my connections on the Mega side.

I AM having trouble understanding however, the code necessary to send analog and digital commands between Arduinos.

I have a sketch I made using a series of pushbuttons connected to Pin A2 Via resistors, that reads the value of the pin and uses it to light an led. (in this short code just to indicate button was pressed and value was read correctly)

I’d like to have each push button (using this method) play a sound Via the second Arduino.

But first Can anyone help me understand how to use this concept on the transmitter to light and led on the Receiver pin 3 using the nrf24l01(s)?
I’d also like the keep the “statusLed” (in the sketch) on the transmitter if possible.

I’d appreciate any help you could offer.

Thank You

Analog buttons sketch:

const int statusLed = 2;

//Variables
int buttonValue; //Stores analog value when button is pressed

void setup()
{
pinMode(statusLed, OUTPUT);
}

void loop()
{
buttonValue = analogRead(A2); //Read analog value from A0 pin

//Button #1:
if (buttonValue>=1013 && buttonValue<=1015){
digitalWrite(statusLed, HIGH);
}
//Button #2:
else if (buttonValue>=612 && buttonValue<=614){
digitalWrite(statusLed, HIGH);
}
//Button #3:
else if (buttonValue>=993 && buttonValue<=995){
digitalWrite(statusLed, HIGH);
}
//Button #4:
else if (buttonValue>=770 && buttonValue<=771){
digitalWrite(statusLed, HIGH);
}
//Button #5:
else if (buttonValue>=974 && buttonValue<=975){
digitalWrite(statusLed, HIGH);
}
//Button #6:
else if (buttonValue>=805 && buttonValue<=807){
digitalWrite(statusLed, HIGH);
}
//Button #7:
else if (buttonValue>=947 && buttonValue<=948){
digitalWrite(statusLed, HIGH);
}
//Button #8:
else if (buttonValue>=841 && buttonValue<=843){
digitalWrite(statusLed, HIGH);
}
//Button #9:
else if (buttonValue>=931 && buttonValue<=932){
digitalWrite(statusLed, HIGH);
}
//Button #10:
else if (buttonValue>=870 && buttonValue<=872){
digitalWrite(statusLed, HIGH);
}
//No button pressed, turn off LEDs
else{
digitalWrite(statusLed, LOW);
}
}


BubbleHockey:
I was however able to get things working using this tutorial howtomechatronics.com, and the nrf24 library
The problem was as I suspected, my connections on the Mega side.

I AM having trouble understanding however, the code necessary to send analog and digital commands between Arduinos.

It will be much easier for me to help if you base your code on the examples in my Tutorial - examples that I am familiar with. If you study those examples you should get a good idea how to send numbers from one to the other.

…R

Robin2:
It will be much easier for me to help if you base your code on the examples in my Tutorial - examples that I am familiar with. If you study those examples you should get a good idea how to send numbers from one to the other.

...R

I don't have anything written for the Nrf24 side of the project. Just the above sketch that uses values from pin A2 to use the single pin to accommodate 10 buttons.

The issue is that, tho Ive tried several examples and they all work with the correct pin layout, regardless of the example, I cant seam to wrap my head around translating the above code into something that works.

Please go back to your earlier Posts and edit them to use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor See How to use the forum

...R