RF24 wireless serial- need help

Good morning!
I desperately need to use the RF24 to send serial sensor data. I haven’t started building it yet, but the receiver is a Mega with 8266 chip.,will host a webserver with data from a few RF sources inside and outside.
The WeMos set up a nice webserver from outside, but I want to bring it in. I think I’ll get better temperature data if I build an actual sensor pod with 3-D printed shell, and I think a Nano or Uno would handle extended heat better.

But I haven’t gotten anything to work yet. The best I’ve gotten are some ponderous weird stuff, like the serial monitor going blank, or repeating a gibberish string. I’ve had the same problem trying to implement serial Bluetooth, but this is just way more practical.

First is the receiver code. I added some serial prints - 'Hello?, Hello??, etc. to see where the program fails. “Hello!” is not printed (line 40), which tells me I don’t get to the last }.

Should this work? Can you see why it doesn’t? This is from LastMinuteEngineers.

#include <Arduino.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//create an RF24 object
RF24 radio(9, 8); // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

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

  radio.begin();

  //set the address
  radio.openReadingPipe(0, address);

  //Set module as receiver
  radio.startListening();
}

void loop()
{ Serial.begin(9600);
  //Read the data if available in buffer
  if (radio.available())
  {Serial.println("Hello?");
    char text[32] = {0};
    Serial.println("Hello??");
    radio.read(&text, sizeof(text));
    Serial.println("Hello???");
    Serial.println(text);
    Serial.println("Hello????");
  }
  
    Serial.println("Hello!");
  }

This is the sender, just a “Hello World.”
I added “Hello?” to see where the script stopped, and it prints.

#include <Arduino.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//create an RF24 object
RF24 radio(9, 8); // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

void setup()
{
  radio.begin();

  //set the address
  radio.openWritingPipe(address);

  //Set module as transmitter
  radio.stopListening();
}
void loop()
{ Serial.begin(9600);
  //Send message to receiver
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
Serial.print("Hello?");
  delay(1000);
}

And I know I have it wired up correctly.

You are reconfiguring the Serial baud in every loop iteration? (Weird but shouldn’t cause any of the errors you’re seeing).

All the examples I’m seeing always match the size of what’s transmitted to the read buffer length – here you’re sending 12 bytes and reading it into 32 byte buffer, but that should be okay – it’s a maximum size. Also you’re ignoring the boolean return value here of both write() and read() which might give more clues.

How have you verified that? Have you executed the function

and see that it returns some senisble output? There might still be electrical errors, for example:

  • unstable power supply of the nRF24 module due to not having a big enough bypass capacitor
  • connecting a 5V device (like a ATMEGA2560 chip) to the 3.3V nRF24 device without a level shifter

There are little modules in which you can plug in a nRF24 module and they take care of the above 2 things, do you have something like that?

And finally: The RF24 library has the capability to print debug output to let you know what it’s doing.

Have you tried adding build_flags = -D SERIAL_DEBUG in the platformio.ini and observed the output on transmitter and receiver?

The sole purpose of the serial begin was to look for the “Hello”, which was only in loop.

But you do it in the receiver code too, where Serial.begin() happens in setup() already.

Anways, if above debugging measures don’t help, we’d need to see a diagram or pictures of your setup. Which of the chips (ESP8266 or ATMEGA2560) are you using, which pins exactly do you wire to the nRF24 module, what additional hardware (voltage regulators, capacitors, level shifters) do you have in place, et cetera.

Unos all have a dedicated and reliable 3.3V output. That’s my power supply.

" void printDetails(void);"

Is that something I can just add to the script? I guess it’s in the library?

I added that to .ini.

do i need a elif or something?

It says #if and #else are unterminated.

Here is the whole setup:
Using Uno and the RF2401. No resistors or capacitors.
Different scripts put CE and CSN in 7 and 8 or 8 and 9.
7- MISO- 12
6- MOSI- 11
5- SCK- 13
4- CSN- 8
3- CE- 7
3V-3.3V
Gnd-Gnd

I moved to this script, because the last one failed the serial prints:

#include <Arduino.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//create an RF24 object
RF24 radio(9, 8); // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

#ifdef SERIAL_DEBUG
  #define IF_SERIAL_DEBUG(x) ({ x; })
#else
  #define IF_SERIAL_DEBUG(x)
  #if defined(RF24_TINY)
    #define printf_P(...)

void setup()
{
  radio.begin();

  //set the address
  radio.openWritingPipe(address);

  //Set module as transmitter
  radio.stopListening();
}
void loop()
{ Serial.begin(9600);
  //Send message to receiver
  const char text[] = "Hello World";
  radio.write(&text, sizeof(text));
Serial.print("Hello?");
  delay(1000);
  void printDetails(void);
}

I think it comes with this receiver:

#include <Arduino.h>
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

//create an RF24 object
RF24 radio(9, 8); // CE, CSN

//address through which two modules communicate.
const byte address[6] = "00001";

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

  radio.begin();

  //set the address
  radio.openReadingPipe(0, address);

  //Set module as receiver
  radio.startListening();
}

void loop()
{ 
  //Read the data if available in buffer
  if (radio.available())
  {Serial.println("Hello?");
    char text[32] = {0};
    Serial.println("Hello??");
    radio.read(&text, sizeof(text));
    Serial.println("Hello???");
    Serial.println(text);
    Serial.println("Hello????");
  }
  
    Serial.println("Hello!");
  }

Okay I was actuall wrong on this. 1. Powering it by the 3.3V regulator is okay. 2. But, the logic level of the microcontroller on the Uno is 5V. And when it sends SPI messages to the nRF24L01 radio, that 3.3V logic-level device will suddenly receive 5V signals! A too high voltage on these components may permanently damage the receiving chip if it does not 5V tolerant inputs. Luckily, the nRF24L01 is a 5V tolerant device (but power supply must be 3.3V), per this. If the chip wasn’t made for that, and really not all chips are, you would need a voltage divider or a level shifter, so keep that in mind for future projects with 3.3V devices.

Per link above that’s not recommended – a bypass capacitor for voltage stabilization, especially during sending, is recommended.

1 Like

I have plenty of capacitors. I’ll look that up. Thanks.