nRF24 / RF24

OSI Layer 2 driver for nRF24L01 on Arduino & Raspberry Pi/Linux Devices
https://nrf24.github.io/RF24
GNU General Public License v2.0
2.2k stars 1.01k forks source link

[Question] RF24 not receiving any payload #744

Closed JuampiCarosi closed 3 years ago

JuampiCarosi commented 3 years ago

Hello, I’m trying to communicate 2 Arduinos nano with RF24 antennas. I’m using a A1117 to take the 5v port to 3.3v on both arduinos.

Here is the radio.printPrettyDetails() and the code of my arduinos:

Receiver

14:06:20.871 -> SPI Frequency       = 10 Mhz
14:06:20.905 -> Channel         = 108 (~ 2508 MHz)
14:06:20.941 -> RF Data Rate        = 1 MBPS
14:06:20.941 -> RF Power Amplifier  = PA_LOW
14:06:20.976 -> RF Low Noise Amplifier  = Enabled
14:06:21.011 -> CRC Length      = 16 bits
14:06:21.049 -> Address Length      = 5 bytes
14:06:21.049 -> Static Payload Length   = 32 bytes
14:06:21.118 -> Auto Retry Delay    = 1500 microseconds
14:06:21.152 -> Auto Retry Attempts = 15 maximum
14:06:21.152 -> Packets lost on
14:06:21.189 ->     current channel = 0
14:06:21.222 -> Retry attempts made for
14:06:21.256 ->     last transmission   = 0
14:06:21.256 -> Multicast       = Disabled
14:06:21.289 -> Custom ACK Payload  = Disabled
14:06:21.326 -> Dynamic Payloads    = Disabled
14:06:21.364 -> Auto Acknowledgment = Disabled
14:06:21.401 -> Primary Mode        = TX
14:06:21.401 -> TX address      = 0xe8e8f0f0e1
14:06:21.434 -> pipe 0 (closed) bound   = 0xe8e8f0f0e1
14:06:21.469 -> pipe 1 ( open ) bound   = 0xe8e8f0f0e1
14:06:21.503 -> pipe 2 (closed) bound   = 0xc3
14:06:21.538 -> pipe 3 (closed) bound   = 0xc4
14:06:21.573 -> pipe 4 (closed) bound   = 0xc5
14:06:21.607 -> pipe 5 (closed) bound   = 0xc6
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>
#include <SPI.h>

const uint64_t pipeIn = 0xE8E8F0F0E1LL; 
RF24 radio(9, 10);  
struct Received_data {
  byte pwm;
};

Received_data received_data;

int ch1_value = 0;

void reset_the_Data()  {
  received_data.pwm = 0;
}

void setup() {
  reset_the_Data();
  Serial.begin(9600);
  radio.begin();
  radio.setAutoAck(false);
  radio.setPALevel(RF24_PA_LOW);
  radio.setChannel(108); 
  radio.openReadingPipe(1,pipeIn);
  printf_begin();

  radio.startListening();
  radio.printPrettyDetails();

}

unsigned long lastRecvTime = 0;

void receive_the_data() {
  while ( radio.available() ) {
  radio.read(&received_data, sizeof(Received_data));
  lastRecvTime = millis(); 
}
}

void loop() {

  receive_the_data();

  unsigned long now = millis();
  if ( now - lastRecvTime > 1000 ) {
    reset_the_Data();

  } 

  ch1_value = map(received_data.pwm, 0, 255, 0, 255);
  Serial.print("Radio available: ");
  Serial.println(radio.available());
  Serial.print("Recibo (");
  Serial.print(lastRecvTime);
  Serial.print("): "); 
  Serial.println(ch1_value);

}

Transmitter

