nRF24 help please

Super beginner. I'm just trying to get a test going to see if my RF24s are communicating using a push button and led. I am using two arduino UNOs and have several RF24s to swap out. I have the Transmitter with a push button with 5vs before it and with a jumper after the button to pin 2 for reading, and then an led and 220ohm resistor to ground. The Receiver is just an led connected to pin 5 then followed by another 220ohm resistor to ground.

Transmitter code-

#include <RF24.h>
#include <SPI.h>
#include <nRF24L01.h>
RF24 radio(7,8);

const byte address[6] = "00001";
int button = 0;

void setup() {
  pinMode(2, INPUT);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MAX);
  radio.stopListening();
}

void loop() {
  button = digitalRead(2);
  if(button == HIGH){
    radio.write(&button, sizeof(button));
    delay(1000);
  }
}

Receiver code-

#include <RF24.h>
#include <SPI.h>
#include <nRF24L01.h>
RF24 radio (7, 8);

const byte address[6] = "00001";


void setup() {
  pinMode(5, OUTPUT);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MAX);
  radio.startListening();
}

void loop() {
  int button;
  delay(5);
   if(radio.available()){
      radio.read(&button, sizeof(button));
      if (button == HIGH){
        digitalWrite(5, HIGH);
    }
      else {
        digitalWrite(5, LOW);
      }   
   }
}

edit: I have been unable to get the led to turn on. I forgot to mention that part.

The else to turn the LED off in the receive code never can execute. All that is received are HIGHs.

ok. So more of an else if statement is needed?

else if (button == LOW) {
digitalWrite(5, LOW);

I believe you need to radio.read prior to checking if radio is avaliable, and I never had luck communicating with the NRF without a 1MB data rate

Or send send the state of the button instead of only sending when the button is pushed. Right now you are only sending ones if anything.

Try this code. Replaces the transmitter loop() function. It reads the button every 50 milliseconds and transmits the state (0 or 1). The LED on the receiver should now follow the transmitted button state. Get rid of the delay in the receive loop().

void loop()
{

   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      button = digitalRead(2);
      radio.write(&button, sizeof(button));
   }
}

You do not. Until there is a packet available there is nothing to read.

Thank you, I will give it a shot and see what rate I am transmitting at.

Also, I think the data you are sending being an integer should be called in the read without the & symbol

radio.read(&button, sizeof(button));

I think should be

radio.read(button, sizeof(button)); as you're not sending an array

Can someone confirm that one?

ok so I got the led blinking with groundFungus' code, but it is not even and very dim. Should I add a capacitor or anything to even out the voltage? I will also try taking out the & and see if that changes something. Thank you guys for your help.

&button is the address of the button variable. The button variable does not have to be an array.

1 Like

How is your button switch wired. Is there an external pulldown resistor connected to the button switch input pin (2)?

No. just the 220ohm after an led that comes after the input pin (2).

Should I have one there?

I suggest that you lose the LED and 220R from the button pin and just put a 10K resistor from the button pin to ground. If you want an LED to follow the button state, put it on its own pin.

The way that you have it, the button pin floats when the switch is open. A floating pin's state is indeterminate (random).

Ok. I did that. Should the Transmitter send the data in intervals if I hold the button down? It seems the led is having a hard time.

I tried taking out the timer code and just left this,

      button = digitalRead(2);
      radio.write(&button, sizeof(button));

would this just leave the light solid when I hold down the button? It still just kind of blinks every now and then.

With the timer, the transmitter would sent 20 times a second. Without the timer the transmitter is trying to send, perhaps, thousands of times a second. Too much.

Oh ok. That makes sense. I will put that back.

The LED on the receiver should follow the state of the switch on the transmitter.

I made some changes to your code so that I could test on my Unos with rf24 radios. So the CE and CSN pins are changed. You will need to change those pin numbers back to your setup. I gave all the pins meaningful names. I changed the switch on the transmitter to active low (which is best practice, nowadays). So the switch is connected to ground and an input set to INPUT_PULLUP to use the internal pullup. No external resistor needed. The input reads LOW when the switch pressed. The LED on the receiver turns on when the transmitter switch is closed and turns off when the transmitter switch is open.

Transmit code;

#include <RF24.h>
#include <SPI.h>
#include <nRF24L01.h>
RF24 radio(9, 10);  // ********* changed pins for my setup

const byte address[6] = "00001";
bool buttonState = 0;
const byte buttonPin = 2;


void setup()
{
   pinMode(buttonPin, INPUT_PULLUP);
   radio.begin();
   radio.openWritingPipe(address);
   radio.setPALevel(RF24_PA_MAX);
   radio.stopListening();
}

void loop()
{

   static unsigned long timer = 0;
   unsigned long interval = 50;
   if (millis() - timer >= interval)
   {
      timer = millis();
      buttonState = digitalRead(buttonPin);
      radio.write(&buttonState, sizeof(buttonState));       
   }
}

// Receive code.

#include <RF24.h>
#include <SPI.h>
#include <nRF24L01.h>
RF24 radio (9, 10);   // ********* changed pins for my setup

const byte address[6] = "00001";

const byte ledPin = 5;

void setup()
{
   pinMode(ledPin, OUTPUT);
   radio.begin();
   radio.openReadingPipe(0, address);
   radio.setPALevel(RF24_PA_MAX);
   radio.startListening();
}

void loop()
{
   int buttonState;  
   if (radio.available())
   {
      radio.read(&buttonState, sizeof(buttonState));
      
      if (buttonState == LOW)
      {
         digitalWrite(ledPin, HIGH);
      }
      else
      {
         digitalWrite(ledPin, LOW);
      }
   }
}

Ok. When I press and hold it, it will blink once and then it won't for about 5 seconds and it might do it a couple times real fast or be really really dim and flicker before shutting all the way off and repeating. Could this be a power issue with the RF24s or Arduinos?

The code that I posted last, works fine on my setup.

I don't know. I have no way of knowing how your rf24s are powered.

The simple rf24 tutorial discusses the problems and solutions with powering the radios.

The tutorial is what I used to learn about those radios. It may be good for you to try the example codes. I know that they work with properly set up radios.

I definitely will look into it. I saw your code and have been putting it in and now I can't get the led to turn off. I must have something miswired. I will check the pinout again. Thank you again for taking time to help me figure this out, especially late at night. I am done for now, but will continue tomorrow. Thanks again. I did learn a lot.