Go Down

Topic: RF24 Library: Driver for nRF24L01(+) 2.4GHz Wireless Transceiver (Read 236433 times) previous topic - next topic

Robin2

Try TMRh20's updated RF24 library.

Your array msg[] is defined as an int (which is 2 bytes) but this line only sends 1 byte
radio.write(msg, 1);
it should be
radio.write(msg, sizeof(msg));

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ArduStooge

Thank you! No wonder the message came out weird.

Anyway, I am trying to make receiver to turn on multiple LEDs based on the message received. I don't know if nRF24L01 can transmit multiple messages at the same time so at this time I just make instructions to turn on the LED based on the message received.

On the TX side there are currently three buttons. Every button will transmit unique message if pressed. So far I managed to instruct the RX to turn on correct LED.

Now I want the RX to understand if buttons are pressed simultaneously. So I give different unique message for every combination of pressed buttons. However, I could not figure out the right method. I managed to instruct the RX to turn on two LEDs but it doesn't turn off the LED even though one of the TX button is already depressed and the serial monitor is showing message has changed.  Sorry for my babbling, I hope you understand what I mean. Here is the code
Code: [Select]
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
int msg[1];
RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;


int LED1 = 3;
int LED2 = 4;
int LED3 = 5;


void setup(void){
 Serial.begin(9600);
 radio.begin();
 radio.openReadingPipe(1,pipe);
 radio.startListening();
 pinMode(LED1, OUTPUT);
 pinMode(LED2, OUTPUT);
 pinMode(LED3, OUTPUT);}

