[solved]Receiving random data with nRF24L01

Hello,
I recently got some nRF24L01 modules and intended to use them as part of a weather station project. While playing around with them, I also tried to transmit the temperature readings from an DS18B20 that I then send from my Nano to my Uno, which is supposed to display them on an LCD. However, all I get on the LCD is either 0 or a random integer number.

Here is the transmitter code:

#include<OneWire.h>
#include<DallasTemperature.h>
#include<SPI.h>
#include"RF24.h"

RF24 radio(7,8);
OneWire wire(3);
DallasTemperature temp(&wire);

float temperature;
int firstD,lastD;
byte adresses[][6] ={"1Node","2Node"};
void setup() {
 radio.begin();
 radio.setPALevel(RF24_PA_LOW);
 radio.openWritingPipe(adresses[1]); 
 temp.begin();
}

void loop() {
  temp.requestTemperatures();
  temperature=temp.getTempCByIndex(0);
  firstD=(int)temperature;
  lastD = (temperature-firstD)*100;
  if(radio.available()){
    while(radio.available()){
      radio.write(firstD,sizeof(int));
      }
  }  
      delay(1500);
}

And here the code of the Receiver:

#include<SPI.h>
#include"RF24.h"
#include<LiquidCrystal.h>

RF24 radio(7,8);
LiquidCrystal lcd(10,9,6,5,4,3);

int firstD, lastD;
float temperature;
byte adresses[][6] ={"1Node","2Node"};

void setup() {
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  lcd.begin(16,2);
  lcd.print("Temperatur");
  radio.openReadingPipe(1, adresses[0]);
  radio.startListening();
}

void loop() {
  unsigned long started_waiting_at = micros();               
    boolean timeout = false;                                   
    while ( ! radio.available() ){                             
      if (micros() - started_waiting_at > 200000 ){            
          timeout = true;
          break;
      }      
    }
    if(!timeout){
      radio.read(&firstD,sizeof(int));
      for(int i=0;i<16;i++){
        lcd.setCursor(i,1);
        lcd.print(" ");
      }
      lcd.setCursor(0,1);
      lcd.print(firstD);
      Serial.println(firstD);
      lcd.print(" gC");
    }else{
      lcd.setCursor(0,1);
      lcd.print("failed");}
    delay(1000);
}

A part of the receivers code is copied from the GettingStarted Example Sketch. I am using the RF24 Library by TMRh20. Also I am using my nRF24L01 with these base modules.

I would really appreciate any help. If you need further Information I provide them if I can.

Regards, Razhyn

radio.available() is true when a packet was received, not when the module is operational.
You want to send the temperature contained in the int,
not something that is placed in RAM at the position of the temperature.
Use '&' to get the address of the data.

Change

  if(radio.available()){
    while(radio.available()){
      radio.write(firstD,sizeof(int));
      }
  }

to

  radio.write(&firstD, sizeof(int));

Have a look at this Simple nRF24L01+ Tutorial

I suggest you get the wireless communication working reliably before you add any other functionality because wireless problems can be very difficult to debug.

...R

@Whandall I applied your suggestion, unfortunately that did not change anything. If anything, the receiver seems to timeout more often, but this might be my imagination. Any suggestions on what is happening?

@Robin2 I run the getting started sketch included in the library and it worked like it was supposed to do. I see the problem in my code rather than on the module because of that.

Thank you both for your quick reply

razhyn:
@Robin2 I run the getting started sketch included in the library and it worked like it was supposed to do. I see the problem in my code rather than on the module because of that.

I find it much easier to help if you are using examples that I am very familiar with.

Are you using the TMRh20 version of the RF24 library or the older maniacBug version?

…R

@Robin2 I am using the TMRh20 version. I also uploaded your code from the Link you posted and gave it a shot. The output of the Serial Monitors of both are attached as images. The TX one seems fine, but the RX one, there I am not too sure about. Hope that this will help further.

Thank you,
Razhyn

Images from Reply #5 so we don’t have to download them. See this Image Guide

884b3b17d1ea8ededbdcea8bea4476cee8155c3c.png

ad09a432ec1e5079eaadece7c27e6624e162d9b7.png

…R

Sorry. I can’t read your pictures. Please just copy and paste the output as text - and use the code button </> to present it separately from your comments.

…R

That is what I get on the Serial Monitor of the Transmitter.

Data Sent Message 2  Acknowledge received
Data Sent Message 3  Acknowledge received
Data Sent Message 4  Acknowledge received
Data Sent Message 5  Acknowledge received
Data Sent Message 6  Acknowledge received
Data Sent Message 7  Acknowledge received

This is the Output that I get on the Receiver:

Data received 
Data received ���������� 
Data received 
Data received 
Data received 
Data received ���������� 
Data received 
Data received 
Data received 
Data received ���������� 
Data received

