0

I'm trying to code a simple transmitter - Receiver program with nRF24L01

Very simple code. Compiles. But when I try to test it using serial monitor. But only the first character is printed on the screen just once.

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

#define CE_PIN 9
#define CSN_PIN 10


const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN);

int smth[1];  // the transmitter data
int x=0;  // the copy of the receiver data
char rec[1]; //the receiver data

int ledPin = 3; // choose the pin for the LED
int inPin = 7;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status


void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);  // declare LED as output
  pinMode(inPin, INPUT);    // declare pushbutton as input

  radio.begin();
  radio.openWritingPipe(pipe);  //originally transmitter
}

void loop(){
  val = digitalRead(inPin);  // read input value
  if (val==HIGH) {         // check if the input is HIGH (button released)
    digitalWrite(ledPin, HIGH);  // turn LED OFF
    Serial.println('1');
    smth[0] = '1';
    radio.write(smth, sizeof(smth));
  } else {
    digitalWrite(ledPin, LOW);  // turn LED ON
    Serial.println('0');
    smth[0] = '0';
    radio.write(smth, sizeof(smth));
  }
}

Serial Monitor :

0


Instead of printing a zeros for infinite time till I change the mode of the switch, even though if I change the mode of the switch from low to high, nothing is printed on the screen.

I'm using Library for instance http://playground.arduino.cc/InterfacingWithHardware/Nrf24L01

Any help?

  • What library are you using? – Avamander Dec 14 '15 at 8:55
  • Try changing int smth[1]; to byte smth[1];. – Gerben Dec 14 '15 at 13:19
  • Solder capacitors to the power pins of the NRF24L01. 100uF would do. – Avamander Dec 14 '15 at 17:34
0

A quick look at the library header files shows you should call radio.stopListening() before calling radio.write() for the module to switch to transmit mode. This could be what is blocking the radio.write in the else block. I also recommend you try a byte array first, to get things working, just in case ints are the issue.

0

Completing TisteAndii reply I suggest to have a look at init. According to the RF24 classe list, it seems radio.begin(); and radio.openWritingPipe(pipe); are "void" function. I personnaly use NRFLite from Dave Parson and his lib send a return value for his _radio.init function. That's interesting because a lot of lib have a "send feature" which don't return until the receiver replied. So if you turn on your nRF and send data while the receiver is OFF, you're blocked. With a test at init, you can check that and loop until the receiver is available.

Note: I let my answer, but that's not the good one. As indicate in the coment, the init function dont tell if the receiver is avaible, but only if the nRF is connected (your nRF, so not the other one).

  • There's a bit of a point to not doing things blindly, yes. But you have mis-stated it. What Dave Parson's function returns has nothing to do with the status of the other end, rather it merely indicates success in communicating with the local chip by reading back the configuration - it does not indicate anything about the remote end being powered or not as you seem to believe. With the normal RF24 you can easily read status to check wiring, also it does not block eternally but only for a given number of retires and only in the mode where it expects acknowledgement. – Chris Stratton Jan 20 '18 at 20:10
  • Agree! I suppose it depend on the LIB? In other cases, if you have a way to send and check the result, please tell as there is a lot of question about that and it seems the results are never as expected. – Peter Jan 21 '18 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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