nrf24l01 receiver coding for multiple sensors

Hi,
I’m a beginner. I’ve a code for transmitting multiple sensors data using nrf24l01 and also have a code which can receive just one sensor’s data if transmitter sends just one data. How to edit receiving codes so that it can receive multiple sensors data simultaneously.

Transmitter

#include <SPI.h>   
#include "RF24.h"
#define variableresistor 2
#define variableresistor2 3

RF24 myRadio (7, 8); 

byte addresses[][6] = {"1Node"}; 
int dataTransmitted;
int dataTransmitted2;
  
  

void setup()  
{
  
  Serial.begin(115200);
  delay(1000);
  Serial.println(F("RF24/Simple Transmit data Test"));
 
  dataTransmitted = analogRead(variableresistor);  
  dataTransmitted2 = analogRead(variableresistor2);  
  myRadio.begin();

  myRadio.setChannel(108);  
  
 myRadio.setPALevel(RF24_PA_MIN); 

  //myRadio.setPALevel(RF24_PA_MAX); 

  myRadio.openWritingPipe( addresses[0]);
   myRadio.openWritingPipe( addresses[1]); 
  delay(1000);
}


void loop()   
{
  myRadio.write( &dataTransmitted, sizeof(dataTransmitted) ); 
   myRadio.write( &dataTransmitted2, sizeof(dataTransmitted2) ); 
  Serial.print(F("Data Transmitted = "));
  Serial.print(dataTransmitted);
  Serial.println(F(" "));
  Serial.print(F("Data Transmitted 2 = "));
  Serial.print(dataTransmitted2);
  Serial.println(F(" "));
  
  delay(500);

}

Here’s the receiver’s code. Can’t receive data. PLease help me editing here.

  1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 7
   4 - CSN to Arduino pin 8
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED

 

#include <SPI.h>   
#include "RF24.h"  
RF24 myRadio (7, 8); 
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int dataReceived; 
void setup()  
{
  
  Serial.begin(115200);
  delay(1000);
  Serial.println(F("RF24/Simple Receive data Test"));
  

  myRadio.begin();  
  myRadio.setChannel(108); 
  
  myRadio.setPALevel(RF24_PA_MIN);
  //  myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power

  myRadio.openReadingPipe(1, addresses[0]); 
  myRadio.startListening();

}


void loop()  
{

  if ( myRadio.available()) 
  {
    while (myRadio.available()) 
    {
      myRadio.read( &dataReceived, sizeof(dataReceived) ); 
    }
   
    Serial.print("Data received = ");
    Serial.println(dataReceived);
  } 

}
byte addresses[][6] = {"1Node"};

  myRadio.openWritingPipe( addresses[0]);
   myRadio.openWritingPipe( addresses[1]);

Why do you want to send to a bogus address, overwriting the previous good one?

  myRadio.write( &dataTransmitted, sizeof(dataTransmitted) );
   myRadio.write( &dataTransmitted2, sizeof(dataTransmitted2) );

Why do you want to send two packets for the two values?

How will you know which packet contains which value?

    while (myRadio.available())
    {
      myRadio.read( &dataReceived, sizeof(dataReceived) );
    }

Why do you read all available packets overwriting into one buffer and print only the last one?


If you want to send two int values you could try (untested)

TX

#include <SPI.h>
#include "RF24.h"
#define variableresistor 2
#define variableresistor2 3

RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"};

struct Pack {
  int data1;
  int data2;
} packet;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Transmit data Test"));

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openWritingPipe(addresses[0]);
}

void loop() {
  packet.data1 = analogRead(variableresistor);
  packet.data2 = analogRead(variableresistor2);

  if (myRadio.write(&packet, sizeof(packet))) {
    Serial.print(F("Data Transmitted = "));
    Serial.print(packet.data1);
    Serial.println();
    Serial.print(F("Data Transmitted 2 = "));
    Serial.print(packet.data2);
    Serial.println();
  } else {
    Serial.print(F("Send failed."));
  }
  delay(500);
}

RX

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

RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"};

struct Pack {
  int data1;
  int data2;
} packet;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Receive data Test"));

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
}

void loop() {
  if ( myRadio.available())   {
    myRadio.read( &packet, sizeof(packet) );
    Serial.print("Data received = ");
    Serial.println(packet.data1);
    Serial.print("Data2 received = ");
    Serial.println(packet.data2);
  }
}

FranticKid: have a code which can receive just one sensor's data if transmitter sends just one data. How to edit receiving codes so that it can receive multiple sensors data simultaneously.

Do you mean that you want to receive data from a number of different nRF24s each connected to a different sensor?

OR do you mean that a single nRF24 needs to send several values in every message?

If you need to communicate with several nRF24s I suggest you treat the "receiver" as the master and have it poll each of the "slaves" in turn. Have a look at this Simple nRF24L01+ Tutorial. I have recently added an example of a master communicating with multiple slaves.

...R

Whandall bro,
thanks for your code, it helped me a lot. with the “if…else” condition serial monitor just showed “SEND FAILED”. After removing “if…else” conditional loop i was able to transmit data correctly. here’s the edited version of the code
TX

#include <SPI.h>
#include "RF24.h"
#define variableresistor 2
#define variableresistor2 3

RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"};

struct Pack {
  int data1;
  int data2;
} packet;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Transmit data Test"));

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openWritingPipe(addresses[0]);
}

