Problems with Getting Started Sketch from RF24 library from TMRH20

Hello
I’m pretty new to try networking between two Arduinos. At present I’m trying to get the Getting Started Sketch from the RF24 library from TMRH20 to work. I’m planning to transmit data from a DS18S20 through a ATTiny84 to an Arduino Uno with the nRF24l01.
But since I don’t even manage to start with that Getting Started Sketch (from Arduino Uno to Arduino Uno) it is a bit difficulte.
It would be great if you could help me.

I followed the instructions on the following website: https://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

I think there is a problem in the communication because I don’t receive an answer on the Serial Display.

PS: This is the code that the Website talks about:

/*
* Getting Started example sketch for nRF24L01+ radios
* This is a very basic example of how to send data from one node to another
* Updated: Dec 2014 by TMRh20
*/

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

/****************** User Config ***************************/
/***      Set this radio as radio number 0 or 1         ***/
bool radioNumber = 0;

/* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
RF24 radio(7,8);
/**********************************************************/

byte addresses[][6] = {"1Node","2Node"};

// Used to control whether this node is sending or receiving
bool role = 0;

void setup() {
  Serial.begin(115200);
  Serial.println(F("RF24/examples/GettingStarted"));
  Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  
  radio.begin();

  // Set the PA Level low to prevent power supply related issues since this is a
 // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  radio.setPALevel(RF24_PA_LOW);
  
  // Open a writing and reading pipe on each radio, with opposite addresses
  if(radioNumber){
    radio.openWritingPipe(addresses[1]);
    radio.openReadingPipe(1,addresses[0]);
  }else{
    radio.openWritingPipe(addresses[0]);
    radio.openReadingPipe(1,addresses[1]);
  }
  
  // Start the radio listening for data
  radio.startListening();
}

void loop() {
  
  
/****************** Ping Out Role ***************************/  
if (role == 1)  {
    
    radio.stopListening();                                    // First, stop listening so we can talk.
    
    
    Serial.println(F("Now sending"));

    unsigned long start_time = micros();                             // Take the time, and send it.  This will block until complete
     if (!radio.write( &start_time, sizeof(unsigned long) )){
       Serial.println(F("failed"));
     }
        
    radio.startListening();                                    // Now, continue listening
    
    unsigned long started_waiting_at = micros();               // Set up a timeout period, get the current microseconds
    boolean timeout = false;                                   // Set up a variable to indicate if a response was received or not
    
    while ( ! radio.available() ){                             // While nothing is received
      if (micros() - started_waiting_at > 200000 ){            // If waited longer than 200ms, indicate timeout and exit while loop
          timeout = true;
          break;
      }      
    }
        
    if ( timeout ){                                             // Describe the results
        Serial.println(F("Failed, response timed out."));
    }else{
        unsigned long got_time;                                 // Grab the response, compare, and send to debugging spew
        radio.read( &got_time, sizeof(unsigned long) );
        unsigned long end_time = micros();
        
        // Spew it
        Serial.print(F("Sent "));
        Serial.print(start_time);
        Serial.print(F(", Got response "));
        Serial.print(got_time);
        Serial.print(F(", Round-trip delay "));
        Serial.print(end_time-start_time);
        Serial.println(F(" microseconds"));
    }

    // Try again 1s later
    delay(1000);
  }



/****************** Pong Back Role ***************************/

  if ( role == 0 )
  {
    unsigned long got_time;
    
    if( radio.available()){
                                                                    // Variable for the received timestamp
      while (radio.available()) {                                   // While there is data ready
        radio.read( &got_time, sizeof(unsigned long) );             // Get the payload
      }
     
      radio.stopListening();                                        // First, stop listening so we can talk   
      radio.write( &got_time, sizeof(unsigned long) );              // Send the final one back.      
      radio.startListening();                                       // Now, resume listening so we catch the next packets.     
      Serial.print(F("Sent response "));
      Serial.println(got_time);  
   }
 }




/****************** Change Roles via Serial Commands ***************************/

  if ( Serial.available() )
  {
    char c = toupper(Serial.read());
    if ( c == 'T' && role == 0 ){      
      Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
      role = 1;                  // Become the primary transmitter (ping out)
    
   }else
    if ( c == 'R' && role == 1 ){
      Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));      
       role = 0;                // Become the primary receiver (pong back)
       radio.startListening();
       
    }
  }


} // Loop

