2

I've been trying on and off to try and get my nrf24l01's working for some time and they never seem to work. Right now I'm just trying to turn a led on and off over the radio and it isn't working.

Here's the code for the transmitter

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

#define button 5
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
boolean buttonState = 0;
void setup() {
  pinMode(5, INPUT_PULLUP);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}
void loop() {

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

And here's the code for the receiver

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

int led = 2;
RF24 radio(7, 8); // CE, CSN
boolean buttonState = 0;
const byte address[6] = "00001";
void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
}
void loop() {
 radio.startListening();
  while (!radio.available());
  radio.read(&buttonState, sizeof(buttonState));
  if (buttonState == HIGH) 
  {
    digitalWrite(led, HIGH);
  }
  else 
  {
    digitalWrite(led, LOW);
  }
}

I have the voltage regulator which steps the 5v arduino to 3.3 to the chip so I don't think not having a capacitor (what others recommended) is a problem. I have both radio chips hooked up to arduino nanos.

For the transmitter and receiver I have CE=7,CSN=8, SCK=13, MO=11, and MI=12

I also know that my led is working and the code for my button is working. Is there a problem with my code or is there a chance that my chips are broken even if im using a voltage regulator

1
  • The capacitor is not used to regulate the voltage per say, it is used to make sure the supplied voltage is 'clean' when the radio transmits it draws 'a lot' of current real fast, and if the regulator cannot keep up with that spike, the voltage drops. I have found that using a capacitor with these radios clears up some issues.
    – Chad G
    Commented Apr 12, 2018 at 15:16

4 Answers 4

3

Interesting. I have been having an issue with nRF24L01's as well so I tried your code (modified to work toggling the transmitter and printing the output). You are possibly getting the same issue as me (nRF24L01 continuously reading closed pipe) so I modified your code as follows:

void loop() {
 uint8_t pipeNum;
 radio.startListening();
  while (!radio.available(&pipeNum));
  radio.read(&buttonState, sizeof(buttonState));
  if (pipeNum < 2) {
    if (buttonState == HIGH) 
    {
      digitalWrite(led, HIGH);
      Serial.println("ON");
    }
    else 
    {
      digitalWrite(led, LOW);
      Serial.println("OFF");
    }
  }
}

and then it worked for me. PS: I normally add a "while (!Serial);" line after Serial.begin to hold off output until the monitor is established.

2

It is not easy to find the problem remotely. Here is what I would do:

  1. My first problem have always been connections. I gave up using breadboards and other connectors. Use soldering instead to be sure your circuit is properly connected.

  2. Add some Serial.println() in both codes to see what exactly is going to be sent, and what has been received.

  3. On the sender code, add a delay(100) to the loop. That would give the module buffers a chance to process.

  4. You can improve it more by checking for the button value you read with the old value, and only send the value if the state has changed.

1

Pretty sure your issue is you're not using the capacitor. I used a 10uF capacitor and it works great.

enter image description here

You put the capacitor on pins 1 and 2, make sure ground is on pin 1.

The capacitor is to make sure there is enough power being supplied when it needs it. Without the capacitor it causes unreliable communication.

If you have other issues this is a great summary video on using the NRF24L01+ transceivers: https://www.youtube.com/watch?v=kDTJULtFPgE

0
1

Check these things first:

  • Is your power supply give a constant 3.3V and a minimum of 0.5A(500mA)?
  • Are you on the right serial baud rate?
  • Is the path from the radio to the Arduino as short as possible?
  • Are your MISO and MOSI wires twined into each other?
  • Try changing your CE to pin 9 and CSN to pin 10.

If none of these things help then, please tell me what library you are using. I had a lot of issues with the RF24 library so maybe try using the NRFLite library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.