void loop()
  {

     radio.read(msg, 1);
     Serial.println(msg[0]);

       if (msg[0] == 250)
       {digitalWrite(LED1, HIGH);   
        }
       else

     if (msg[0] == 222)
       {
       digitalWrite(LED2, HIGH); 
       }
       
     else

      if (msg[0] == 111)
       {
       digitalWrite(LED3, HIGH); 
       }

    else
    if (msg[0] == 251)
    {digitalWrite(LED2, HIGH);
     digitalWrite(LED3, HIGH);
    }
 

    else
   
     
    PORTD &= B11000111;

Robin2

On the TX side there are currently three buttons. Every button will transmit unique message if pressed. So far I managed to instruct the RX to turn on correct LED.
I think your system will be very much simpler if you always transmit the state of all the buttons even if they don't change. That way there are always 3 values and always in the same order so the receiver has no trouble figuring things out.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

ArduStooge

I think your system will be very much simpler if you always transmit the state of all the buttons even if they don't change. That way there are always 3 values and always in the same order so the receiver has no trouble figuring things out.

...R
Do you mind to elaborate? I failed to understand your two points

terryking228

Quote
would u please help me on
what are pipes and their addresses and the relationship?
I have found this confusing to start. My take:
 
a Pipe is:

- an identifier of a logical connection between two nRF24L01 transceivers that are on the same channel (There are 125 possible channel frequencies from 2.400GHz to 2.524GHz)
  - A transceiver transmitting and the  receiving transceiver must have the same pipe name.

- a data buffer area in each transceiver. There can be 6 receiving pipes on each transceiver (But pipe 1 is taken over and used as the transmitting pipe)

See the How-To for nRF24L01 on the ArduinoInfo.Info page  HERE: and follow the example code..

And some suggestions to improve RANGE:

You can do these things to get better range:
  • Make sure you have good 3.3V power (not just plain UNO, Mega etc. on USB power). You can use a separate 3.3V supply, or a Base Module with 3.3V regulator, or a YourDuino RoboRED.
  • After you have good 3.3V power, set the RF "Power Amplifier Level" to MAX. Like this:radio.setPALevel(RF24_PA_MAX);
  • Set radio.setDataRate(RF24_250KBPS); Fast enough.. Better range
  • Set radio.setChannel(108); 2.508 Ghz - Above most Wifi Channe


Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

nelysk4

Hi folks!
Didn't create a new topic, as I have some issue with addressing using RF24 library.

Whole system contains two transmitters and one receiver (if it's possible to fix this issue - it will have 4 transmitters).
The problem I've got is, that whether I read from first or second pipe I get data sometimes from right pipe, sometimes from wrong pipe. And data getting sometimes consistent, sometimes not:

Got data on 0  Gauta: 3485
Got data on 0  Gauta: -11673
Got data on 1  Gauta: 3486
Got data on 1  Gauta: -11672
Got data on 1  Gauta: 3487
Got data on 0  Gauta: -11671
Got data on 0  Gauta: 3488
Got data on 1  Gauta: -11670
Got data on 1  Gauta: 3489
Got data on 0  Gauta: -11669
Got data on 0  Gauta: 3490
Got data on 1  Gauta: -11668
Got data on 0  Gauta: 3491
Got data on 0  Gauta: -11667
Got data on 0  Gauta: 3492
Got data on 1  Gauta: -11666
Got data on 0  Gauta: 3493
Got data on 0  Gauta: -11665
Got data on 0  Gauta: 3494
Got data on 0  Gauta: -11664
Got data on 0  Gauta: 3495
Got data on 1  Gauta: -11663
Got data on 0  Gauta: 3496
[on which pipe data was got] [the data itself]

transmitters sends increases data value by 1 each time it sends data (these are started at different times to have different numbers.

As you see in the part of the data receiving module gets it's totally random, while it reads one by one both pipes.
What could be cause of it or how to solve it?

Transmitter code (variable "pipeNumber" defines which receiver it is):
Code: [Select]
//----TRANSMITTER----
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(9,10);
//const uint64_t communication_pipes[5] = { 0xF0F0F0F0D6LL, 0xF0F0F0F0C6LL, 0xF0F0F0F0B4LL, 0xF0F0F0F0A5LL, 0xF0F0F0F096LL };
const uint64_t communication_pipes[5] = { 0xE7D3F03577LL, 0xC2C2C2C2C2LL, 0xC2C2C2C2C3LL, 0xC2C2C2C2C4LL, 0xC2C2C2C2C5LL };
const int pipeNumber = 1; // pirmas pipe = 0
uint64_t pipe = communication_pipes[pipeNumber]; // naudojamas adresas
//------ Adresavimas --------
//------ SIUNTIMO kintamieji --- START -----
bool initialize = false;
int data = 0;
//------ SIUNTIMO kintamieji --- END -----
void setup(){
  Serial.begin(250000);
  printf_begin();
}

void loop(){
  send(pipe, data);
  data++;
  delay();
}

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
int send(uint64_t pipeSEND, int dataTOsend){
  if(initialize == false) RF24_Initialization_SEND(pipeSEND);
  bool sendStatus = false;
  int retries = -1;
  printf("Siunciama: %i\n", data);
  radio.write(&dataTOsend, sizeof(dataTOsend));
}

void RF24_Initialization_SEND(uint64_t pipeSENDinit){
  printf("Transmitter %i\n", pipeNumber);
  printf("Address: ");
  Serial.println((long)pipeSENDinit, HEX);
  radio.begin();
  radio.enableAckPayload();
  radio.openWritingPipe(pipeSENDinit);
  radio.printDetails();
  initialize = true;
  printf("Initialization DONE\n");
}


Receiver code:
Code: [Select]
//----RECIEVER----
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(9,10);
//const uint64_t pipesAddress[5] = { 0xF0F0F0F0D6LL, 0xF0F0F0F0C6LL, 0xF0F0F0F0B4LL, 0xF0F0F0F0A5LL, 0xF0F0F0F096LL };
const uint64_t pipesAddress[5] = { 0xE7D3F03577LL, 0xC2C2C2C2C2LL, 0xC2C2C2C2C3LL, 0xC2C2C2C2C4LL, 0xC2C2C2C2C5LL };
const int pipeNumber[5] = {0, 1, 2, 3, 4}; // pirmas pipe = 0

int data;
const int button = 4;
bool buttonState = 0;

void setup(){
  Serial.begin(250000);
  printf_begin();
  RF24_Initialization_GET();
  pinMode(button, INPUT);
}

void loop(){
  get(pipesAddress[0], pipeNumber[0]);
  get(pipesAddress[1], pipeNumber[1]);
}

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
int get(uint64_t pipeGET, int pipeNumberTOget){
  int dataGOT = 0;
  if(radio.available()){
    bool done = false;
    while(!done){
      done = radio.read(&dataGOT, sizeof(dataGOT));
    }
    printf("Got data on %i  Gauta: %i\n", pipeNumberTOget, dataGOT);
  }
  return dataGOT;
}

void RF24_Initialization_GET(){
  radio.begin();
  radio.enableAckPayload();
  radio.openReadingPipe(pipeNumber[0], pipesAddress[0]);
  radio.openReadingPipe(pipeNumber[1], pipesAddress[1]);
 // radio.openReadingPipe(pipeNumber[2], pipesAddress[2]);
 // radio.openReadingPipe(pipeNumber[3], pipesAddress[3]);
 // radio.openReadingPipe(pipeNumber[4], pipesAddress[4]);
  radio.startListening();
  radio.printDetails();
  printf("Initialization DONE\n");
}

Robin2

You seem to have set up two receiving pipes but you only have a single read() that does not seem to differentiate between them. I confess I have not used multiple reading pipes so I would have to read the documentation to find an answer. I will leave that to you.

As you don't seem to need to send much data why not turn the whole process on its head and make what is now the receiver into a master and treat the other devices as slaves. Then the master can poll each of the slaves in turn. That is how I control a model train system. The code in the pair of programs in this link is derived from that.

I recommend using the TMRh20 version of the RF24 library - it solves some problems from the ManiacBug version

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

You can only read the next packet and determine which pipe it was received on.

There is no 'reading from a pipe'.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

guow3

Hello

I am having trouble getting this module to work using the tmrh20 lib. I am using these modules with two arduino mega and a 3.3V external power supply. I also placed 10 uF in parallel with the modules power pin. I would really appreciate any comments or suggustions.

In the code below all I am trying to do is turn on a led.
Here are my pin assignment for both arduinos
CE -> 7
CSN -> 8
SCK -> 52
MOSI -> 51
MISO -> 50
IRQ -> unconnected

Transmitter
Code: [Select]
//#include <DigitalIO.h>
#include <SPI.h>
#include "RF24.h"
#include <printf.h>
RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
unsigned long msg = 375983;

void setup() {
    Serial.begin(115200);
    printf_begin();
    Serial.println("Transmitter");
    pinMode(9, INPUT);
    radio.begin();
    radio.setChannel(108);
    radio.openWritingPipe(pipe);
    radio.startListening();
    radio.printDetails();
}

void loop() {
  radio.stopListening();
  if (digitalRead(9) == HIGH)
  {
    while (!radio.write(&msg, sizeof(msg))){
      radio.write(&msg, sizeof(msg));
}
  Serial.println("Sending.....");
  }
  else {
    Serial.println("Awaiting Input....");
  }
  delay(500);

}


Code: [Select]
STATUS = 0xff RX_DR=1 TX_DS=1 MAX_RT=1 RX_P_NO=7 TX_FULL=1
RX_ADDR_P0-1 = 0xffffffffff 0xffffffffff
RX_ADDR_P2-5 = 0x7f 0xff 0x7f 0xff
TX_ADDR = 0x0000000000
RX_PW_P0-6 = 0xff 0x7f 0xff 0x7f 0xff 0xff
EN_AA = 0xff
EN_RXADDR = 0xff
RF_CH = 0xff
RF_SETUP = 0xff
CONFIG = 0xff
DYNPD/FEATURE = 0x7f 0xff
Data Rate = 1MBPS
Model = nRF24L01
CRC Length = Disabled
PA Power = PA_MAX


Receiver
Code: [Select]
//#include <DigitalIO.h>
#include <SPI.h>
#include "RF24.h"
#include <printf.h>
RF24 radio(7,8);
const uint64_t pipe = 0xE8E8F0F0E1LL;
unsigned long msg;

void setup() {
    Serial.begin(115200);
    printf_begin();
    Serial.println("Receiver");
    pinMode(9, OUTPUT);
    radio.begin();
    radio.setChannel(108);
    radio.openReadingPipe(0,pipe);
    radio.printDetails();
 
}

void loop() {
   radio.startListening();
   digitalWrite(9, LOW);
  if(radio.available()){
    while(radio.available())
    {
    radio.read(&msg, sizeof(msg));
    Serial.println("Incoming...");
    delay(500);
    Serial.println(msg);
    }
  }
  else{
    Serial.println("No Msg");
  }
  radio.stopListening();
  Serial.println(msg);
  if (msg == 375983){
    digitalWrite(9, HIGH);
    Serial.println(msg);
  }
  delay(500);
}


Code: [Select]
STATUS = 0x00 RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=0 TX_FULL=0
RX_ADDR_P0-1 = 0x0000000000 0x0000000000
RX_ADDR_P2-5 = 0x00 0x00 0x00 0x00
TX_ADDR = 0x0000000000
RX_PW_P0-6 = 0x00 0x00 0x00 0x00 0x00 0x00
EN_AA = 0x00
EN_RXADDR = 0x00
RF_CH = 0x00
RF_SETUP = 0x00
CONFIG = 0x00
DYNPD/FEATURE = 0x00 0x00
Data Rate = 1MBPS
Model = nRF24L01
CRC Length = Disabled
PA Power = PA_MIN

nelysk4

@Robin2 Thanks!


You can only read the next packet and determine which pipe it was received on.

There is no 'reading from a pipe'.
Can you give an example? I mean, how to check on which pipe it was received on?

Whandall

#280
Mar 07, 2016, 08:27 pm Last Edit: Mar 07, 2016, 09:25 pm by Whandall Reason: added library code
Status register bits 3:1 hold this information. (From the datasheet page 59)

Quote
RX_P_NO 3:1 111 R Data pipe number for the payload available for reading from RX_FIFO
 000-101: Data Pipe Number
 110: Not Used
 111: RX FIFO Empty
From the original library
Code: [Select]
bool RF24::available(uint8_t* pipe_num)
{
  if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){
    // If the caller wants the pipe number, include that
    if ( pipe_num ){
      uint8_t status = get_status();
      *pipe_num = ( status >> RX_P_NO ) & 0b111;
    }
    return 1;
  }
  return 0;
}
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

nelysk4

Status register bits 3:1 hold this information. (From the datasheet page 59)

From the original library
Code: [Select]
bool RF24::available(uint8_t* pipe_num)
{
  if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){
    // If the caller wants the pipe number, include that
    if ( pipe_num ){
      uint8_t status = get_status();
      *pipe_num = ( status >> RX_P_NO ) & 0b111;
    }
    return 1;
  }
  return 0;
}


Thanks! Will try it out

omglasers

Hello,
Is there a way to flush the FIFO buffer without calling read() until available() returns 0 ?

vagulus

In post #107, finnduino wrote,

Code: [Select]
It's still showing this when I look status of module:
Data Rate        = 1MBPS
Model            = nRF24L01
CRC Length       = 16 bits
PA Power         = PA_HIGH


Would someone please tell me how to obtain such a status reading?
"Answers are easy;
               it's asking the right questions
                                                      which is hard."
 The Doctor (Dr Who: The Face of Evil (1977))

Go Up