RF24 no transmission

Hi,

I developed a project and now I want to add radio (nrf24l01+) transmission functionality to it. I wrote some test code for the radio feature and it works fine as expected. However when I add the radio functionality to the completed project the begin() method returns true but there is no packet transmission. I tried putting the write method in the main loop of the transmitter; but there is still nothing on the receiver end.

I'm not sure how to proceed. There is too much code to post; I will post the test code to give an idea on what I'm attempting. The below code works even when switching sides (swapping transmitter/receiver roles) between the two ends.

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

#define RECEIVER

RF24 radio(7, 8);

uint8_t address[][6] = {"RCSYS"};

struct Data
{
  unsigned char ch1;
  unsigned char ch2;
} data;

void setup()
{ 
  Serial.begin(9600);

  if(radio.begin())
  {
    Serial.println("radio working");
  }
  else
  {
    Serial.println("not working");
  }
  
  radio.setAutoAck(false);
  radio.setRetries(0, 0);
  radio.setPALevel(RF24_PA_MAX);
  radio.setDataRate(RF24_250KBPS);
  radio.setPayloadSize(sizeof(data));
#ifdef RECEIVER
  radio.startListening();
  radio.openReadingPipe(1, address[0]);
#else
  radio.stopListening();
  radio.openWritingPipe(address[0]);
#endif
}

void loop()
{
#ifdef RECEIVER
  if (radio.available())
  {
    radio.read(&data, sizeof(data));

    Serial.print(data.ch1);
    Serial.print(" ");
    Serial.println(data.ch2);
  }
#else
  radio.write(&data, sizeof(data));
  data.ch1 += 1;
  data.ch2 += 2;
  delay(500);
#endif
}

If you read and, closely, follow Robin2's simple rf24 tutorial you should be able to get them working. That tutorial sure helped me. Run the CheckConnection.ino (look in reply #30) to verify the physical wiring between the radio module and its processor (Arduino).

Make sure the rf24 power supply can provide enough current. This is especially true for the high power (external antenna) modules. I use homemade adapters like these. They are powered by 5V and have a 3.3V regulator on the board.

If using the high powered radios make sure to separate them by a few meters. They may not work too close together.

Reset the radios by cycling power to them after uploading new code. I have found that to help. They do not reset with the Arduino.

Switch to 1MB data rate to catch the not so cloned clones.

 radio.setDataRate( RF24_1MBPS );

As I said, the test code works; it is only not working when implemented in the main project.

This disables all ACK's and on top of that disables any retries, that would not happen anyway.
So it looks like the person who wrote that code does not really know what he is doing.

What's wrong with that? I don't want acknowledgements or retries; I want to send once and if the packet isn't received it doesn't matter as other packets will be transmitted.

If the code you posted does work, then the problem is the way you have integrated that code in your main sketch. Are you sure that the receiver part is running at the time you are transmitting?

It's not wrong, but the setRetries() is useless.
Using it shows, that you don't really know what you are doing.
Do you switch the lights off, after you switched off main power,
to make sure there will be no light?

BTW, from my personal experience, it is quite rare that a single packet reaches its destination,
most need 1 or two retries.

If you don't have the type of cloned NRFs, that can not ACK properly, I would use ACK's,
to have a simple automatic retry, and a feedback to the sender, whether the packet made it.

1 Like

Packet loss isn't an issue as I'm not sending vital or important data. I mean the data isn't unique; as long as I have a good packet rate, that is sufficient.

I'm only sharing experience, and somehow your transmissions did not work, did they?

Just tested that code again now to be sure, and it works.

In the code example I provided the transmission works fine; however when incorporated into the main project it does not transmit for some reason.

And we should guess why?

I will not.

Good luck with your project.

I was desperate to get it working so tried adding acknowledgements, but with no luck. I tried swapping the roles of transmitter and receiver and still no luck. I tried a few other arrangements and they didn't work.

I was using the Arduino IDE to upload the test code I provided. I am using PlatformIO to develop my project; and to eliminate incompatibilities I tried uploading the example code from PlatformIO and it works; I'm not sure what to test next.

Great. You have some NRF24L01 code which works independently and that you can verify.
The only problem is that when you copy it (somehow) into your own main sketch, which you have said is too big to publish here, it ceases to work.

Try commenting out large chunks of your main sketch until the NRF24L01 code does start working.
Also look for pin conflicts ( 7 and 8 ) and other devices also using the SPI bus.

Here is (most) of the relevant code:
(to avoid confusion I am using PlatformIO on Visual studio code as my IDE)

Receiver:

Radio.h

#ifndef RADIO_H
#define RADIO_H

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

#define PREV_RX_INTERVAL 250

extern RF24 receiver;
extern bool radio_mode;

extern PotVals pot_vals;

void radio_setup();
void radio_loop();

#endif

Radio.cpp

#include "Radio.h"

RF24 receiver(7, 8);
bool radio_mode = false;
PotVals pot_vals;

namespace
{
    uint8_t address[][6] = {"RCSYS"};
}

unsigned long prev_rx_time = 0;

void radio_setup()
{
    if (receiver.begin())
    {
        Serial.print("begin ok");
    }

    receiver.setAutoAck(false);
    receiver.setRetries(0, 0);
    receiver.setPALevel(RF24_PA_MAX);
    receiver.setDataRate(RF24_250KBPS);
    receiver.setPayloadSize(sizeof(pot_vals));
    receiver.startListening();
    receiver.openReadingPipe(1, address[0]);
}

void radio_loop()
{
    while (receiver.available())
    {
        Serial.print("receive");
        receiver.read(&pot_vals, sizeof(pot_vals));
        radio_mode = true;

        prev_rx_time = millis();
    }

    if (millis() - prev_rx_time >= PREV_RX_INTERVAL)
    {
        radio_mode = false;
    }
}