void loop() {
  packet.data1 = analogRead(variableresistor);
  packet.data2 = analogRead(variableresistor2);

  myRadio.write(&packet, sizeof(packet))
    Serial.print(F("Data Transmitted = "));
    Serial.print(packet.data1);
    Serial.println();
    Serial.print(F("Data Transmitted 2 = "));
    Serial.print(packet.data2);
    Serial.println();
  
  delay(500);
}

but i was unable to receive data using your RX code on the receiver’s end. Can you please help me out ? can you please recheck your RX code? thanks in advance bro

FranticKid: with the "if...else" condition serial monitor just showed "SEND FAILED".

So the sender does not receive an acknowledge from the receiving node, which means it failed.

FranticKid: After removing "if..else" conditional loop i was able to transmit data correctly.

Nope, you just removed the error message, not the error.

FranticKid: but i was unable to receive data using your RX code on the receiver's end.

Not receiving a packet is a very good reason not to send an acknowledgement.


My guess: you don't have a good 3.3V supply for the tx-NRF and no capacitor across its power pins.

See 'NOTE! Power Problems' in http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

A second (less probable) reason could be a faulty module, I have one that works basically, but is unable to transmit due to a missing part.

Thanks robin2 for your tutorial. And i meant a single nRF24 needs to send several values in every message.

FranticKid: Thanks robin2 for your tutorial. And i meant a single nRF24 needs to send several values in every message.

Some of the examples in my tutorial do that.

...R

TX

#include <SPI.h>
#include "RF24.h"
#define led1 4
#define led2 5

RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"};
 int sum;

struct Pack {
  int data1;
  int data2;
} packet;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Receive data Test"));

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
}

void loop() {
 
  if ( myRadio.available())   {
    myRadio.read( &packet, sizeof(packet) );
    long int sum=packet.data1+packet.data2;
    Serial.print("Data received = ");
    Serial.println(packet.data1);
    Serial.print("Data2 received = ");
    Serial.println(packet.data2);
    
    Serial.print("sum = ");
    Serial.println(sum);
    
     if ( sum<=612) digitalWrite (led1, HIGH);
    else digitalWrite (led1, LOW);

      if (sum>612) digitalWrite (led2, HIGH);
        else digitalWrite (led2, LOW);
  }
}

RX

#include <SPI.h>
#include "RF24.h"
#define variableresistor 2
#define variableresistor2 3

RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"};

struct Pack {
  int data1;
  int data2;
} packet;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Transmit data Test"));

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openWritingPipe(addresses[0]);
}

void loop() {
  packet.data1 = analogRead(variableresistor);
  packet.data2 = analogRead(variableresistor2);

  if (myRadio.write(&packet, sizeof(packet))) {
    Serial.print(F("Data Transmitted = "));
    Serial.print(packet.data1);
    Serial.println();
    Serial.print(F("Data Transmitted 2 = "));
    Serial.print(packet.data2);
    Serial.println();
  } else {
    Serial.print(F("Send failed."));
  }
  delay(500);
}

after receiving the data LED isnt getting sufficient power to be visible in daylight but i can see them in the dark. how to solve the power problem so that it can be visible in daylight??

Add the following to setup:

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

:) tnx bro.... lol how could i miss that!

TX

#include <SPI.h>
#include "RF24.h"
#define variableresistor 2
#define variableresistor2 3

RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"};

struct Pack {
  int data1;
  int data2;
} packet;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Transmit data Test"));

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openWritingPipe(addresses[0]);
}

void loop() {
  packet.data1 = analogRead(variableresistor);
  packet.data2 = analogRead(variableresistor2);

  myRadio.write(&packet, sizeof(packet))
    Serial.print(F("Data Transmitted = "));
    Serial.print(packet.data1);
    Serial.println();
    Serial.print(F("Data Transmitted 2 = "));
    Serial.print(packet.data2);
    Serial.println();
  
  delay(500);
}

RX

   #include "RF24.h"
#define led1 4
#define led2 5

RF24 myRadio (7, 8);
byte addresses[][6] = {"1Node"};
 

struct Pack {
  int data1;
  int data2;
} packet;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/Simple Receive data Test"));

  myRadio.begin();
  myRadio.setChannel(108);
  myRadio.setPALevel(RF24_PA_MIN);
  myRadio.openReadingPipe(1, addresses[0]);
  myRadio.startListening();
}

void loop() {
 
  if ( myRadio.available())   {
    myRadio.read( &packet, sizeof(packet) );
    long int sum=packet.data1+packet.data2;
    Serial.print("Data received = ");
    Serial.println(packet.data1);
    Serial.print("Data2 received = ");
    Serial.println(packet.data2);
    
    Serial.print("sum = ");
    Serial.println(sum);
    
     if ( sum<=612) digitalWrite (led1, HIGH);
    else digitalWrite (led1, LOW);

      if (sum>612) digitalWrite (led2, HIGH);
        else digitalWrite (led2, LOW);
  }
}

at the receiver’s end how can i know which one is the value of “variableresistor” and which one is “variableresistor2”??? At transmission code packet.data1=analogRead (variableresistor), does it mean at the receiver’s end packet.data1 will be data of “variableresistor”?

FranticKid: at the receiver's end how can i know which one is the value of "variableresistor" and which one is "variableresistor2"???

You fill the structure, then transfer the data and create a copy in the receiver.

FranticKid: At transmission code packet.data1=analogRead (variableresistor), does it mean at the receiver's end packet.data1 will be data of "variableresistor"?

That's the plan.