Start Radio
14:07:46.018 -> SPI Frequency       = 10 Mhz
14:07:46.056 -> Channel         = 108 (~ 2508 MHz)
14:07:46.089 -> RF Data Rate        = 1 MBPS
14:07:46.124 -> RF Power Amplifier  = PA_LOW
14:07:46.124 -> RF Low Noise Amplifier  = Enabled
14:07:46.161 -> CRC Length      = 16 bits
14:07:46.195 -> Address Length      = 5 bytes
14:07:46.230 -> Static Payload Length   = 32 bytes
14:07:46.266 -> Auto Retry Delay    = 4000 microseconds
14:07:46.300 -> Auto Retry Attempts = 15 maximum
14:07:46.338 -> Packets lost on
14:07:46.338 ->     current channel = 0
14:07:46.374 -> Retry attempts made for
14:07:46.412 ->     last transmission   = 0
14:07:46.446 -> Multicast       = Disabled
14:07:46.446 -> Custom ACK Payload  = Disabled
14:07:46.481 -> Dynamic Payloads    = Disabled
14:07:46.518 -> Auto Acknowledgment = Disabled
14:07:46.552 -> Primary Mode        = TX
14:07:46.587 -> TX address      = 0xe8e8f0f0e1
14:07:46.621 -> pipe 0 (closed) bound   = 0xe8e8f0f0e1
14:07:46.621 -> pipe 1 (closed) bound   = 0xe8e8f0f0e1
14:07:46.691 -> pipe 2 (closed) bound   = 0xc3
14:07:46.725 -> pipe 3 (closed) bound   = 0xc4
14:07:46.763 -> pipe 4 (closed) bound   = 0xc5
14:07:46.800 -> pipe 5 (closed) bound   = 0xc6
#include <printf.h>
#include <nRF24L01.h>
#include <RF24_config.h>
#include <RF24.h>
#include <SPI.h>

const uint64_t my_radio_pipe = 0xE8E8F0F0E1LL;
RF24 radio(9, 10);   

struct canales_radio {
  byte pwm;

};

canales_radio enviar;

void setup() {

  Serial.begin(9600);
  radio.begin();
  printf_begin();
  radio.setAutoAck(false);
  radio.setPALevel(RF24_PA_LOW);
  radio.setChannel(108); 
  radio.setRetries(15, 15);
  radio.openWritingPipe(my_radio_pipe);
  enviar.pwm = 0;
  Serial.println("Start Radio");
  radio.printPrettyDetails();
}

void loop() {

  enviar.pwm = map(analogRead(A0), 0, 1024, 0, 255);

  if(radio.write(&enviar, sizeof(canales_radio))){
    Serial.print("Sending: ");
  }else {
   Serial.print("Not sent: ") ;
   }
  Serial.println(map(analogRead(A0), 0, 1024, 0, 255));

}
2bndy5 commented 3 years ago

You should call stopListening() at the end of you transmitter's setup() function (preferably right before you call printPrettyDetails()). There is a difference between a transmitter not sending and receiver not receiving. Does your transmitter code ever print Not sent: ... (I doubt it should because you disable auto-ack)?

Additional advice

Please be cautious of following unofficial example code (the code you posted feels like it didn't come from any of the official RF24 library examples).

This

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

could be reduced to

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

Its better (in my experience) to pass an instantiated object to sizeof() (not the datatype of the instantiated object). So

sizeof(canales_radio)
// and 
sizeof(Received_data)

should be

sizeof(enviar)
// and
sizeof(received_data)

It might also be better to setPayloadSize(sizeof(data_variable)) for various reasons; most importantly radio.read() can leave the top RX FIFO level occupied with the received payload that was appended with 0s if setPayloadSize() wasn't called on both receiver and transmitter. In your case, I suspect the code you posted would transmit 32 bytes when the last 31 bytes would just be all 0s

JuampiCarosi commented 3 years ago

Thanks for the answer. I did all the changes you recommended, I had auto-ack disabled for debugging purposes since it wasn't working and several forums said to disable it if I had any troubles. However there is still no connection, I called radio.stopListening() on my setup as you instructed just before printPrettyDetails() and set the payload size, on the transmitter code I did

  radio.setPayloadSize(sizeof(to_send.pwm));
  Serial.println(sizeof(to_send.pwm));

And on the receiver I just wrote radio.setPayloadSize(1); Also once I set auto-ack back to true it is printed Not sent:

Notes

2bndy5 commented 3 years ago

@stanekTM I'm not sure your input is relevant (it actually feels like spam).

@juampapo546 it sounds like your project is suffering from power supply noise. Are you using a capacitor on the nRF24 VCC & GND? Lots of people recommend turning off auto-ack because most of the clone/counterfeit chips don't work with auto-ack enabled (and partly because people don't understand what the feature does or how it works).

Since you enabled auto-ack, the other thing to pay attention to (on the transmitter) is the "Retry attempts made for last transmission" output of printPrettyDetails(). This will tell you how many times the radio tried to send for the last call to write(). Again this info is only relevant when auto-ack is enabled.