main.cpp

#include <Arduino.h>
#include <FastLED.h>

// #include "Leds.h"
// #include "Effects.h"
#include "Radio.h"

void setup()
{
  Serial.begin(9600);

  // effect_setup();
  radio_setup();
}

void loop()
{
  while (receiver.available())
  {
    Serial.print("receive");
    receiver.read(&pot_vals, sizeof(pot_vals));
  }
}

Transmitter:

Radio.h

#ifndef RADIO_H
#define RADIO_H

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

extern RF24 transmitter;

extern PotVals pot_vals;

void radio_setup();

#endif

Radio.cpp

#include "Radio.h"


RF24 transmitter(5, 32);
PotVals pot_vals;

namespace
{
    uint8_t address[][6] = {"RCSYS"};
}

void radio_setup()
{
    if (transmitter.begin())
    {
        Serial.print("begin ok esp32");
    }

    transmitter.setAutoAck(false);
    transmitter.setRetries(0, 0);
    transmitter.setPALevel(RF24_PA_MAX);
    transmitter.setDataRate(RF24_250KBPS);
    transmitter.setPayloadSize(sizeof(pot_vals));
    transmitter.stopListening();
    transmitter.openWritingPipe(address[0]);
}

main.cpp:

#include <Arduino.h>
#include <FastLED.h>

#include "Effects.h"
#include "Pins.h"
#include "Pot.h"
#include "Radio.h"

void setup()
{
  Serial.begin(9600);

  radio_setup();
}

void loop()
{
  transmitter.write(&pot_vals, sizeof(pot_vals));
}

I've written some more test code; but there is no transmission in this one either. Here's the code:

Receiver:
Radio.h:

#ifndef RADIO_H
#define RADIO_H

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

#define RADIO_RX_INTERVAL 250

extern RF24 receiver;
extern PotData RGBdata;

bool radio_setup();
void radio_loop();
bool radio_test_timeout();
void radio_reset();

#endif

Radio.cpp:

#include "Radio.h"

RF24 receiver(7, 8);
PotData RGBdata;

uint8_t address[][6] = {"RCSYS"};

namespace
{
    unsigned long last_rx_time = 0;
}

void radio_loop()
{
    while (receiver.available())
    {
        Serial.println("receiving");

        receiver.read(&RGBdata, sizeof(RGBdata));
        last_rx_time = millis();
    }

    radio_test_timeout();
}

bool radio_test_timeout()
{
    if (millis() - last_rx_time > RADIO_RX_INTERVAL)
    {
        // Serial.println("We are here (timeout)");

        radio_reset();

        return false;
    }

    return true;
}

void radio_reset()
{
    
}

bool radio_setup()
{
    if (receiver.begin())
    {
        Serial.println("Started");

        return false;
    }

    radio_reset();

    receiver.setAutoAck(false);
    receiver.setRetries(0, 0);
    receiver.setPALevel(RF24_PA_MAX);
    receiver.setDataRate(RF24_250KBPS);
    receiver.setPayloadSize(sizeof(RGBdata));
    receiver.startListening();
    receiver.openReadingPipe(1, address[0]);

    return true;
}

Data.h:

#ifndef DATA_H

#define DATA_H

struct PotData

{

  int red = 0;
  int green = 0;
  int blue = 0;
};

#endif

main.cpp:

#include <Arduino.h>

#include "Radio.h"

void setup()
{
  Serial.begin(9600);
  
  radio_setup();
}

void loop() 
{
  radio_loop();
}

Transmitter:
Radio.h:

#ifndef RADIO_H
#define RADIO_H

#include <RF24.h>
#include <Arduino.h>
#include "Data.h"

extern RF24 transmitter;

extern PotData RGBdata;

bool radio_setup();

#endif

Radio.cpp:

#include "Radio.h"

RF24 transmitter(5, 32);
PotData RGBdata;

uint8_t address[][6] = {"RCSYS"};

bool radio_setup()
{
  if(!transmitter.begin())
  {
    return false;
  }

  transmitter.setAutoAck(false);
  transmitter.setRetries(0, 0);
  transmitter.setPALevel(RF24_PA_MAX);
  transmitter.setDataRate(RF24_250KBPS);
  transmitter.setPayloadSize(sizeof(RGBdata));
  transmitter.stopListening();
  transmitter.openWritingPipe(address[0]);

  return true;
}

Data.h:

#ifndef DATA_H
#define DATA_H

struct PotData
{
  int red = 0;
  int green = 0;
  int blue = 0;
};

#endif

main.cpp:

#include <Arduino.h>

#include "Radio.h"

void setup() 
{
  radio_setup();
}

void loop() 
{
  transmitter.write(&RGBdata, sizeof(RGBdata));
}

Again, I'm using PlatformIO; which is why I have main.cpp instead of a .ino file.

Iā€™m having difficulty matching up the braces ā€˜{ā€˜ ā€˜}ā€™ in the transmitter part Radio.cpp, function radio_setup(). This did compile ?

You are transmitting in a continuous loop. Add a delay for testing of say 1000 ms.

The extra { was a typo my bad sorry. It isn't there in the project code.

I tried adding a 100ms delay last night but it didn't work. My initial test code works fine even without adding a delay (continuous output to Serial).

One test you can make is to take your original (presumably simpler) test code and modify it to handle the new data format struct PotData you have defined for the main code.

Once that works, you can test the new transmitter against the simpler receiver to see if you can narrow down the problem.

But first, post the original code (both transmitter and receiver parts) which you have said you have tested and works.