Unable to send integer over nRF24L01

I am trying to constantly send integers from a single analog input pin (only one pin for now, but I want to end up sending three 'simultaneously') from a Nano through two nRF24L01's to a Mega to control a servo. I use a joystick module as input. The transceivers are the larger versions with antennas.

I have tried everything on forums I could find (and remotely understand) to make it work, but I haven't been able to move the servo with the joystick. The farthest I've been able to get was that the Nano (transmitter) was correctly printing the analogread value from the analog pin and the TX light keeps flashing like a marathon runner. On the Mega (receiver) side, the RX light is constantly on, but it is printing the 'received' value as 0 regardless of what I do with the Nano. The Mega is moving the servo when I manually enter a value for the servo, but only moving the servo to position 0 when using the code below.

Here is my transmitter code (Nano)

//transmitter

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

  int joystick;
  RF24 radio(7, 8);
  const uint16_t pipe = "00010";
  
  
  
void setup() {

  Serial.begin(9600);
  radio.begin(); 
  radio.openWritingPipe(pipe);
  radio.printDetails();
  radio.stopListening();
  pinMode(A4, INPUT);
  
}

void loop() {

      joystick = analogRead(A4);
      Serial.println(joystick);
      radio.setRetries(5,5);
      radio.write(&joystick, sizeof(joystick));
      delay(20);
        
      
}

Here is my receiver code (Mega)

//receiver


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


  int joystick;
  int servoPin = 12;  
  Servo servo;
  int servoAngle = 0;
  RF24 radio(7, 8);   
  const uint16_t pipe = "00010";     

  
  
void setup() {

  Serial.begin(9600);
  servo.attach(servoPin);
  radio.begin();          
  radio.openReadingPipe(0, pipe); 
  radio.startListening();  
  
}

void loop() {

         radio.setRetries(5,5);
         radio.read(&joystick, sizeof(joystick));
         Serial.println(joystick);
         servo.write(joystick/6);
         
}

Any help would be much appreciated.

How is the radio wired to the Uno? How is the radio wired to the Mega? What is supplying power to the radios? The 3.3V regulators on the Uno and Mega may not supply enough current. See Robin2's tutorial (linked below). In a large number of cases it is lack of power to the radio modules that causes trouble.

I used Robin2's simple rf24 tutorial to get started with those radios.

The high power radios may not work if they are too close together. You may need to move them a few meters apart.

This is wrong. You cannot assign a string to an integer type...

 const uint16_t pipe = "00010";

Try...

 const char[] pipe = "00010";

Edit: sorry, correct C syntax...

 const char pipe[] = "00010";

groundFungus:
How is the radio wired to the Uno? How is the radio wired to the Mega? What is supplying power to the radios? The 3.3V regulators on the Uno and Mega may not supply enough current. See Robin2's tutorial (linked below). In a large number of cases it is lack of power to the radio modules that causes trouble.
The high power radios may not work if they are too close together. You may need to move them a few meters apart.

Radio wiring to Nano:
Nano pins - radio pins
D7 - CE
D8 - CSN
D11 - MOSI
D12 - MISO
D13 - SCK
3.3V - 3.3V and cap +
GND - GND and cap -

Radio wiring to Mega:
Mega pins - radio pins
7 - CE
8 - CSN
50 - MISO
51 - MOSI
52 - SCK
3.3V - 3.3V and cap +
GND - GND and cap -

I have a regulated 12V 5A power supply going to VIN and GND of both boards and then I used each board's 3.3V and GND to supply it's own radio. I have also added a 47uF cap between each 3.3V and GND pin, this seemed to help from TX on Nano staying off, to blinking.
Should I try using an off-board regulator to power each transceiver?
The radios are currently about 5cm apart

pcbbc:
This is wrong. You cannot assign a string to an integer type...

 const uint16_t pipe = "00010";

Try...

 const char[] pipe = "00010";

Thanks, just tried it, but I get an error saying
"expected unqualified-id before '[' token"

What am I missing?

rudi1441:
Thanks, just tried it, but I get an error saying
"expected unqualified-id before '[' token"

What am I missing?

Sorry, my bad. Too used to writing C# in the day job. Brackets in the wrong place...

 const char pipe[] = "00010";

Also the onboard 3.3v regulator may not have enough juice. I would definitely suggest a separate power supply.

pcbbc:
Sorry, my bad. Too used to writing C# in the day job. Brackets in the wrong place...

 const char pipe[] = "00010";

Also the onboard 3.3v regulator may not have enough juice. I would definitely suggest a separate power supply.

Now I get
"call of overloaded 'openReadingPipe(int, const char [6])' is ambiguous"

I don't have any 3.3v regulators. But (laugh if you must) how about I use a 12v regulated power supply to power a 12v regulator and 9v regulator in parallel, then use the 12v out as 3v and 9v out as gnd. Should this work as long as I don't use the same power supply to power anything else and cause ground loops?

rudi1441:
Now I get
"call of overloaded 'openReadingPipe(int, const char [6])' is ambiguous"

