NRF24L01 radio not working with my code, but does with the sample code. What did I mess up?

I have a program written for a little garage security system I am making. I can get the radio's to talk using the getting started example, but not with my code. So I'm trying to take just a small section and get it to work...to no avail. I have two Nano Every's. One is transmitting. It has a switch, two wires. One to (-) and the other to pin D3. It's using the built in pullup resistor. When I open the switch, it should go high and start transmitting. It will also let me know via the serial monitor that the switch is working correctly. I have verified it does. It goes from 0 to 1 when I open the switch. Here is that code:

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

const int sensorLowpin = 3;    // garage door sensor near floor
int LowSensorStatus = 0;

// create an RF24 object
RF24 radio(14, 15); // CE, CSN

// address through which two modules communicate.
byte address[][6] = {"00001", "00002"};

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

  radio.begin(); // set radio to receive

  radio.setPALevel(RF24_PA_MIN); // radio.setDataRate(RF24_1MBPS);

  // set the address
  radio.openWritingPipe(address[1]);    // writing on 00002
  radio.openReadingPipe(0, address[0]); // reading on 00001

  // radio.stopListening();   // Set module as transmitter..disabled because NRFL01 radios are not full duplex and must both remain in receive unless they want to transmit
  radio.startListening();

  pinMode(sensorLowpin, INPUT_PULLUP);  // pin d3 for sensor attached to door pulled up to +3.3V


}

void loop() {
if (sensorLowpin == HIGH)
{radio.stopListening();
 delay(100);
 const char Payload[] = "Payload";
 radio.write(&Payload, sizeof(Payload));
 delay(1000);
 radio.startListening();
}
LowSensorStatus = digitalRead(sensorLowpin);
Serial.print(LowSensorStatus);
delay(1000);
}

The radio that is receiving should just turn on an LED when it gets the signal. The LED turns on for a half second at startup to make sure it works. It turns on. So I know it works. Here is it's code:

// Include Libraries
#include <Arduino.h>
#include <SPI.h>
#include <RF24.h>

// create an RF24 object
RF24 radio(14, 15); // CE, CSN

// address through which two modules communicate.
byte address[][6] = {"00001", "00002"};

const int DoorLEDPin = 5;   

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

  radio.begin();

  radio.setPALevel(RF24_PA_MIN);

  // set the address
  radio.openWritingPipe(address[0]);
  radio.openReadingPipe(0, address[1]);

  // Set module as receiver
  radio.startListening();
  pinMode(DoorLEDPin, OUTPUT);

  digitalWrite(DoorLEDPin, HIGH);
delay(500);
digitalWrite(DoorLEDPin, LOW);
 
}


void loop() {
   if (radio.available())
  {
    char Payload[32];                       
    radio.read(&Payload, sizeof(Payload));
   if (strcmp(Payload, "Payload") == 0) 
    {
      digitalWrite(DoorLEDPin, HIGH); /
      delay(100);
    }
  }
}

Keep in mind, I copy and pasted just a small section of code from the larger program. My aim is to test each sensor and switch independently and find what's wrong. But the first switch I tested doesn't work. Can you see what's wrong with my code?

If you print the result of the read, what do you get? Copy and paste the serial monitor output into a post, please,

   radio.read(&Payload, sizeof(Payload));
   Serial.print("<");
   Serial.print(Payload);
   Serial.println(">");
   if (strcmp(Payload, "Payload") == 0) 

It looks more or less OK to me. There is at least one extraneous character in the receiver code which would stop it compiling. Also, once the receiver LED is switched on, it stays on.
If the debug statement suggestion does not help, just rebuild your code incrementally from the example you said worked, testing at each stage. You have relics in your transmitter code which appear to support the transmitter swapping its role to a receiver. Don't add that back until you can immediately test it.

post the code of this getting started example as a code-section
I guess it is this one

You should keep serial output in your code like this one

// initialize the transceiver on the SPI bus
  if (!radio.begin()) {
    Serial.println(F("radio hardware is not responding!!"));
    while (1) {}  // hold in infinite loop
  }

Serial output will be very helpful for analysing errors
best regards Stefan

I don't get anything in the serial monitor. I removed the if statement from the transmitter, the code will properly transmit if I do that. So there is something wrong with the if statement then. But what?

You mean that you removed this statement from the transmitter and it worked ? :

Or what do you mean by "properly transmit" ?

This will never be true:

if (sensorLowpin == HIGH)

I suspect you're missing a digitalRead there, or you intended to use LowSensorStatus instead of sensorLowpin.

1 Like

this short piece of text is almost useless because you haven't posted the complete sketch.

This is the reason why user @6v6gt has to ask back

guys I strongly recommend that you use more words. The summarised amount of words will be the same just more efficient than posting too short and asking back

Correct.

I have posted the complete sketch. Groundfungus asked me to include some serial print text, that is what I was responding to. I guess I could have reposted it with the additional 3 lines of code. I see how that could be useful. I’ll be sure to repost any code in the future if someone asks me to make changes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.