[SOLVED] nRF24L01

Thread Starter

wernerdejong1972

Joined Sep 10, 2021
12
Hi all

I´m trying to get to arduinos to communicate together througt a nRF24L01. On the receiver side I want to turn the builtin LED to turn on every time I press a button on the transmitter. Furthermore I want to show a message on the Serial monitor that tells me the state off the button.
The part with the serial monitor works as planned but the builtin LED doesn´t turn on. What am I doing wrong?

C:
//RECEIVER
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
boolean button_state = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);

  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(0, address);   //Setting the address at which we will receive the data
  radio.setPALevel(RF24_PA_MIN);       //You can set this as minimum or maximum depending on the distance between the transmitter and receiver.
  radio.startListening();              //This sets the module as receiver
}
void loop()
{
  if (radio.available())              //Looking for the data.
  {
    char text[32] = "";                 //Saving the incoming data
    radio.read(&text, sizeof(text));    //Reading the data
    radio.read(&button_state, sizeof(button_state));    //Reading the data
    if (button_state == HIGH)
    {
      Serial.println(text);
      delay(5);
      digitalWrite(LED_BUILTIN, HIGH);
    }
    else
    {
      Serial.println(text);
      delay(5);
      digitalWrite(LED_BUILTIN, LOW);
    }
  }
  delay(5);
}
Moderator edit: added code tags like this [code] ... your code... [/code]
 

Thread Starter

wernerdejong1972

Joined Sep 10, 2021
12
Sure :)

C-like:
// Transmitter

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>

RF24 radio(9, 10); // CE, CSN       

const byte address[6] = "00001";     //Byte of array representing the address. This is the address where we will send the data. This should be same on the receiving side.

int button_pin = 2;

boolean button_state = 0;


void setup() {

pinMode(button_pin, INPUT);


radio.begin();                  //Starting the Wireless communication

radio.openWritingPipe(address); //Setting the address where we will send the data

radio.setPALevel(RF24_PA_MIN);  //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.

radio.stopListening();          //This sets the module as transmitter

}

void loop()

{

button_state = digitalRead(button_pin);

if(button_state == HIGH)

{

const char text[] = "Your Button State is HIGH";

radio.write(&text, sizeof(text));                  //Sending the message to receiver

}

else

{

const char text[] = "Your Button State is LOW";

radio.write(&text, sizeof(text));                  //Sending the message to receiver

}

radio.write(&button_state, sizeof(button_state));  //Sending the message to receiver

delay(1000);

}
 

Thread Starter

wernerdejong1972

Joined Sep 10, 2021
12
When I remove:
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();

and add
digitalWrite(LED_BUILTIN, HIGH);
in setup, the builtin LED turn on. Otherwise it´s impossible to get the LED to turn on.
 

Yaakov

Joined Jan 27, 2019
3,621
Try adding a delay at the end of the on button state block, just to check.

C++:
    {
      Serial.println(text);
      digitalWrite(LED_BUILTIN, HIGH);
      delay(1000);     
    }
 

Yaakov

Joined Jan 27, 2019
3,621
OK. More quick troubleshooting. Comment out the serial write, just leave the digital write code in the conditional block.
 

Thread Starter

wernerdejong1972

Joined Sep 10, 2021
12
Do you mean this?
C-like:
if (button_state == HIGH)

    {

      //Serial.println(text);

      digitalWrite(LED_BUILTIN, HIGH);

      delay(1000);

    }

    else

    {

      //Serial.println(text);

      digitalWrite(LED_BUILTIN, HIGH);

      delay(1000);

    }

  }
(didn´t work either...)
 
Last edited by a moderator:

Yaakov

Joined Jan 27, 2019
3,621
Yes, that's what I meant. So I have this straight, when the serial code is uncommented, it does write to the serial monitor, correct?
 

Thread Starter

wernerdejong1972

Joined Sep 10, 2021
12
" SPI uses pin 13, so you can’t really use the built in LED while using SPI. "
could this be the solution? I´m not sure, because I´ve also tried pin3 without luck.
 

Yaakov

Joined Jan 27, 2019
3,621
" SPI uses pin 13, so you can’t really use the built in LED while using SPI. "
could this be the solution? I´m not sure, because I´ve also tried pin3 without luck.
See above. pin 13 is hardwired to the builtin LED. You will ahve to put an external led on a different pin to make it work.
 
Top