Arduino Nrf24l01 Problems

Hi ,

I'm a 4th grade student at the university. I can't solve the code part of my graduation project. I have 4 sensors. I get analog data from them. For example: 1.sensor:1023 2.sensor:1024 3.sensor:1023 4. sensor:1021 . I need to transfer this analog data from nrf24l01 to other nrf24l01. But I can only transfer 1 data. I want to transfer 4 of them at the same time. I've connected a capacitor to both the receiver and the transmitter.
Please help me in the code part ?

So post the code (both ends) that is successfully transferring one piece of data and we can start from there.

BTW if you really got 1024 from one sensor then something has really gone wrong!

Steve

slipstick:
So post the code (both ends) that is successfully transferring one piece of data and we can start from there.

BTW if you really got 1024 from one sensor then something has really gone wrong!

Steve

My sensor is a soil moisture sensor. If the soil is dry 1024 is not worth between 200-500.

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

...R

Robin2:
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

...R

Thank you so much for guide. I used a 10pf capacitor by mistake. I will replace the capacitor..I write here if there is a problem again. Thanks again Robin2

Tx Code:

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

RF24 radio(9, 10);

const byte rxAddr[6] = “00001”;
int sensor1;

void setup()
{
radio.begin();
radio.setRetries(15, 15);
radio.openWritingPipe(rxAddr);

radio.stopListening();
}

void loop()
{
sensor1= analogRead(A0);
static int text = sensor1;
radio.write(&text, sizeof(text));

delay(1000);
}

Rx Code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(9, 10);

const byte rxAddr[6] = “00001”;
int sensor1;

void setup()
{
while (!Serial);
Serial.begin(9600);

radio.begin();
radio.openReadingPipe(0, rxAddr);

radio.startListening();
}

void loop()
{
if (radio.available())
{
static int text = sensor1;
radio.read(&text, sizeof(text));

Serial.println(text);
}
}

Using this code, I need to read and print values ​​from A1, A2 and A3 analog pins, how can I write a code?
(Note:I took care of wireless problems in nrf24l01. Running smoothly.

Have you tried the tutorial code referenced in Reply #3?

Yes, I tried.I can read one sensor data. But I don't know what code to put the data on the 4 sensors. I'm just writing the part.My code for 1 sensor is:

Tx Code:
int sensor1;

sensor1= analogRead(A0);
static int text = sensor1;
radio.write(&text, sizeof(text));

delay(1000);

RX Code:

int sensor1;

if (radio.available())
{
static int text = sensor1;
radio.read(&text, sizeof(text));

Serial.println(text);

A1-A2-A3-A4 pin .I'm getting data from these pins. I want to see 4 data on a serial monitor at the receiving Arduino. Please help me coding.

Create an array to hold the values from the ADC and send the array. Have an identical array on the receiving side and save the data into it.

That’s all my first example does - it is using an array of CHARs. You can change that to be an array of INTs

…R

So how do I save it into the array? Is it :

char dataToSend[10]

int sensor1,sensor2,sensor3,sensor4;

sensor1=analogRead(A0);
sensor2=analogRead(A1);
sensor3 =analogRead(A2);
sensor4=analogRead(A3);

dataToSend[0] = sensor1;
dataToSend[1] = sensor2;
dataToSend[2] = sensor3;
dataToSend[3] = sensor4;

bool rslt;
rslt = radio.write( &dataToSend, sizeof(dataToSend) );

Serial.print("Data Sent ");
Serial.print(dataToSend);

I'm using the one-way transmission code on your page as an example code.

ArduinoPlus:
So how do I save it into the array? Is it :

You are nearly right.

Change this

char dataToSend[10]

to

int dataToSend[4]

because analogRead returns a value between 0 and 1023 which won't fit into a CHAR and you only need space for 4 values.

Make the same change on the receiver.

...R

Tx Code;

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



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


RF24 radio(7,8); // Create a Radio

char dataToSend[4];
int sensor1,sensor2,sensor3,sensor4;


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() {
  
  sensor1=analogRead(A0);
  sensor2=analogRead(A1);
  sensor3=analogRead(A2);
  sensor4=analogRead(A3);
  dataToSend[0] = sensor1;
  dataToSend[1] = sensor2;
  dataToSend[2] = sensor3;
  dataToSend[3] = sensor4;
    bool rslt;   
    rslt = radio.write( &dataToSend, sizeof(dataToSend) );
        

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

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


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

RF24 radio(7,8);

char dataReceived[4]; // 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;
    }
}

That’s how I get Rx. Where did I make a mistake?

Is this what I recommended?

char dataToSend[4];

And this won't work with an array of INTs

Serial.print(dataToSend);

You need to print each element separately

I reckon you need to do some studying about C/C++ data types

...R

Excuse me for At first I wrote char instead of int. I made arrangements according to your suggestions.

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

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


RF24 radio(7,8); 

int dataToSend[4];
int sensor1,sensor2,sensor3,sensor4;


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() {
  
  sensor1=analogRead(A0);
  sensor2=analogRead(A1);
  sensor3=analogRead(A2);
  sensor4=analogRead(A3);
  dataToSend[0] = sensor1;
  dataToSend[1] = sensor2;
  dataToSend[2] = sensor3;
  dataToSend[3] = sensor4;
    bool rslt;   
    rslt = radio.write( &dataToSend[0], sizeof(dataToSend[0]) );
    rslt = radio.write( &dataToSend[1], sizeof(dataToSend[1]) );
    rslt = radio.write( &dataToSend[2], sizeof(dataToSend[2]) );
    rslt = radio.write( &dataToSend[3], sizeof(dataToSend[3]) );

    delay(500);

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


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

RF24 radio(7,8);
int dataReceived[4]; 



void setup() {

    Serial.begin(9600);

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



void loop() {
  if ( radio.available() ) {
        radio.read( &dataReceived[0], sizeof(dataReceived[0]) );
        radio.read( &dataReceived[1], sizeof(dataReceived[1]) );
        radio.read( &dataReceived[2], sizeof(dataReceived[2]) );
        radio.read( &dataReceived[3], sizeof(dataReceived[3]) );
      
        Serial.print(dataReceived[0]);
        Serial.print("*");
        Serial.println(dataReceived[1]);
        Serial.print("*");
        Serial.print(dataReceived[2]);
        Serial.print("*");
        Serial.print(dataReceived[3]);
        Serial.print("*");
        
    }
}

Rx output as follows: He’s just reading a value. And he reads the value twice.

This is wrong - a retrograde step

   rslt = radio.write( &dataToSend[0], sizeof(dataToSend[0]) );
    rslt = radio.write( &dataToSend[1], sizeof(dataToSend[1]) );
    rslt = radio.write( &dataToSend[2], sizeof(dataToSend[2]) );
    rslt = radio.write( &dataToSend[3], sizeof(dataToSend[3]) );

You had it correct in the code in Reply #11

rslt = radio.write( &dataToSend, sizeof(dataToSend) );

...R

wow . Values are getting awesome. Thank you very much Robin2. We can close topic.