Try this Simple nRF24L01+ Tutorial. I have deliberately kept things simpler with no options.

...R

Hello
I tried your sketches with the tutorial and first it didn't work. So I decided to reconnect everything. And somehow it does work when Ground isn't connected. It's strange but I decided to do it without the Ground wired.
Thank you for your help :-*

techniclover:
And somehow it does work when Ground isn't connected. It's strange but I decided to do it without the Ground wired.

Can you make a simple pencil drawing showing all of YOUR connections and post a photo of the drawing.

...R

I have the same connections twice. Reciever and Transmitter:

But I disconnected the Ground because then it transmits

With the following two sketchs I’m able to send the temperature, but I can’t receive it with the second modul.

Code for sending:

#include <OneWire.h>
#include <DallasTemperature.h>
#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'};
float tempout;

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

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);



void setup() {

  Serial.begin(9600);
  Serial.println("SimpleTx Starting");
  sensors.begin();
  sensors.setResolution(9);
  // Setup and configure rf radio
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop() {
  sensors.requestTemperaturesByIndex(0);
  float tempout = sensors.getTempCByIndex(0);
  bool rslt;
  rslt =  radio.write( &tempout, sizeof(tempout) );
  if (rslt) {
    Serial.println("  Acknowledge received");
    // so you can see that new data is being sent
  }
  Serial.print(tempout);
}

Code for receiving:

#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);

float tempout; // this must match dataToSend in the TX
bool newData = false;



void setup () {
  radio.begin();
    radio.setDataRate( RF24_250KBPS );
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.startListening();
    
  Serial.begin(115200);
}

void loop () {
 if ( radio.available() ) {
        radio.read( &tempout, sizeof(tempout) );
        newData = true;
    }
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(tempout);
        newData = false;
    }
  Serial.print(tempout);
}

On the serial monitor for the sending modul I get this:

SimpleTx Starting
  Acknowledge received
25.06  Acknowledge received
25.00  Acknowledge received
25.00  Acknowledge received
25.00  Acknowledge received
25.00  Acknowledge received
25.00  Acknowledge received
25.00  Acknowledge received
25.06  Acknowledge received
25.00  Acknowledge received
25.00  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.06  Acknowledge received
25.00  Acknowledge received
25.00  Acknowledge received

For the receiving one there is this:

.000.000.000.000.000.000.000.000.000.000.000.000.000

and this continues

techniclover:
But I disconnected the Ground because then it transmits

Based on your diagram I cannot see how it could work at all if you don't have a GND connection.

Have you put a 10µF capacitor across Vcc and GND for the nRF24? That is often (but not always) necessary.

In your TX program you have float tempout in two places meaning you have two separate variables with the same name. Take the "float" off line 36 and see what happens.

...R
PS. For the future please attach your jpg file here. See this Image Guide

For me the thing with ground doesn't make sense either but it works...

I tried what you said but the message I get on the RX is still the same. Not receiving anything exept the 000.000.000

And thanks for the tutorial to add the pictures I struggles with this more than just now.

I see what part of the problem is

void loop () {
 if ( radio.available() ) {
        radio.read( &tempout, sizeof(tempout) );
        newData = true;
    }
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(tempout);
        newData = false;
    }
  Serial.print(tempout);
}

Your last line prints the variable even though nothing has been received. The purpose of the line if (newData == true) { is only to print when something arrives.

Take out the last line and see what happens.

Also (at least for testing) add a line with delay(500); as the last thing in loop() in your TX program. This will slow things down to human speeds.

...R

okay I did what you said…

i also changed on which rate i have the serial montior…

this is my new code:

//Kommunikation
#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);

float tempout; // this must match dataToSend in the TX
bool newData = false;



void setup () {

 //Kommunikation starten
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();

  Serial.begin(9600);
}




void loop () {

if ( radio.available() ) {
    radio.read( &tempout, sizeof(tempout) );
    newData = true;
  }
  if (newData == true) {
    Serial.print("Data received ");
    Serial.println(tempout);
    newData = false;
  }
  
delay(1000);

}

But it still tells me that the modul is receiving something, but just 0.00

Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00

I persume that the data gets lost on the way or something like that…
And if i cange the two nRF24l01 i have nothing changes… so i think it must be a softwareproblem

Why have you a delay() in the receiving program? I told you to put one in the TX program.

And the output from your RX program suggests to me that the TX is not sending the data correctly - so post the TX program.

In a situation like that I always go back to a pair of working programs and adapt them piece by piece. Checking with every change that it still works.

...R

Code for sending: TX:

#include <OneWire.h>
#include <DallasTemperature.h>
#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'};
float tempout;

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

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);