Use sizeof(to_send) in setPayloadSize() (it's just better practice).

ch1_value = map(received_data.pwm, 0, 255, 0, 255);

Just copies received_data.pwm to the variable ch1_value

JuampiCarosi commented 3 years ago

No, we're not using a capacitor just the voltage regulator. Which capacitor you recommend us to get (how much µF)? Afaik both arduinos are original, unless we've been scammed (hope not haha). Btw, auto-ack makes sure connection is established before sending any packages right?

It makes 15 attempts :

18:22:43.441 -> Retry attempts made for
18:22:43.441 ->     last transmission   = 15

Just copies received_data.pwm to the variable ch1_value

We know that, we were previously mapping to another values but left it that way temporarily until we can get the antennas to communicate

@stanekTM I'm not sure your input is relevant (it actually feels like spam).

Lol it kinda does

EDIT

I left it running some hours and I saw lastRecvTime was not 0 so I scrolled up and saw this

18:42:28.401 -> Radio available: 1
18:42:28.438 -> Received  (time: 0): 0
18:42:28.438 -> Radio available: 0
18:42:28.476 -> Received  (time: 200049): 254

It received 21 times 254 (It actually is what I was sending), so... I need a capacitor then?

2bndy5 commented 3 years ago

No, we're not using a capacitor just the voltage regulator

Wait, what?! The capacitor question would've been my first, but linear voltage regulators usually require these (as specified by their datasheet). If you're using just the 3V output from the Arduino, then a 100uF is a safe bet to use. However, you should look at the datasheet for the A1117 regulator; it should tell you what capacitance to use (usually need at least 2 capacitors - 1 on Vin and GND & 1 on Vout & GND) Ironically, the A1117 is similar to the voltage regulator used on the adapter boards (which has builtin capacitors) specifically made for the nRF24L01+PA+LNA modules. If your're trying to say that you're using that adapter board, then try adding a small (I've used 2uF in the past) capacitor on the adapter board's voltage input.

JuampiCarosi commented 3 years ago

Using the 3v output (with or without capacitor) won't deliver enough power (I'm guessing) since it doesn't power up the antenna, I'm searching for the A1117 datasheet to see what capacitor I should get and I'll report

2bndy5 commented 3 years ago

Btw, auto-ack makes sure connection is established before sending any packages right?

Actually, auto-ack makes the transmitter automatically enter RX mode and wait for an Acknowledgement (ACK for short) packet from the receiver (which is only transmitted if the receiver actually received a payload and the auto-ack feature is enabled for both transmitter and receiver). After waiting for the ACK, the transmitter goes back to TX mode (successful or not).

I'm searching for the A1117 datasheet to see what capacitor I should get and I'll report

Do you have a store link? Websites like alibaba, amazon, ebay, etc don't always provide a link to the datasheet. ☹️

2bndy5 commented 3 years ago

This may not be the same exact part, but just as a sample:

won't deliver enough power (I'm guessing) since it doesn't power up the antenna

this sounds like you're using a PA/LNA radio module. I recently added vital info to the COMMON_ISSUES.md about that module.

JuampiCarosi commented 3 years ago

Do you have a store link? Websites like alibaba, amazon, ebay, etc don't always provide a link to the datasheet. ☹️

Ok so it did have a datasheet, looking at it rn

EDIT Ok so I need to get 10uf and 22uf capacitors and 2 resistors, I'll report if it solves my issue

this sounds like you're using a PA/LNA radio module. I recently added vital info to the COMMON_ISSUES.md about that module.

Yes it is

2bndy5 commented 3 years ago

Performance on those PA/LNA modules could also be improved by adding Electromagnetic shielding (I've added instructions with pictures to the COMMON_ISSUES.md file).

2bndy5 commented 3 years ago

@juampapo546 did you get those capacitors? more importantly, did you get the radio working reliably? (I'm looking to close this)

JuampiCarosi commented 3 years ago

@2bndy5 my partner got them but due to timing and work we couldn't test them, we will probably get to it on the next four-day holiday

JuampiCarosi commented 3 years ago

I got the capacitors and it's still not working, I'll return the antennas

2bndy5 commented 3 years ago

That's disappointing.

Just curious, can you post the store link to the module you bought? (so others can avoid going through this)