Trying to understand and get RF24L01 to work with 1 receiver and 2 transmitters

Hi

I am trying to get two arduino units to measure the light level in two locations and then transmit the values back wirelessly to a receiver arduino which will then read the values and determine what LED lights should be lit.

I have managed to get one transmitter to work with the receiver but not sure how to get another one in there. Im struggling what to do next. I have pasted the code below and it may be incomplete in some areas. Im just wondering how you are able to have two transmitters transmitting to one receiver. Also I do not need to send data back, one way communication is fine as I have code on the receiver if no signal is being received red led lights will flash, and when there is data green led lights will be on.

Receiver code

/* YourDuinoStarter Example: nRF24L01 Receive Joystick values

 - WHAT IT DOES: Receives data from another transceiver with
   2 Analog values from a Joystick or 2 Potentiometers
   Displays received values on Serial Monitor
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe[2] = {0xE8E8F0F0E1LL,0xE8E8F0F0E2LL}; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
//int joystick[2];  // 2 element array holding Joystick readings
  int lightLevel[2];
  int lightLevel2[2];
  int redLed = 2;
  int greenLed = 4;
  int redLed2 = 7;
  int greenLed2 = 8;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe[0]);
  radio.openReadingPipe(2,pipe[1]);
  radio.startListening();
  pinMode(redLed, OUTPUT);
  pinMode(greenLed,OUTPUT);
  pinMode(redLed2, OUTPUT);
  pinMode(greenLed2,OUTPUT);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    //while (!done)
    {
      // Fetch the data payload
     radio.read( lightLevel, sizeof(lightLevel) );
      Serial.print("Light level ");
      Serial.println(lightLevel[0]);
      //Serial.print(" Y = ");      
     // Serial.println(joystick[1]);
     if ( lightLevel[0] < 500)
     {
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);
      delay(500);
     }
     if (lightLevel[0] > 501);
     {
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed,LOW);
      delay(500);
     }
     
     

     

     

     
    }
  }
  else
  {    
      Serial.println("No radio available");
      digitalWrite(greenLed,LOW);
      digitalWrite(redLed,HIGH);
      digitalWrite(greenLed2,LOW);
      digitalWrite(redLed2,HIGH);
      delay(500);
      digitalWrite(redLed,LOW);
      digitalWrite(redLed2,LOW);
      delay(500);
      
     
  }

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

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

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

Transmitter code for one ( the other one will be exactly the same except one will have lightValue and the other lightValue2.

/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
 - WHAT IT DOES: Reads Analog values on A0, A1 and transmits
   them over a nRF24L01 Radio Link to another transceiver.
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   - 
   Analog Joystick or two 10K potentiometers:
   GND to Arduino GND
   VCC to Arduino +5V
   X Pot to Arduino A0
   Y Pot to Arduino A1
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10
#define LIGHTLEVEL A0
//#define JOYSTICK_Y A1

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = {0xE8E8F0F0E1LL}; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
//int joystick[2];  // 2 element array holding Joystick readings
///int lightLevel[2];

int light1 = A0;
int light2 = A1;
int light1val = 1;
int light2val = 2;

int lightLevel[2];






 //const int lightLevel = A0;

void setup()   /****** SETUP: RUNS ONCE ******/
{
  
  Serial.begin(9600);
  Serial.begin(38400);
  radio.begin();
  radio.openWritingPipe(pipe);
  
  
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  
  //lightLevel = analogRead(A0);
  //Serial.print("Light level: ");
  //Serial.println(LIGHTLEVEL,DEC);
  //delay(500);
//  joystick[0] = analogRead(JOYSTICK_X);
 // joystick[1] = analogRead(JOYSTICK_Y);
/// lightLevel[1]=analogRead(LIGHTLEVEL);

  light1val = analogRead(light1);
  lightLevel[0] = 1023 - light1val;
  Serial.print("Light reading 1: ");
  Serial.println(lightLevel[0]);
  
  light2val = analogRead(light2);
  lightLevel[1] = light2val;
  Serial.print("Light reading 2: ");
  Serial.println(lightLevel[1]);




  delay(500);
  
  

  
//  radio.write( joystick, sizeof(joystick) );
  radio.write(lightLevel, sizeof(lightLevel));

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

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

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

I reckon it would be easier to swap things round and have the "receiving" Arduino take the role of Master and treat the others as slaves. Then the master would ask each slave in turn for its data.

The pair of programs in this Demo work like that and it would be easy to make the master call several slaves.

I suggest you use the TMRh20 version of the RF24 library - it solves some problems from the ManiacBug version

...R

It should be no problem.
The transmitters will each at intervals (say every X seconds) and transmit two items of information

  1. an identifier so you know which transmitter sent the data
  2. the information it self (the light value).

Lets say you have an array:

int lightSensor[2] ;

in transmitter 1, you set lightSensor[0] to 1 // identifies the sender as transmitter 1
in transmitter 2, you set lightSensor[0] to 2 // identifies the sender as transmitter 2

in both transmitters, you set lightSensor[1] to the value obtained from the light sensor and send the array lightSensor[] to the receiver.

The receiver part simply runs continuously, processing the array lightSensor[] as it receives it.

6v6gt:
It should be no problem.
The transmitters will each at intervals (say every X seconds) and transmit two items of information

  1. an identifier so you know which transmitter sent the data
  2. the information it self (the light value).

Lets say you have an array:

int lightSensor[2] ;

in transmitter 1, you set lightSensor[0] to 1 // identifies the sender as transmitter 1
in transmitter 2, you set lightSensor[0] to 2 // identifies the sender as transmitter 2

in both transmitters, you set lightSensor[1] to the value obtained from the light sensor and send the array lightSensor[] to the receiver.

The receiver part simply runs continuously, processing the array lightSensor[] as it receives it.

Ok thanks for your advice, but how do I 'identify' the transmitters?

I could have lightValue and lightValue2 in my receiver code to store the data but how do I send the lightValue from arduino 1 and lightValue2 from arduino 2? What code do I need to include or modify? This is where im stuck at the moment.

The main differences are in the use of the array:
int lightLevel[2]
to pass information from the transmitters to the receiver.
Once you get Serial.print on the receiver to display readings from both transmitters, then attempt to get the LEDs working.

Transmitter code
modify 1 line in setup() to distinguish between transmitter 1 and transmitter 2

/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
 - WHAT IT DOES: Reads Analog values on A0, A1 and transmits
   them over a nRF24L01 Radio Link to another transceiver.
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   -
   Analog Joystick or two 10K potentiometers:
   GND to Arduino GND
   VCC to Arduino +5V
   X Pot to Arduino A0
   Y Pot to Arduino A1
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10
#define LIGHTLEVEL A0
//#define JOYSTICK_Y A1

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = {0xE8E8F0F0E1LL}; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
//int joystick[2];  // 2 element array holding Joystick readings

int lightLevel[2] ;   // array  lightLevel[0] = sensor identifier, lightLevel[1] = sensor value

int lightval  ;   // now lightval

// //int lightLevel[2];

 //const int lightLevel = A0;

void setup()   /****** SETUP: RUNS ONCE ******/
{
// change the following statement if necessary:
  lightLevel[0]  =  1   // for transmit 1, lightLevel[0]  =  1  ;  for transmit 2, lightLevel[0]  =  2  ; 

  Serial.begin(9600);
  Serial.begin(38400);
  radio.begin();
  radio.openWritingPipe(pipe);
 
 
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  //lightLevel = analogRead(A0);
  //Serial.print("Light level: ");
  //Serial.println(LIGHTLEVEL,DEC);
  //delay(500);
//  joystick[0] = analogRead(JOYSTICK_X);
 // joystick[1] = analogRead(JOYSTICK_Y);
/// lightLevel[1]=analogRead(LIGHTLEVEL);

  lightval = analogRead(LIGHTLEVEL);  // light sensor is connected to pin A0 (LIGHTLEVEL)
  lightLevel[1] = 1023 - lightval;   // lightLevel[1] now holds modified light value
  Serial.print("Light reading 1: ");
  Serial.println(lightLevel[1]);


  delay(500);
 
//  radio.write( joystick, sizeof(joystick) );
  radio.write(lightLevel, sizeof(lightLevel));

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

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

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

receiver code (only one receiver)

/* YourDuinoStarter Example: nRF24L01 Receive Joystick values

 - WHAT IT DOES: Receives data from another transceiver with
   2 Analog values from a Joystick or 2 Potentiometers
   Displays received values on Serial Monitor
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe[2] = {0xE8E8F0F0E1LL,0xE8E8F0F0E2LL}; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
//int joystick[2];  // 2 element array holding Joystick readings
  int lightLevel[2];
  int lightLevel2[2]; // ??
  int redLed = 2;
  int greenLed = 4;
  int redLed2 = 7;
  int greenLed2 = 8;

void setup()   /****** SETUP: RUNS ONCE ******/
{
 
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe[0]);
  radio.openReadingPipe(2,pipe[1]);
  radio.startListening();
  pinMode(redLed, OUTPUT);
  pinMode(greenLed,OUTPUT);
  pinMode(redLed2, OUTPUT);
  pinMode(greenLed2,OUTPUT);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    //while (!done)
    {
      // Fetch the data payload
     radio.read( lightLevel, sizeof(lightLevel) );
      Serial.print("Transmitter: ");
      Serial.println(lightLevel[0]);
     Serial.print("Light level : ");
      Serial.println(lightLevel[1]);

      //Serial.print(" Y = ");     
     // Serial.println(joystick[1]);
     if ( lightLevel[1] < 500)  //now [1]
     {
      digitalWrite(greenLed, LOW);
      digitalWrite(redLed, HIGH);
      delay(500);
     }
     if (lightLevel[1] > 501);
     {
      digitalWrite(greenLed, HIGH);
      digitalWrite(redLed,LOW);
      delay(500);
     }
     
          
    }
  }
  else
  {   
      Serial.println("No radio available");
      digitalWrite(greenLed,LOW);
      digitalWrite(redLed,HIGH);
      digitalWrite(greenLed2,LOW);
      digitalWrite(redLed2,HIGH);
      delay(500);
      digitalWrite(redLed,LOW);
      digitalWrite(redLed2,LOW);
      delay(500);
     
     
  }

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

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

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

6v6gt:
The main differences are in the use of the array:
int lightLevel[2]
to pass information from the transmitters to the receiver.
Once you get Serial.print on the receiver to display readings from both transmitters, then attempt to get the LEDs working.

Transmitter code
modify 1 line in setup() to distinguish between transmitter 1 and transmitter 2

Thank you 6v6gt for your help and even modifying the code and commenting. I only just realised when you mentioned in the quote that and array say light[2] for example, that the light[1] is the identifier and light[2] is the value, or thats what I think anyway. But thanks to you I have managed to get the two transmitters working and sending data to one receiver and the corrosponding leds do flash as intended. I will post the code so you can check if you like.

I have if statements in there to determine what the leds should do depending on the light sensor value but not sure if thats the best way or not, but it seems to work so far.

I also have attached a photo as well for reference.

Receiver code

/* YourDuinoStarter Example: nRF24L01 Receive Joystick values

 - WHAT IT DOES: Receives data from another transceiver with
   2 Analog values from a Joystick or 2 Potentiometers
   Displays received values on Serial Monitor
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe[2] = {0xE8E8F0F0E1LL,0xE8E8F0F0E2LL}; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/

  int lightLevel[2];
  int lightLevel2[2];
  int redLed = 2;
  int greenLed = 4;
  int redLed2 = 7;
  int greenLed2 = 8;

void setup()   
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe[0]);
  radio.openReadingPipe(2,pipe[1]);
  radio.startListening();
  pinMode(redLed, OUTPUT);
  pinMode(greenLed,OUTPUT);
  pinMode(redLed2, OUTPUT);
  pinMode(greenLed2,OUTPUT);
}


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    //while (!done)
    {
      radio.read( lightLevel, sizeof(lightLevel) );
      radio.read( lightLevel2, sizeof(lightLevel2) );
      Serial.print("Light level ");
      Serial.println(lightLevel[1]);
      Serial.print("Light level2 ");
      Serial.println(lightLevel2[1]);
      
     
     if ( lightLevel[1] < 500 && lightLevel2[1] < 500)
     {
      digitalWrite(greenLed,LOW);
      digitalWrite(redLed, HIGH);
      digitalWrite(greenLed2,LOW);
      digitalWrite(redLed2, HIGH);
      delay(500);
     }

     if ( lightLevel[1] > 500 && lightLevel2[1] > 500)
     {
      digitalWrite(greenLed,HIGH);
      digitalWrite(redLed, LOW);
      digitalWrite(greenLed2,HIGH);
      digitalWrite(redLed2, LOW);
      delay(500);
     }

     if ( lightLevel[1] > 500 && lightLevel2[1] < 500)
     {
      digitalWrite(greenLed,HIGH);
      digitalWrite(redLed, LOW);
      digitalWrite(greenLed2,LOW);
      digitalWrite(redLed2, HIGH);
      delay(500); 
     }

     if ( lightLevel[1] < 500 && lightLevel2[1] > 500)
     {
      digitalWrite(greenLed,LOW);
      digitalWrite(redLed, HIGH);
      digitalWrite(greenLed2,HIGH);
      digitalWrite(redLed2, LOW);
      delay(500);
     }
          
    }
  }
  else
  {    
      Serial.println("No radio available");
      digitalWrite(greenLed,LOW);
      digitalWrite(redLed,HIGH);
      digitalWrite(greenLed2,LOW);
      digitalWrite(redLed2,HIGH);
      delay(500);
      digitalWrite(redLed,LOW);
      digitalWrite(redLed2,LOW);
      delay(500);
      
     
  }

}

and the transmitter code

/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
 - WHAT IT DOES: Reads Analog values on A0, A1 and transmits
   them over a nRF24L01 Radio Link to another transceiver.
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   - 
   Analog Joystick or two 10K potentiometers:
   GND to Arduino GND
   VCC to Arduino +5V
   X Pot to Arduino A0
   Y Pot to Arduino A1
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10
#define LIGHTLEVEL A0
//#define JOYSTICK_Y A1

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = {0xE8E8F0F0E1LL}; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/

int lightLevel2[2];
int lightValue2;


void setup()   /****** SETUP: RUNS ONCE ******/
{
  
  lightLevel2[0] = 2;
  //Serial.begin(38400);
  radio.begin();
  radio.openWritingPipe(pipe);
    
}


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
    
  lightValue2 = analogRead(A0);
  lightLevel2[1] = 1023 - lightValue2;
  //Serial.print("Light reading 2: ");
 // Serial.println(lightLevel2[1]);
  delay(500);
  radio.write(lightLevel2, sizeof(lightLevel2));

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

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

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