Which NRF library are you using? There are several and the way they expect their pipe names to be specified are different for each.
For none of them is the code in your original example correct. I’m not even sure how that compiled without errors or warnings.

Some will accept a char[], others expect an array of bytes. Sometimes it’s a uint64_t.
Use the correct one for your library as shown in the examples and/or header file for that library.
Or you can cast your char[] to the correct type.

const uint8_t pipe[] = { 0x30, 0x30, 0x30, 0x31, 0x30 };
const uint64_t pipe = 0x3031303030;

Edit regulator idea might work in theory, but what happens if one of the regulators is slow to ramp up? Now your differential voltage is 9 or 12v I think? I wouldn’t trust it.

Use regular 2x1.5v AA or AAA batteries if you have nothing else to hand.

pcbbc:
Which NRF library are you using? There are several and the way they expect their pipe names to be specified are different for each.
For none of them is the code in your original example correct. I’m not even sure how that compiled without errors or warnings.

Some will accept a char[], others expect an array of bytes. Sometimes it’s a uint64_t.
Use the correct one for your library as shown in the examples and/or header file for that library.
Or you can cast your char[] to the correct type.

const uint8_t pipe[] = { 0x30, 0x30, 0x30, 0x31, 0x30 };
const uint64_t pipe = 0x3031303030;

Edit regulator idea might work in theory, but what happens if one of the regulators is slow to ramp up? Now your differential voltage is 9 or 12v I think? I wouldn’t trust it.

Use regular 2x1.5v AA or AAA batteries if you have nothing else to hand.

I am not sure how to specify which library I am using... But I used the uint64_t you suggested.
I am going to try using the regulators with a 470uF cap between the 12v and 9v out. I don't have any good batteries lying around. I'll just have to get some 3.3V regulators on monday.

rudi1441:
I am not sure how to specify which library I am using... But I used the uint64_t you suggested.

URL to wherever you downloaded it from. Libraries installed via the IDE provide a helpful link to the library documentation in the library manager.
Whenever I write a sketch, I copy and pasted that URL into my sketch right above the #includes so I know were I originally got it from.

I am going to try using the regulators with a 470uF cap between the 12v and 9v out. I don't have any good batteries lying around. I'll just have to get some 3.3V regulators on monday.

i would suggest wait for the power supply to power up, then connect the module. Do not power up power supply with module attached.

Batteries suggested as usually possible to find a store and get some, even on Sunday. Whereas electronics shops with 3.3v regs, if your lucky enough to have any still, and they aren’t shut in lockdown, are harder to come by.

rudi1441:
I am not sure how to specify which library I am using... But I used the uint64_t you suggested.

Have you tried the examples in the link you were given in Reply #1?

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.

...R

Robin2:
Have you tried the examples in the link you were given in Reply #1?

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.

...R

Yes I am currently busy trying the code you posted. I just copied and pasted the whole thing and will see if I get better results than before.

Is the test program part of the code you posted? Or is it separate? I am now only at the second post in the thread.

rudi1441:
Is the test program part of the code you posted? Or is it separate? I am now only at the second post in the thread.

Everything is in the Thread.

...R

So I tried your code Robin2 and also I found batteries with some power left in them. The radios receive about 2.8V each from separate batteries. Still the message is not being received. The transmitter keeps writing "data sent message 0 TX failed" and the receiver is writing "data received" much quicker than one second intervals.

Edit: the receiver is only writing "SimpleRX Starting" and nothing else.

I then tried the check connection code. Here is what I got back.
Transmitter:

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

STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0x4141417852 0x4141417852
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0x4141417852
RX_PW_P0-6 = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x03
RF_CH = 0x4c
RF_SETUP = 0x07
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX


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

STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0x4141417852 0x4141417852
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0x4141417852
RX_PW_P0-6 = 0x20 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x03
RF_CH = 0x4c
RF_SETUP = 0x27
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 250KBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX

Receiver:

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

STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xe7e7e7e7e7 0x4141417852
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xe7e7e7e7e7
RX_PW_P0-6 = 0x00 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x4c
RF_SETUP = 0x07
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1MBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX


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

STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1 = 0xe7e7e7e7e7 0x4141417852
RX_ADDR_P2-5 = 0xc3 0xc4 0xc5 0xc6
TX_ADDR = 0xe7e7e7e7e7
RX_PW_P0-6 = 0x00 0x20 0x00 0x00 0x00 0x00
EN_AA = 0x3f
EN_RXADDR = 0x02
RF_CH = 0x4c
RF_SETUP = 0x27
CONFIG = 0x0e
DYNPD/FEATURE = 0x00 0x00
Data Rate = 250KBPS
Model = nRF24L01+
CRC Length = 16 bits
PA Power = PA_MAX

Could you tell me if this looks right? I have no idea whether this is good or bad news...

rudi1441:
So I tried your code Robin2 and also I found batteries with some power left in them.

Get some new batteries. I can never understand why people try so hard to make their experiments fail.