I cannot say what I am receiving exactly, hope you can help me out there as I do not have a clue what this output means.

Razhyn

I forgot to ask you which of the examples in my tutorial are you using?

So there are no possible misunderstandings please post the two programs that YOU have uploaded to your Arduino.

It can be useful to de-power and re-power the Arduino after you have uploaded the program to make sure that the nRF24 resets.

Do you have a 3rd nRF24 that you can substitute so as to check if one of the others is faulty?

...R

I used the SimpleTx and SimpleRx from your simple one way transmission post.

I just noticed that i had different connections for CE and CSN than posted in the example. Now the Receiver shows nothing besides SimpleRx started.

What i uploaded to the Nano:

// SimpleRx - the slave or the receiver

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

#define CE_PIN  7
#define CSN_PIN 8

const byte thisSlaveAddress[5] = {'R','x','A','A','A'};

RF24 radio(CE_PIN, CSN_PIN);

char dataReceived[10]; // this must match dataToSend in the TX
bool newData = false;

//===========

void setup() {

    Serial.begin(9600);

    Serial.println("SimpleRx Starting");
    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
}

//=============

void loop() {
    getData();
    showData();
}

//==============

void getData() {
    if ( radio.available() ) {
        radio.read( &dataReceived, sizeof(dataReceived) );
        newData = true;
    }
}

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        newData = false;
    }
}

and this I uploaded to the Uno:

// SimpleTx - the master or the transmitter

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


#define CE_PIN   7
#define CSN_PIN 8

const byte slaveAddress[5] = {'R','x','A','A','A'};


RF24 radio(CE_PIN, CSN_PIN); // Create a Radio

char dataToSend[10] = "Message 0";
char txNum = '0';


unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000; // send once per second


void setup() {

    Serial.begin(9600);

    Serial.println("SimpleTx Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.setRetries(3,5); // delay, count
    radio.openWritingPipe(slaveAddress);
}

//====================

void loop() {
    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
        prevMillis = millis();
    }
}

//====================

void send() {

    bool rslt;
    rslt = radio.write( &dataToSend, sizeof(dataToSend) );
        // Always use sizeof() as it gives the size as the number of bytes.
        // For example if dataToSend was an int sizeof() would correctly return 2

    Serial.print("Data Sent ");
    Serial.print(dataToSend);
    if (rslt) {
        Serial.println("  Acknowledge received");
        updateMessage();
    }
    else {
        Serial.println("  Tx failed");
    }
}

//================

void updateMessage() {
        // so you can see that new data is being sent
    txNum += 1;
    if (txNum > '9') {
        txNum = '0';
    }
    dataToSend[8] = txNum;
}

Robin2:
It can be useful to de-power and re-power the Arduino after you have uploaded the program to make sure that the nRF24 resets.

That did not change anything.

Sadly no, I just have those two modules.

razhyn:
I just noticed that i had different connections for CE and CSN than posted in the example. Now the Receiver shows nothing besides SimpleRx started.

I don’t know what to make of that.

In the code in your Reply #10 you are not using the same pins as in my example. Try again using the same pins as in my example and let me know if it works.

And check at least 3 times that you have everything wired up correctly.

I can’t recall if you said whether you have a 10µF capacitor between Vcc and GND for the nRF24.

I don’t have a Nano and it just occurred to me to wonder if it can produce enough current at 3.3v. I have a Mega clone that cannot. It may be worth trying a separate 3.3v supply (a pair of AA alkaline batteries should work) with a common GND with the Arduino.

The nRF24 modules are cheap. I suggest you get a couple of spares.

…R

I tried it with the pins specified in your code. By accident, I made the Nano the transmitter instead of the uno and now the serial monitor shows Tx failed. I guess that the module is fried.

I have not put a Capacitor on them, I read that, if you are using these base modules, you don’t need them. I also use the 5V Pin for power, since the base modules have built in 3V3 regulators.

Yeah, I will order some more soon.

I went ahead and used my second nano as receiver, an guess what. Now it works like a charm. I cannot exactly say why, but it works like it is supposed to. I receive the right transmit and receive output on the serial monitor. Any Idea why it started working using the second nano?

razhyn:
Any Idea why it started working using the second nano?

Not without seeing all the programs and all the connections.

When in doubt add add the capacitors.

since the base modules have built in 3V3 regulators.

Post a link so I can see exactly what modules you have. My nRF24 modules don't have voltage regulators.

...R

Thank you all four your help and advice. I finally got around to work on this again, and all of my problems disappeared into thin air as I swapped out the old modules for the newly ordered ones.

@Robin2

This is the link to these base modules I mentioned, that are the ones I bought.

Thanks for the update.

...R