nRF24L01 not sending Array

Hello everyone,
I am new to this forum so if anything is wrong, please let me know.

The project:
Currently I am creating a remote controlled car. I am using 2 potmeters (one for steering and one for power). For this project I am using two arduino Nano's with one nRF24L01 each. Using these nRF24L01 the two Arduino's are communicating.

The Issue:
On Arduino Nano 1 (Tx) I have connected the two potmeters. I am trying to send the value of those two potmeters to the other Arduino Nano (Rx). I am trying to use an Array to send both values over one channel. With both codes uploaded (see codes bellow) the value of Array[0] (pot_power_pin) is send and received, I see it on the Serial Monitor (Rx). I also see a value of '0' after the value of Array[0] (Rx). However when I turn the potmeter of Array[1] (pot_steer_pin), the value is not changing on the receiver side (Rx). On the Serial Monitor of Tx the value is changing.

It seems to me that the Tx has the correct values. The problem could be one of the following:

  • Only Array[0] is send and Array[1] is not.
  • Only Array[0] is received and Array[1] is not.

Code Tx

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

int pin_RF_CE = 9;        //RF-CE pin
int pin_RF_CSN = 10;      //RF-CSN pin
RF24 radio(pin_RF_CE, pin_RF_CSN);
const byte address[6] = "00001";

int pot_power_pin = A0;   //Potmeter gas pin
int pot_power_min = 0;    //Minimum gas
int pot_power_max = 255; //Maximum gas

int pot_steer_pin = A1;   //Potmeter stuur pin
int pot_steer_min = 0;    //Minimum stuur
int pot_steer_max = 255; //Maximum stuur

const int Arraynum = 2;
int Array[Arraynum];

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);      //RF PALevel
  radio.stopListening();              //RF Transmitter

  pinMode (pot_power_pin, INPUT);
  pinMode (pot_steer_pin, INPUT);

  Serial.begin(9600);                   //Teste, verwijderen als code klopt
  delay(2000);
}

void loop() {
  Array[0] = analogRead(pot_power_pin);
  Array[0] = map(Array[0], 0, 1023, pot_power_min, pot_power_max);
  
  Array[1] = analogRead(pot_steer_pin);
  Array[1] = map(Array[1], 0, 1023, pot_steer_min, pot_steer_max);

  Serial.print(Array[0]);          //Testen, verwijderen als code klopt
  Serial.println(Array[1]);        //Testen, verwijderen als code klopt

  radio.write(&Array, sizeof(Array));
}

Code Rx

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

RF24 radio(9,10); // CE, CSN
const byte address[6] = "00001";

const int Arraynum = 2;
int Array[Arraynum];

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    byte Array[2];
    radio.read(&Array, sizeof(Array));
    Serial.print(Array[0]);
    Serial.println(Array[1]);
  }
}

My Question:
I have put many hours in trying to figure out why not both values are send. Can someone please help me to find the error in my code.

As I said I am new to this forum and still learning to work with Arduino so any advice is very much appreciated.

Thanks to you all for helping!

//This will send 2 bytes containing the pointer address of the array
radio.write(&Array, sizeof(Array));

//This is how to do it
radio.write(Array, Arraynum * sizeof(int));

You could also declare a const with the size of the array instead of recalculating it for each iteration of loop.

EDIT: You are making the same mistake in the receiver code.

1 Like

Your arrays hold different data types, maybe this has something to do with it?
On the TX:

const int Arraynum = 2;
int Array[Arraynum];

Meanwhile, on the RX:

byte Array[2];

int variables are of size 2 on Arduino, while byte variables are of size 1. You might be sending more data (4 bytes) than the RX can store on the array (2 bytes), therefore the second value is being "lost".

1 Like

Thanks for pointing this out! This was indeed the problem, that line of code was still there from modifying an example code. When I deleted it, the problem was solved.

I will post the new codes in case anyone has the same problem.

Tx code (NEW)

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

int pin_RF_CE = 9;        //RF-CE pin
int pin_RF_CSN = 10;      //RF-CSN pin
RF24 radio(pin_RF_CE, pin_RF_CSN);
const byte address[6] = "00001";

int pot_power_pin = A0;   //Potmeter gas pin
int pot_steer_pin = A1;   //Potmeter stuur pin

const int Arraynum = 2;
int Array[Arraynum];

void setup() {
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);      //RF PALevel
  radio.stopListening();              //RF Transmitter

  pinMode (pot_power_pin, INPUT);
  pinMode (pot_steer_pin, INPUT);

  Serial.begin(9600);                   //Testen, verwijderen als code klopt
  delay(2000);
}

void loop() {
  Array[0] = analogRead(pot_power_pin); 
  Array[1] = analogRead(pot_steer_pin);

  Serial.print(Array[0]);          //Testen, verwijderen als code klopt
  Serial.println(Array[1]);        //Testen, verwijderen als code klopt

  radio.write(Array, Arraynum * sizeof(int));
}

Rx code (NEW)

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

RF24 radio(9,10); // CE, CSN
const byte address[6] = "00001";

const int Arraynum = 2;
int Array[Arraynum];

//Array[0] = Value pot_power_pin
//Array[1] = Value pot_steer_pin

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(Array, Arraynum * sizeof(int));
    Serial.print(Array[0]);
    Serial.println(Array[1]);
  }
}

Thanks for showing me this, it is now fixed in the new code.

You new code cannot work since you are not sending the content of the array as pointed out in my previous post.

1 Like

Like @Danois90 has written Array is already a pointer, you don't have to put the & before!
Also the sizeof instruction is wrong.

1 Like

I think I misunderstood what you was pointing out to me. Both codes work in my case but yours is deffinately more reliable. I have fixed it now and your line is in the new code.

Thanks for the help!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.