and the receiver is writing "data received" much quicker than one second intervals.

That almost certainly means a problem with the connections between the Arduino and its nRF24

...R

Robin2:
Get some new batteries. I can never understand why people try so hard to make their experiments fail.

That almost certainly means a problem with the connections between the Arduino and its nRF24

...R

I'll get 3.3v regulators tomorrow, so then there won't be any power issues.

The receiver wrote "data received" only before I connected it to the battery. As soon as I did, it stopped. I re-opened the serial monitor, then it only wrote "Simple RX Starting" and nothing after that.

Any advice on what else I could try? I'll retest as soon as I have the regulators.

rudi1441:
I'll get 3.3v regulators tomorrow, so then there won't be any power issues.

By all means get the regulators, but also get the new batteries. Batteries work really well and are completely uncomplicated provided they are new.

The receiver wrote "data received" only before I connected it to the battery. As soon as I did, it stopped. I re-opened the serial monitor, then it only wrote "Simple RX Starting" and nothing after that.

Any advice on what else I could try?

You will have to explain those symptoms in a lot more detail if I am to create a mental image of what happens.

If the receiver shows Simple RX Starting and then nothing it probably means it is not receiving anything.

...R

Robin2:
You will have to explain those symptoms in a lot more detail if I am to create a mental image of what happens.

If the receiver shows Simple RX Starting and then nothing it probably means it is not receiving anything.

That is my understanding of it as well. The transmitter keeps repeating "data sent message 0 TX failed" every second and never changes the 0 into 1. But I am not sure if it is even sending data or if the receiver has the problem. Any idea how I could narrow it down? If I need to give more information, please let me know what information you need. I want to solve the problem, but I don't have the knowledge yet

I finally got it working! ;D I removed both antennas and now the receiver is receiving the data correctly. Thanks for all the help!!

Now my next question, is how do I change the data being sent from text to two analogread values from two seperate pins and possibly more inputs like whether a digital pin is 1 or 0? I've tried messing with the code, but I can't seem to find the line I should edit to change what is being sent

Here is my code for sending and receiving joystick positions and a switch position. They use a stuct in each to hold the data to be transmitted and the received data. The beauty of a struct over an array is that a struct can hold a mix any data types. An array can only hold data of one data type. This code is tested an known to work.

Sending code.

// Example from Robin2's simple rf24 tutorial modified by c goulding
// to transmit 2 joystick axes and one switch state.

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

const byte JoyX = A0; // Joystick X pin connected to A0 on the UNO
const byte JoyY = A1; // Joystick Y pin connected to A1 on the UNO
const byte Switchpin = 3; // Digital pin connected to switch output

#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

struct Data  // define stuct
{
   int x;
   int y;
   boolean switchState;   
};

Data dataToSend; // create instance of struct

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


void setup()
{
   pinMode(Switchpin, INPUT_PULLUP);   
   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;
   dataToSend.x = (analogRead(JoyX));
   dataToSend.y = (analogRead(JoyY));
   dataToSend.switchState = (digitalRead(Switchpin));
   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("Joy X : ");
   Serial.println(dataToSend.x);
   Serial.print("Joy Y : ");
   Serial.println(dataToSend.y);
   Serial.print("Button : ");
   Serial.println(dataToSend.switchState);
   Serial.println("========");
   if (rslt)
   {
      Serial.println("  Acknowledge received");
   }
   else
   {
      Serial.println("  Tx failed");
   }

}
//================

Receiving code:

// SimpleRx - the slave or the receiver
// Modified by c. goulding (AKA groundfungus) to receive
// 2 joystick inputs and a switch state

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
# include <Servo.h> // add servo library

#define CE_PIN   9  // **** change for your setup
#define CSN_PIN 10  // **** change for your setup

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

RF24 radio(CE_PIN, CSN_PIN);
Servo servo;  // create instance of servo

struct Data //this must match dataToSend in the TX
{
   int x;
   int y;
   boolean switchState;   
}; 

Data dataReceived;

int xData = 0;
int yData = 0;
bool switchState = HIGH;

bool newData = false;
//===========

void setup()
{

   Serial.begin(9600);
   Serial.println("SimpleRx Starting");
   pinMode(ledPin, OUTPUT);
   servo.attach(3);  // attach servo
   radio.begin();
   radio.setDataRate( RF24_250KBPS );
   radio.openReadingPipe(1, thisSlaveAddress);
   radio.startListening();
}

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

void loop()
{
   getData();
   showData(); // will write to servo here
}

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

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

void showData()
{
   if (newData == true)
   {
      //Serial.print("Data received  ");
      //Serial.println(dataReceived);
      xData = dataReceived.x;
      yData = dataReceived.y;
      switchState = dataReceived.switchState;
      Serial.print("x pos  ");
      Serial.print(xData);
      Serial.print("   y pos  ");
      Serial.print(yData);
      Serial.print("  switch  ");
      Serial.println(switchState);
      digitalWrite(ledPin, switchState);
      newData = false;
   }
}