arduino nrf24l01

Hi,

I work with a transmitter and a receiver. I have two sketches and that works fine, How can I add more arduino Nano's (receivers) ? I thought I have to work with addresses, but don't know how...

transmitter

/*
   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


/*-----( Import needed libraries )-----*/
#include <SPI.h>   // Comes with Arduino IDE
#include "RF24.h"  // Download and Install (See above)
/*-----( Declare Constants and Pin Numbers )-----*/
//None yet
/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
RF24 myRadio (7, 8); // "myRadio" is the identifier you will use in following methods
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int dataTransmitted;  // Data that will be Transmitted from the transmitter
boolean STAT = true;


//test
int contact = 0;

void setup()   /****** SETUP: RUNS ONCE ******/
{

  pinMode(2,OUTPUT);

  
  // Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
  Serial.begin(115200);
  delay(1000);
  Serial.println(F("RF24/Simple Transmit data Test"));
  Serial.println(F("Questions: terry@yourduino.com"));
  dataTransmitted = 100; // Arbitrary known data to transmit. Change it to test...
  myRadio.begin();  // Start up the physical nRF24L01 Radio
  myRadio.setChannel(108);  // Above most Wifi Channels
  // Set the PA Level low to prevent power supply related issues since this is a
  // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  //myRadio.setPALevel(RF24_PA_MIN);
    myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power

  myRadio.openWritingPipe( addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  delay(1000);

}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{

contact=digitalRead(2);
 Serial.println(contact);

      dataTransmitted = contact;
  
  myRadio.write( &dataTransmitted, sizeof(dataTransmitted) ); //  Transmit the data

  Serial.print(F("Data Transmitted = "));
  Serial.print(dataTransmitted);
  Serial.println(F(" No Acknowledge expected"));
  dataTransmitted = dataTransmitted + 1;  // Send different data next time
  delay(500);
dataTransmitted = 0;


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/


//*********( THE END )***********

Receiver:

/* 
   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

/*-----( Import needed libraries )-----*/
#include <SPI.h>   // Comes with Arduino IDE
#include "RF24.h"  // Download and Install (See above)
/*-----( Declare Constants and Pin Numbers )-----*/
//None yet
/*-----( Declare objects )-----*/
// (Create an instance of a radio, specifying the CE and CS pins. )
RF24 myRadio (7, 8); // "myRadio" is the identifier you will use in following methods
/*-----( Declare Variables )-----*/
byte addresses[][6] = {"1Node"}; // Create address for 1 pipe.
int dataReceived;  // Data that will be received from the transmitter

void setup()   /****** SETUP: RUNS ONCE ******/
{
  // Use the serial Monitor (Symbol on far right). Set speed to 115200 (Bottom Right)
  Serial.begin(115200);
  delay(1000);

  myRadio.begin();  // Start up the physical nRF24L01 Radio
  myRadio.setChannel(108);  // Above most Wifi Channels
  // Set the PA Level low to prevent power supply related issues since this is a
  // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  //myRadio.setPALevel(RF24_PA_MIN);
    myRadio.setPALevel(RF24_PA_MAX);  // Uncomment for more power

  myRadio.openReadingPipe(1, addresses[0]); // Use the first entry in array 'addresses' (Only 1 right now)
  myRadio.startListening();

  // initialize digital pin LED_BUILTIN as an output.
  pinMode(4, OUTPUT);
  
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{

  if ( myRadio.available()) // Check for incoming data from transmitter
  {
    while (myRadio.available())  // While there is data ready
    {
      myRadio.read( &dataReceived, sizeof(dataReceived) ); // Get the data payload (You must have defined that already!)
    }
    // DO something with the data, like print it
    if (dataReceived == 1) {
    
    digitalWrite(4, HIGH);
    Serial.print("AAN: ");
    }
    if (dataReceived == 0) {  
    
    digitalWrite(4, LOW);
    Serial.print("UIT: ");
    }
    
    Serial.print("Data received = ");
    Serial.println(dataReceived);
  } //END Radio available

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//None yet
//*********( THE END )***********

You need a separate address for each "slave" and the "master" needs to open the writing pipe for each address immediately before it sends data to the different slaves.

At the moment you are opening the writing pipe in the master in setup(). You need to move that into loop() and then iterate through the array of addresses.

...R
Simple nRF24L01+ Tutorial

Can you show me with my code

I followed your link but I don't understand it completely

Something like this

byte numSlaves = 3;
byte addresses[][6] = {"1Node", "2Node", "3Node"};

and this

void loop() {
    Serial.println(contact);
    dataTransmitted = contact;
    for (byte n = 0; n < numSlaves; n++) {
        myRadio.openWritingPipe( addresses[n]);
        myRadio.write( &dataTransmitted, sizeof(dataTransmitted) );
        Serial.print(F("Data Transmitted = "));
        Serial.print(dataTransmitted);
        Serial.println(F(" No Acknowledge expected"));
        dataTransmitted = dataTransmitted + 1;  // Send different data to next slave
    }
    delay(500);
}

...R

What is the maximum of addresses I can add??

byte numSlaves = 3;
byte addresses[][6] = {"1Node", "2Node", "3Node"};

How many addresses do you need? With 5 bytes you have 2555 options - but don't worry, the Arduino does not have that much memory :slight_smile:

A more practical question is how long it takes to send a message to a slave and get an acknowledgement and then how many messages you need to send every second. I did some tests on the round-trip time and I think it was less than 10 millisecs which would imply about 100 messages per second. That could be 1 message per second to each of 100 slaves or 10 messages per second to 10 slaves. That actually makes me think the round trip is much faster - I think I am planning my model rail system on the basis of 1o messages per second to 20 slaves. Obviously if you only communicate with each slave at (say) 30 second intervals you could have many more than 100 - but you will probably run out of Arduino memory.

...R