void setup() {

  Serial.begin(9600);
  Serial.println("SimpleTx Starting");
  sensors.begin();
  sensors.setResolution(9);
  // Setup and configure rf radio
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.setRetries(3, 5); // delay, count
  radio.openWritingPipe(slaveAddress);
}

void loop() {
  sensors.requestTemperaturesByIndex(0);
  tempout = sensors.getTempCByIndex(0);
  bool rslt;
  rslt =  radio.write( &tempout, sizeof(tempout) );
  if (rslt) {
    Serial.println("  Acknowledge received");
    // so you can see that new data is being sent
    Serial.print(tempout);
}
delay (1000);
}

Displayed based on the TX code:

24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received
24.87  Acknowledge received

RX:

//Kommunikation
#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);

float tempout; // this must match dataToSend in the TX
bool newData = false;



void setup () {

 //Kommunikation starten
  radio.begin();
  radio.setDataRate( RF24_250KBPS );
  radio.openReadingPipe(1, thisSlaveAddress);
  radio.startListening();

  Serial.begin(9600);
}




void loop () {

if ( radio.available() ) {
    radio.read( &tempout, sizeof(tempout) );
    newData = true;
  }
  if (newData == true) {
    Serial.print("Data received ");
    Serial.println(tempout);
    newData = false;
  }

}

Displayed based on RX-Code:

Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00
Data received 0.00

In a situation like that I always go back to a pair of working programs and adapt them piece by piece. Checking with every change that it still works.

Well, in fact I don’t have a pair of programs that work. The ones from the tutorial you gave me isn’t receiving too. It transmits but doesn’t receive. Same problem if I exchange the two moduls.

techniclover:
Well, in fact I don't have a pair of programs that work. The ones from the tutorial you gave me isn't receiving too.

In that case we need to fix that first.

Use the EXACT code from any one pair of my examples and if it does not work please post the pair of programs that YOU have uploaded to your Arduinos. Note that you cannot mix the Rx and Tx programs from different examples - they only work as pairs.

...R

// 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) {
         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");
          // so you can see that new data is being sent
    txNum += 1;
    if (txNum > '9') {
        txNum = '0';
    }
    dataToSend[8] = txNum;
    }
    else {
        Serial.println("  Tx failed");
    }
        prevMillis = millis();
    }
}
// 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() {
   if ( radio.available() ) {
        radio.read( &datareceived, sizeof(datareceived) );
        newData = true;
    }
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(datereceived);
        newData = false;
    }
}

I uploaded those two programs. Its the simplest pair you had in the tutorial. I took the functions and inserted them where they appear because for me its simpler to unterstand it in that way.

The TX tells me that the data is sent. But the RX only receives 0.

techniclover:
I uploaded those two programs. Its the simplest pair you had in the tutorial.

You have made changes to my programs. In Reply #12 I specifically told you not to.

Please try the unchanged programs and don't move on until they work perfectly.

...R

Okey. I did it again with you unchanged programms.

That's what I get for the TX on the Serial Monitor:

Data Sent Message 0  Acknowledge received
Data Sent Message 1  Acknowledge received
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
Data Sent Message 8  Acknowledge received
Data Sent Message 9  Acknowledge received
Data Sent Message 0  Acknowledge received
Data Sent Message 1  Acknowledge received
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
Data Sent Message 8  Acknowledge received
Data Sent Message 9  Acknowledge received
Data Sent Message 0  Acknowledge received

That's what I get for the RX on Serial Monitor:

Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received 
Data received

so no data arrives, but the program says that it's transmitted.

OK. Now we can make progress. Please post the EXACT programs that YOU uploaded to YOUR Arduino.

What is the interval between the received messages - it should be one per second?

...R

The ones from your tutorial:
Tx:

// SimpleTx - the master or the transmitter

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


#define CE_PIN   9
#define CSN_PIN 10

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;
}

Rx:

// SimpleRx - the slave or the receiver

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

#define CE_PIN   9
#define CSN_PIN 10

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;
    }
}

I added a question to Reply #16 which overlapped with your Reply #17

What is the interval between the received messages - it should be one per second?

...R

Oh… no it’s faster. I don’t know how fast but faster than the sending one