Arduino Forum

Community => Exhibition / Gallery => Topic started by: Robin2 on Aug 28, 2016, 03:38 pm

Title: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Aug 28, 2016, 03:38 pm
Introduction
The nRF24L01+ 2.4GHz transceiver modules are cheap and very effective but I have come across a few Threads in which people were having trouble with them so I thought it might be useful to gather together in one place a basic step-by-step sequence for getting them to work.

Edit 29 Aug 2016. I have today downloaded and successfully tested all 3 pairs of programs with Arduino IDE 1.6.3 - 2 small corrections were needed for SimpleTxAckPayload.ino

Edit 06 Sep 2017 2016 (oops). Modified examples 1 and 2 to listen on Pipe 1 in accordance with the text

Edit 18 Mar 2017   Added Reply #29 with a simple program to check the connection with the Arduino

TMRh20 version of the RF24 library
This tutorial uses the TMRh20 version of the RF24 library (https://tmrh20.github.io/RF24/) which fixes some problems in the earlier ManiacBug version. Unfortunately, however, TMRh20 did not think to give his version a different name so it can be difficult to be certain which one is on your PC. If, before reading this, you have downloaded and installed the RF24 library the simplest thing may be to delete all traces of it and then download and install the TMrh20 version. Note that the demo programs will NOT work with the ManiacBug version of the library.


nRF24L01+ module pin connections
The nRF24 modules that I am using look like this.
(http://forum.arduino.cc/index.php?action=dlattach;topic=421081.0;attach=178852)

With the pins uppermost and the antenna on the right hand side the pin connections are
(http://forum.arduino.cc/index.php?action=dlattach;topic=421081.0;attach=178843)


Arduino connections
Because the nRF24s use SPI to communicate with the Arduino they must use Arduino pins 13, 12 and 11 (SCK, MISO and MOSI). It is also necessary to connect the CSN and CE pins and any of the Arduino I/O pins can be used for them. However for demo purposes it seems easiest to use pins 10 and 9 so that all 5 connections are adjacent on pins 13 to 9. I use some female-male jumper wires to connect to the nRF24 for test and development. The jumper wires come as a piece of ribbon cable and if you separate a piece with 5 wires and allocate the wires to suit the Arduino pins it is very easy to connect and disconnect the nRF24 from the Arduino without getting the connections mixed up.
(http://forum.arduino.cc/index.php?action=dlattach;topic=421081.0;attach=178845)
If you are using pins other than 10 and 9 for CSN and CE you must still set pin 10 for OUTPUT to ensure that the Uno acts as the SPI master.


Powering the nRF24L01+ module
The nRF24 modules require a 3.3v power supply. Be careful NOT to connect the VCC pin to a 5v power supply. I have found that they work satisfactorily when connected to the 3.3v pin on my Unos.

It is recommended to plce a 10µF capacitor across VCC and GND as near to the module as possible and I have found this essential when I made some units with an Atmega328 chip on a breadboard. The capacitor stabilizes the power supply for the module. However I have not found it necessary to use a capacitor with my Uno (or Mega). But if you believe you have followed all the other instructions correctly and are having problems it would certainly be a good idea to install a capacitor.


Resetting the nRF24
It seems that on some occasions the nRF24 does not reset after a new program is uploaded to the Arduino. This can prevent the program from working properly. If you think that is the case then disconnect the Arduino from the USB cable and reconnect it.


Some of the jargon explained
Because the nRF24s are transceivers they can obviously transmit and receive. To try to avoid confusion I will assume that you are using one them (which I will refer to as TX or "master") to transmit and another one (RX or "slave") to receive.

Like a lot of other equipment (WiFi and Bluetooth, for example) the nRF24L01+ modules broadcast on the 2.4GHz band. The precise frequency is determined by the channel that is selected. Both TX and RX must use the same channel. The default channel for the RF24 library is 76.

When the TX sends a message every RX listening on the same channel will receive the message. The TX includes an "address" in the message and the RX will ignore messages that do not have its address. The address is similar in concept to a phone number except that you cannot easily change the number of your phone. The address is a 5 byte number.

The nRF24L01+ modules can listen on any or all of 6 "pipes". Unfortunately in some RF24 demo programs the variable that is used to hold the address is given the name "pipe" even though pipes and addresses are quite different. For the purpose of this tutorial I will only be using one pipe (pipe 1) for listening. Note that pipe 0 is the only writing pipe. It is possible to listen on pipe 0 but it is simpler to leave that excusively for writing.

The 6 pipes allow an nRF24 to listen for messages from 6 other devices with 6 different addresses. But those devices may not transmit at the exact same time.


Radio interference
Note that if 2 or more TXs transmit at the same time on the same channel they will interfere with each other and the message will not be receivable. That is why an NRF24 cannot receive messages on its 6 pipes at the same instant.

Messages will also be garbled if (say) a nearby Wifi system transmits at the same time on the exact same frequency.

However each individual transmission is very short (a few millisecs) so that it is unlikely that the transmission from your TX will overlap with something else.

Edit 14 Oct 2016 -- It has been pointed out that this is not accurate. I am leaving the erroneous text so that people who may already have read it will understand the changes
Data is sent in 32 byte packets
The nRF24L01+ modules transmit data in 32 byte packets. If you only want to send a few bytes the RF24 library will automaatically pad out the message with \0 chars. If you attempt to send more than 32 bytes it will be transmitted as a number of packets. For this tutorial I will not be sending more than 32 bytes. Also, I do not intend to cover the dynamic payload feature which allows the user to select a smaller packet size to reduce transmission time.

Revised version
Data Packets
The nRF24L01+ modules can transmit a maximum of 32 bytes in a single message. If you need to send more you will need to break it into a number of separate messages. For this tutorial I will not be sending more than 32 bytes.

There are two modes of operation {A} a fixed payload size which defaults to 32 bytes and can be changed with setPayloadSize() and {B} a dynamic payload mode which is chosen with enableDynamicPayloads(). The dynamic payload mode is automatically applied when you choose the ackPayload feature (see Example 2). You can check the payload size with getDynamicPayloadSize().

When using dynamic payloads you must ensure that you read all the bytes that are received or the communication will break down.


Data validation
The nRF24s automatically include sophisticated systems to identify whether the data received matches the data that was sent. If the data is not received correctly the RX will not show data available() and will not send an acknowledgment. That means the TX will consider the transmission to have failed. You will see in the example programs that the TX is instructed automatically to retry the transmission up to 5 time before giving up. (The max is 15).

In your RX code you can therefore assume that if data is available() it will be correct.

Also note that, unlike Arduino Serial communication, when the RF24 library shows data as available() it means that the entire mesage has been correctly received.


Program style
In the example programs I have tried to use the same style that I used in Serial Input Basics (http://forum.arduino.cc/index.php?topic=396450.0) and in Planning and Implementing a Program (http://forum.arduino.cc/index.php?topic=261445.0)
By arranging the parts into small functions it should be easy to incorporate them into other programs.


Continues in next Post
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Aug 28, 2016, 03:38 pm
Simple one way transmission
The following pair of programs sends a simple message "Message N" from the TX to the RX where N is a number that increments from 0 to 9 so that you can see that successive messages are sent and received.

SimpleTx.ino
Code: [Select]


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



SimpleRx.ino
Code: [Select]


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


Edit 20 Jul 2018 ...
When working properly the Rx program should show "Data received Message n" in which n varies from 0 to 9. This should be printed at about one second intervals.
if all you see is "Data received" repeating much more quickly then there is a problem - most likely theArduino is not communicating properly with its nRF24. See Reply #29 for more info

Continues in next Post
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Aug 28, 2016, 03:38 pm
Two-way transmission
If the slave is required to send data back to the master it would seem necessary for the two devices to swap roles. Among other things that will mean that the "master" will need an address as well as the slave. The question of timing also needs to be considered - for example, how long should the master continue listening for the reply from the slave? And, if the slave is trying to write rather than listen it may miss a transmission from the master.


Two-way transmission using the ackPayload concept
The complications of swapping roles can be completely avoided by using the ackPayload concept. The idea is that the slave puts data in the ackPayload buffer BEFORE it receives a message from the master and then the data in the ackPayload buffer is sent automatically as part of the normal acknowledgment process without your code needing to make the devices swap roles.

The ackPayload concept can only be used when the amount of data transferring from slave to master is 32 bytes or less.

It also means that the data sent from the slave is always a step behind the data received by the slave. For example the ackPayoad cannot take account of the data received in the message for which it is the acknowledgment.

ackPayload example
In this example the slave will send a pair of numbers back to the master.

SimpleTxAckPayload.ino
Code: [Select]


// SimpleTxAckPayload - 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';
int ackData[2] = {-1, -1}; // to hold the two values coming from the slave
bool newData = false;

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

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

void setup() {

    Serial.begin(9600);
    Serial.println(F("Source File /mnt/sdb1/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/SimpleTxAckPayload.ino"));
    Serial.println("SimpleTxAckPayload Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.enableAckPayload();

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

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

void loop() {

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

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

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) {
        if ( radio.isAckPayloadAvailable() ) {
            radio.read(&ackData, sizeof(ackData));
            newData = true;
        }
        else {
            Serial.println("  Acknowledge but no data ");
        }
        updateMessage();
    }
    else {
        Serial.println("  Tx failed");
    }

    prevMillis = millis();
 }


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

void showData() {
    if (newData == true) {
        Serial.print("  Acknowledge data ");
        Serial.print(ackData[0]);
        Serial.print(", ");
        Serial.println(ackData[1]);
        Serial.println();
        newData = false;
    }
}

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

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



SimpleRxAckPayload.ino
Code: [Select]


// SimpleRxAckPayload- 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
int ackData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;

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

void setup() {

    Serial.begin(9600);

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

    radio.enableAckPayload();
    
    radio.startListening();

    radio.writeAckPayload(1, &ackData, sizeof(ackData)); // pre-load data
}

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

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

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

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

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

void showData() {
    if (newData == true) {
        Serial.print("Data received ");
        Serial.println(dataReceived);
        Serial.print(" ackPayload sent ");
        Serial.print(ackData[0]);
        Serial.print(", ");
        Serial.println(ackData[1]);
        newData = false;
    }
}

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

void updateReplyData() {
    ackData[0] -= 1;
    ackData[1] -= 1;
    if (ackData[0] < 100) {
        ackData[0] = 109;
    }
    if (ackData[1] < -4009) {
        ackData[1] = -4000;
    }
    radio.writeAckPayload(1, &ackData, sizeof(ackData)); // load the payload for the next time
}


Edit 05 Dec 2016 to correct the wrong pipe reference in the function updateReplyData(). Apologies for any confusion.

Edit 18 Mar 2017 - see Reply #17 for a version that works with multiple slaves

Edit 21 Feb 2019 to change setup() in RxAckPayload so the payload is uploaded after startListening()

Continues in next Post

Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Aug 28, 2016, 03:38 pm
Two-way transmission by swapping roles
I have not found a need for this approach but I will illustrate it in case it is useful for someone else.

In this example the two nRF24s (master and slave) spend most of their time listening and only switch to talking for the minimum time needed to send a message. As with the ackPayload example the slave only sends a message in response to a message from the master.

MasterSwapRoles.ino
Code: [Select]


// MasterSwapRoles

#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'};
const byte masterAddress[5] = {'T','X','a','a','a'};


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

char dataToSend[10] = "Message 0";
char txNum = '0';
int dataReceived[2]; // to hold the data from the slave - must match replyData[] in the slave
bool newData = false;

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

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

void setup() {

    Serial.begin(9600);

    Serial.println("MasterSwapRoles Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(slaveAddress);
    radio.openReadingPipe(1, masterAddress);

    radio.setRetries(3,5); // delay, count
    send(); // to get things started
    prevMillis = millis(); // set clock
}

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

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

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

void send() {

        radio.stopListening();
            bool rslt;
            rslt = radio.write( &dataToSend, sizeof(dataToSend) );
        radio.startListening();
        Serial.print("Data Sent ");
        Serial.print(dataToSend);
        if (rslt) {
            Serial.println("  Acknowledge received");
            updateMessage();
        }
        else {
            Serial.println("  Tx failed");
        }
}

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

void getData() {

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

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

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

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

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



SlaveSwapRoles.ino
Code: [Select]


// SlaveSwapRoles

#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'};
const byte masterAddress[5] = {'T','X','a','a','a'};

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

char dataReceived[10]; // must match dataToSend in master
int replyData[2] = {109, -4000}; // the two values to be sent to the master
bool newData = false;

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


void setup() {

    Serial.begin(9600);

    Serial.println("SlaveSwapRoles Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
    radio.openReadingPipe(1, slaveAddress);

    radio.setRetries(3,5); // delay, count
    radio.startListening();

}

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

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

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

void send() {
    if (newData == true) {
        radio.stopListening();
            bool rslt;
            rslt = radio.write( &replyData, sizeof(replyData) );
        radio.startListening();

        Serial.print("Reply Sent ");
        Serial.print(replyData[0]);
        Serial.print(", ");
        Serial.println(replyData[1]);

        if (rslt) {
            Serial.println("Acknowledge Received");
            updateReplyData();
        }
        else {
            Serial.println("Tx failed");
        }
        Serial.println();
        newData = false;
    }
}

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

void getData() {

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

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

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

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

void updateReplyData() {
    replyData[0] -= 1;
    replyData[1] -= 1;
    if (replyData[0] < 100) {
        replyData[0] = 109;
    }
    if (replyData[1] < -4009) {
        replyData[1] = -4000;
    }
}




Useful link
The reader will find more details and additional examples here (http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo)


End of Tutorial

...R
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Aug 28, 2016, 03:39 pm
Reserved for future use

...R
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: LMI1 on Sep 14, 2016, 07:46 pm
I have the Due and 2560 boards but no Unos. I suppose these instructions apply for them too?

I wish I could find something similar for STM32F407 Discovery boards, too.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: burhanmz on Oct 06, 2016, 07:01 pm
Some thing is wrong with RF24 lib.
Ever since I updated my RF24 library (and Arduino IDE) to the latest, the nRF24L01+ seemed to have stopped working.

They were working fine previously.
My setup is Mega 2560 R3 with one nRF24L01+ (with CE and CSN as 9 and 10 as in code)
and the other is Nano 3.0 with another nRF24L01+ (CE & CSN = 9 and 10).

I powered the radios with on board 3.3v regulator, no extra caps and it was all working fine.

Just needed to update/change my code recently and thats when it happened. Compiling my code with the new library seemed to have caused the trouble (or perhaps the IDE updated SPI.h - Arduino 1.6.12).

So I came to this thread, and copy pasted the examples for test. Still no communication.
Tried the many examples included in RF24 lib (pingpair, getting started, call handling, transfer etc)
Nothing seems to work.

BTW the radios, I bought from Chinese store online, but the Mega and Nano are genuine (bought from UK).
Just in case Arduino/nordic implemented some anti-pirated checks.

Is anyone else having problems.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: zoomx on Oct 12, 2016, 04:39 pm
Some thing is wrong with RF24 lib.
Ever since I updated my RF24 library (and Arduino IDE) to the latest, the nRF24L01+ seemed to have stopped working.
If the previous library was the one of ManiacBug some things are changed so some previous sketch doesn't work.
Take care not to have the two libraries at the same time since the IDE can use the wrong one.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Mogaraghu on Oct 13, 2016, 01:45 pm
I guess this needs correction :

In the SimpleRxAckPayload.ino sketch , inside the function updateReplyData() the following line,

radio.writeAckPayload(0, &ackData, sizeof(ackData)); // load the payload for the next time

needs to have 1 in place of 0.

Otherwise the Tx module complains of No Data feedback.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: medeslip on Nov 20, 2016, 10:50 pm
Hi,

I plugged the wire to 3.3 and 5V entry. I didn#t notice any difference.
I reused the simple example.

Here is the code:
Code: [Select]

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


and
Code: [Select]

// 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 get:
SimpleTx Starting

And
Data received
Data received
Data received

Apparently the information is not sent by the transmitter.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: batchku on Dec 01, 2016, 07:35 pm
I have also set-up two nRF's with two Uno's and am trying get the simpleTx and simpleRx one way communication working; it's not working.

I'm using this library:
https://github.com/TMRh20/RF24

I've followed Robin2's instructions, added the 10uf capacitor for stabilizing power, and my nRF modules look like the ones in his picture. 

Still no luck; i get this on the sender:

SimpleTx Starting
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
Data Sent Message 0  Tx failed
...

And this on the receiver:

SimpleRx Starting
Data received Message 0
Data received Message 0
Data received Message 0
Data received Message 0


No errors other wise.  Has anyone successfully gotten "Simple one way transmission" working with Robin2's provided SimpleTx and SimpleRx code?

Ali
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: BJHenry on Dec 05, 2016, 08:02 am
Thanks for this fantastic guide Robin, having a simple sent/receive program is really useful.
I have a strange issue when trying to use the NRF24L01 radios. I have it all wired up, but whenever my sketch calls the radio.write function the program freezes. I've used Serial.print to debug and can see that the program steps through correctly all the way until it reaches the radio.write call, and then it jut stops working completely.
I've also tried commenting out the radio.write call and the program works fine- obviously it gives a 'Tx failed' response, but that is to be expected.
This error persists in both your example sketch and all of the examples that come with the library.
Do you have any thoughts on what could be causing this? It is doing my head in.
EDIT: I should say that I am powering the radio module with an LM3940 regulator with plenty of bypass and filtering capacitance.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: husainnaji on Feb 01, 2017, 07:48 pm
pleas where is the video to this process ?
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: ABIN_RAJ on Feb 04, 2017, 08:53 pm
how can i use three transmitter and one reciever. could you pls explain with new adress format of the tmrh 20 library. iam really confused with new format
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: akatchi on Feb 11, 2017, 05:30 pm
Thanks Robin I've read it all but I'm wondering what the output looks like in the Serial monitor.
Is it actually even possible to send messages (master) like 'a' 'b' 'd' 'z', and to read these out in your Arduino (slave) to let your Arduino react to that message and then send a message back to the master.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: juan3211 on Feb 12, 2017, 10:47 pm
Hi @Robin2.

I like a lot your samples. I have already worked with NRF24 last year. 3 TX and 1 RX. Each TX with its address and its pipe, so everything works fine.

Now I want to start another project.

The RX node will be an arduino with a SIM800L GSM module. I want it to send to a web page, or with MQTT broker all the data that it receives. It acts as a gateway between NRF24 net and GPRS data.

But my question is about TX nodes. I want to do them in next months, and I don't want to reprogram the TX sketch

For example, I want to put one TX node in my room to get temperature. Two months after, I will put another one that measures wattage and voltage in my home, after another one that measures temperature and humity in the outside.

I want to program my RX gateway node to receive all these packets (different TXs and sizes) and send them to a web server.

The web server (PHP file) will read all data and "understand" it. RX gateway only cast the data. If I add in future another TX node, I have to change only PHP file to understand "another type of information" (see questions)

I have two questions:
1. does NRF24 RX node listen only one pipe and all of TX nodes writting in that pipe ? (I HAVE TO USE ACK PAYLOADs to know TX that its info has been read)
Will it be impossible for RX node to get all information ? Imagine 2 nodes --> probably OK, but with 20 nodes ????

2. My payloads have to be something like this in order to mantain flexibility for future TX nodes:


Have you got any opinion or improvement about this way of transmiting data ?

Regards,

Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: sapman on Mar 16, 2017, 04:16 pm
Robin2,

thank your for sample code.  each one of them just worked out of the box for the most part. I'm using the nRF24L01 modules with the power amp and external antenna with the base module.  I'm using a Mega as the master, one thing to point out for other people how are using the Mega, the pins are in a different location, MISO,MOIS,SCK,CSN are on pins d50-d53.

thanks,

Dan

Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Mar 18, 2017, 06:10 pm
I thought it would be helpful to add a slightly modified version of the program SimpleTxAckPayload.ino to illustrate how it can be used as a master with two slaves. And, of course, it could be extended to work with several slaves. For this example the slaves should each use the program SimpleRxAckPayload.ino with the address for one of the slaves changed to {'R','x','A','A','B'}. Please note this version does not use any extra pipes compared to the SimpleTxAckPayload program.

MultiTxAckPayload.ino
Code: [Select]



// MultiTxAckPayload - the master or the transmitter
//   works with two Arduinos as slaves
//     each slave should the SimpleRxAckPayload program
//       one with the adress {'R','x','A','A','A'}
//         and the other with {'R','x','A','A','B'}

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


#define CE_PIN   9
#define CSN_PIN 10

const byte numSlaves = 2;
const byte slaveAddress[numSlaves][5] = {
        // each slave needs a different address
                            {'R','x','A','A','A'},
                            {'R','x','A','A','B'}
                        };

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

//~ char dataToSend[10] = "Message 0";
char dataToSend[10] = "ToSlvN  0";
char txNum = '0';
int ackData[2] = {-1, -1}; // to hold the two values coming from the slave
bool newData = false;

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

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

void setup() {

    Serial.begin(9600);
    Serial.println(F("Source File = /mnt/SGT/SGT-Prog/Arduino/ForumDemos/nRF24Tutorial/MultiTxAckPayload.ino "));
    Serial.println("SimpleTxAckPayload Starting");

    radio.begin();
    radio.setDataRate( RF24_250KBPS );

    radio.enableAckPayload();

    radio.setRetries(3,5); // delay, count
        // radio.openWritingPipe(slaveAddress); -- moved to loop()
}

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

void loop() {

    currentMillis = millis();
    if (currentMillis - prevMillis >= txIntervalMillis) {
        send();
    }
        // showData(); -- moved into send()
}

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

void send() {

        // call each slave in turn
    for (byte n = 0; n < numSlaves; n++){

            // open the writing pipe with the address of a slave
        radio.openWritingPipe(slaveAddress[n]);

            // include the slave number in the message
        dataToSend[5] = n + '0';

        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("  ========  For Slave ");
        Serial.print(n);
        Serial.println("  ========");
        Serial.print("  Data Sent ");
        Serial.print(dataToSend);
        if (rslt) {
            if ( radio.isAckPayloadAvailable() ) {
                radio.read(&ackData, sizeof(ackData));
                newData = true;
            }
            else {
                Serial.println("  Acknowledge but no data ");
            }
            updateMessage();
        }
        else {
            Serial.println("  Tx failed");
        }
        showData();
        Serial.print("\n");
    }

    prevMillis = millis();
 }


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

void showData() {
    if (newData == true) {
        Serial.print("  Acknowledge data ");
        Serial.print(ackData[0]);
        Serial.print(", ");
        Serial.println(ackData[1]);
        Serial.println();
        newData = false;
    }
}

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

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


...R
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: CHEGGARI on Mar 29, 2017, 05:31 pm
In the last example, it gave me "tx failed" , even the data is received by the slaves...
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: DevilWAH on Mar 29, 2017, 10:46 pm
With the Ackpayload method can you set up different replies depending on what slave has transmitted.

So my example would be I have one device acting as a master in a hub/spoke arrangement, lets supose a master device and slave devices A,B and C 

Devices A B and C are monitoring the number of times some one presses a button connected to each device.

every x amount of time each node transmits back to the master the number of times its button has been pressed since the last update.


The master than takes all 3 values and calculates the % each button has been presses, then the next time the slave "checks in" it transmits back to each station its value to be displayed on a LED display.

so A transmits back 50, B 30 and C 20

The master calculates the % as 50,30,20%

next time A transmits back 30, B 20 and C 70.

The master would replay to A update with 50, B with 30 and C with 20 (results one step behind).

If this possible, I know in this case I could send all values back to all and have them work out what is for them but image if it was 100 devices or larger amounts of data.

Cheers
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: CHEGGARI on Apr 10, 2017, 04:54 pm
I don't know if the ack method is working well, but I advise you not to use it because as I know, when you send a request to a node(A,B or C) these have to reply immediately this will work just if you are sending requests sequentially, yet you have to use other method !!!   
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: BlackChart on Apr 17, 2017, 03:18 pm
I have this type of radio, and I won't work with this example:
https://www.aliexpress.com/store/product/10PCS-LOT-NRF24L01-PA-LNA-Onboard-Ceramic-Antenna-Wireless-Transmission-Module-905-CC1101/610196_2026712476.html


If i change this:
Code: [Select]

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


to

Code: [Select]

void getData() {
   if ( radio.available() ) {
       radio.read( &dataReceived, sizeof(dataReceived) );
       newData = true;
   } else {
       Serial.println("No radio available");
   }
}


I only get a serial console flooded with "No radio available" messages :(
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: DevilWAH on Apr 18, 2017, 11:32 pm
I don't know if the ack method is working well, but I advise you not to use it because as I know, when you send a request to a node(A,B or C) these have to reply immediately this will work just if you are sending requests sequentially, yet you have to use other method !!!   
I have been playing with it as it does work using the ack method.

The slave node builds an array of data it wants to send back. then it simply populates the buffer ready for the ACK with the first item in the array. The master polls it 5 times a second (will get faster), and collect the data via the ack method.

Because this is for racing gates and there are only up to 16 cars/drone on track, I am not limited by time, There are generally several seconds between items and / or laps. I am adding error correction so the master has to poll the slave 22 time to collect all 16 items in the array and check them off, and generally has 30+ seconds per lap to achieve this. Once up to speed I am planning to poll the slaves 9 times a second I have plenty of time to do this via acks.

I tried both having send and receive method on slaves and master, and using the ack and the ack version is much easier to code, and because i know only the master is talking and the rest are listening i can cut down on the timeouts and retries so make the process quicker.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: JoshuaACNewman on May 08, 2017, 04:13 am
After a solid couple of days working with this, I'm still not receiving any information. I see the Tx light blinking once a second on the transmitter, but nothing happens at the Rx end. I've added a bit of code just so it says, "nope" when it doesn't have any new data, and the result is a solid string of "nope"s.

I know that these modules can't take more than 3.3v, but mine came without numbered pins and I plugged them in backward, misreading which side of the board the diagram was representing. Could the 5v signal have blown the radio board? How could I confirm that?

I'm using a Nano for both Tx and Rx. Could I maybe have them plugged into the wrong pins? I've corrected my pin order so many times that I have no faith in the order they're in now. Nonetheless, I've listed the order I'm currently using in the comment at the top of the code, just so I have a reference to work from as I go.

Tx code
Code: [Select]

/*
Transmitter for nRF24L01

nRF24L01 pin    Nano pin    Color (arbitrary)
CE   03         9(any)      orange
CS/N 04         10(any)     yellow
SCK  05         13          green
MOSI 06         11          blue
MISO 07         12          purple
IRQ  08         (none)      grey

Nano pins, in order   color
9                     orange
10                    yellow
11                    blue
12                    purple
13                    green

*/

// 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);
   
    digitalWrite(10, HIGH); //sets it to master mode?

    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 Code

Code: [Select]

/*
Receiver for nRF24L01

nRF24L01 pin    Nano pin    Color (arbitrary)
CE   03         9           orange
CS/N 04         10          yellow
SCK  05         13          green
MOSI 06         11          blue
MISO 07         12          purple
IRQ  08         (none)      grey

Nano pins, in order   Color
9                     orange
10                    yellow
11                    blue
12                    purple
13                    green

*/

// 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() {
    digitalWrite(10, HIGH); //Sets to Master mode? I don't understand how it can only work in Master mode.
    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;
    }else{
      Serial.println("nope");
    }
}

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




This is like a steering wheel stuck on a pirate's crotch.
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: nurulb on Jun 11, 2017, 08:28 am
Hi Robin2

Thank's a lot for your clear explanation, and your example about simple one way transmission using NFR24L01

I Tried it with my NRF24l01 + PA + LNA
and that's work fine, i just modified a little configuration, adapt it to the hardware connection

I tried another type of simple one way transmission using push button to controll the transmiter

the main idea of the projcet is the transmitter will send "Hello World" message if we push the button on, if we do not push the button, nothing wil transmit

I use pin 7 as vcc for the push button so i set the pin become High

here is the code of Transmitter (using Arduino Mega 2560)

Code: [Select]


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 53); // CE, CSN
const byte address[][6] = {"00001","00002"};
const int pinout = 7;
const int pinin = 4; //input from button to arduino

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.openWritingPipe(address[1]);
  radio.setPALevel(RF24_PA_MIN); // it doesn't matter what power level we use (MIN, LOW, HIGH, MAX)it still works for my trial
  radio.stopListening();
  
  pinMode(pinin, INPUT);
  pinMode(pinout, OUTPUT);
}
void loop() {
  digitalWrite(pinout, HIGH);
  
  int tombol = digitalRead(pinin);
  
  if(tombol != LOW){
  txin();}
  else{
  Serial.println("Press the button to send data");
  delay(1000);}
}
void txin(){
  bool txan;
  const char data[] = "Hello World";
  txan = radio.write(&data, sizeof(data));
  delay(1000);
  if(txan)
    Serial.println("Data Sent");
  else
    Serial.println("Failed");
}




and this is the Receiver


Code: [Select]

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

RF24 radio(9, 10); // CNS, CE
const byte address[][6] = {"00001","00002"};
void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.openReadingPipe(0, address[1]);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  
}
void loop() {
  if (radio.available()) {
    char text[90] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }



and it works Properly


my question is, i try to make two way transmission which the data is variable
but i try to make simple trial

the main ide is, when i push the button on arduino(1), the first arduino will stop listening and transmit the data (hello world for example) and if we push the button in arduino (2), the second arduino will stop listening and transmit the data

and i try this code for The Arduino(1) (using Arduino uno R3)

Code: [Select]

include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); //CE,CSN
const byte address[][6] = {"00001","00002"};
const int pinout = 7;
const int pinin = 4;

void setup()
{
  // put your setup code here, to run once:
 pinMode(pinin, INPUT);
 pinMode(pinout, OUTPUT);
 
 Serial.begin(115200);
 radio.begin();
 radio.openWritingPipe(address[0]);
 radio.openReadingPipe(0,address[1]);
 radio.setPALevel(RF24_PA_MAX);
 radio.setDataRate(RF24_250KBPS);
 radio.startListening();
 
}

void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(pinout, HIGH);
  int button = digitalRead(pinin);

  if(button != LOW)
  {
    radio.stopListening();
    abc();
  }
  else
  {
    radio.startListening();
    Serial.println("Receiving Mode, Press button to send");
    delay(1000);
  }

  if( radio.available() && button == LOW )
  {
    while (radio.available())
    {
    char text[] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
    }
  }
}

void abc()
{
  bool txan;
  const char data[] = "Hello World1 ";
  txan = radio.write(&data, sizeof(data));
  delay(1000);
  if(txan)
    Serial.println("Data Sent");
  else
    Serial.println("Failed");
}


and this is the code for Arduino(2) (Using Arduino Mega 2560)
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 53); //CE,CSN
const byte address[][6] = {"00001","00002"};
const int pinout = 7;
const int pinin = 4;

void setup()
{
  // put your setup code here, to run once:
 pinMode(pinin, INPUT);
 pinMode(pinout, OUTPUT);
 
 Serial.begin(115200);
 radio.begin();
 radio.openWritingPipe(address[1]);
 radio.openReadingPipe(0,address[0]);
 radio.setPALevel(RF24_PA_LOW);
 radio.setDataRate(RF24_250KBPS);
 radio.setRetries(15, 15);
 radio.startListening();
}

void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(pinout, HIGH);
  int button = digitalRead(pinin);

  if(button != LOW)
  {
    radio.stopListening();
    abc();
  }
  else
  {
    radio.startListening();
    Serial.println("Receiving Mode, Press button to send");
    delay(1000);
  }

  if((radio.available()) && (button == LOW))
  {
   while (radio.available())
    {
    char text[50] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text); }
  }
}

void abc()
{
  bool txan;
  const char data[] = "Hello World 2";
  txan = radio.write(&data, sizeof(data));
  delay(1000);
  if(txan)
    Serial.println("Sent");
  else
    Serial.println("Failed");
}



done uploading but when i try the transmission, it always failed

I hope you can know and explain what is my mistake

thank's a Lot
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: vaj4088 on Jun 11, 2017, 05:02 pm
"...it always failed" is not very helpful.  What do your Serial.println(...) statements tell you?

Why oh why do you use delay(...) ?

Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: nurulb on Jun 12, 2017, 07:23 am
the Serial println show that the transmission is failed, it can't send the data "Hello World" to the Receiver
so in the serial monitor the message shown is

failed
failed
failed

i use delay to make it send once after 1000ms

but for the details i think it is not failed at all but, i make a mistake about how to get notification when the data sent or failed, when i try the conde again this is what happen

when i used the first code, Mega as a transmitter and when i pressed the button, this happened
(https://www.facebook.com/photo.php?fbid=1641995205825424&set=pcb.1641995615825383&type=3&theater)

and when i looked out to the uno as the receiver, it happened
(https://www.facebook.com/photo.php?fbid=1641995212492090&set=pcb.1641995615825383&type=3&theater)

so i think that's work, for send simplex mode

but this happen when i press the button on my Mega and see the serial monitor
(https://www.facebook.com/photo.php?fbid=1641995209158757&set=pcb.1641995615825383&type=3&theater)

i notice, it means that the transmission is fail


now i try to make difference for the code so we can notice if the data sent from Mega or uno

in the Mega Code, i change the string from "Hello World " to "Hello world from Mega", so when we see in the uno Serial monitor as receiver, we can know that the data is coming from arduino mega, i do it for my uno too

when i press the button on my Mega(it means that my mega is tx), i see this in the serial monitor of my Uno (it means that my uno is Rx)
(https://www.facebook.com/photo.php?fbid=1641995392492072&set=pcb.1641995615825383&type=3&theater)


and when i press the button on my uno (uno as Tx), i see this in the serial monitor of my Mega (Mega as Rx)
(https://www.facebook.com/photo.php?fbid=1641995575825387&set=pcb.1641995615825383&type=3&theater)


Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: nurulb on Jun 12, 2017, 09:43 am
i just modify my code, and now it is work
when i press the button, it will send the data, and if we do not push the button, it will stay listening

here is the code for Uno with input from button is in pin 4 and pin 7 is Vcc for the button

Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); //CE,CSN
const byte address[][6] = {"00001","00002"};
const int pinout = 7;
const int pinin = 4;

void setup()
{
  // put your setup code here, to run once:
 pinMode(pinin, INPUT);
 pinMode(pinout, OUTPUT);
 
 Serial.begin(115200);
 Serial.println("Receiving Mode, Press button to send data");
 radio.begin();
 radio.openWritingPipe(address[0]);
 radio.openReadingPipe(1,address[1]);
 radio.setPALevel(RF24_PA_HIGH);
 radio.setDataRate(RF24_250KBPS);
 radio.setRetries(15, 15);
 radio.startListening();
 
}

void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(pinout, HIGH);
  int tombol = digitalRead(pinin);

  if(tombol != LOW)
  {
    radio.stopListening();
    abc();
  }
  else
  {
    radio.startListening();
  }

  if( radio.available() && tombol == LOW)
  {
    while (radio.available())
    {
    char text[50] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
    }
  }
}

void abc()
{
  bool txan;
  const char data[] = "Hello World from uno ";
  txan = radio.write(&data, sizeof(data));
  delay(1000);
  if(txan)
    Serial.println("Data Sent");
  else
    Serial.println("Failed");



here is the code for Mega with input from button is in pin 4 and pin 7 is Vcc for the button
Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 53); //CE,CSN
const byte address[][6] = {"00001","00002"};
const int pinout = 7;
const int pinin = 4;

void setup()
{
  // put your setup code here, to run once:
 pinMode(pinin, INPUT);
 pinMode(pinout, OUTPUT);
 
 Serial.begin(115200);
 Serial.println("Receiving Mode, Press button to send");
 radio.begin();
 radio.openWritingPipe(address[1]);
 radio.openReadingPipe(1,address[0]);
 radio.setPALevel(RF24_PA_HIGH);
 radio.setDataRate(RF24_250KBPS);
 radio.setRetries(15, 15);
 radio.startListening();
}

void loop()
{
  // put your main code here, to run repeatedly:
  digitalWrite(pinout, HIGH);
  int tombol = digitalRead(pinin);

  if(tombol != LOW)
  {
    radio.stopListening();
    abc();
  }
  else
  {
    radio.startListening();
  }

  if(radio.available() && tombol == LOW)
  {
     while (radio.available())
    {
    char text[50] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
    }
  }
}

void abc()
{
  bool txan;
  const char data[] = "Hello World from mega ";
  txan = radio.write(&data, sizeof(data));
  delay(1000);
  if(txan)
    Serial.println("Sent");
  else
    Serial.println("Failed");
}
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Jul 01, 2017, 09:52 am
Sorry. I have not been monitoring this Thread - I had assumed it is locked. And I will now ask the Moderator to lock it.

If anyone reading this has a question please start your own Thread in the Networking section and I will see it.

...R
Title: Re: Simple nRF24L01+ 2.4GHz transceiver demo
Post by: Robin2 on Mar 18, 2018, 08:54 pm
There have been several Threads in which people have reported problems with the SimpleRX program repeatedly printing Data Received very quickly even though it is obvious that data is not being received. When the communication is working properly messages should only be printed once per second.

As far as I can see the problem is caused by a poor connection between the Arduino and the nRF24 module. I can reproduce the symptom by disconnecting the CSN_PIN connection.

The following program may help to diagnose this sort of problem as it just tests the connection between the Arduino and its nRF24 without attempting to send or receive any data to / from another nRF24.

I hope the messages in the program are self-explanatory

CheckConnection.ino

Code: [Select]


// 18 Mar 2018 - simple program to verify connection between Arduino
//      and nRF24L01+
//  This program does NOT attempt any communication with another nRF24

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

#include <printf.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);
    printf_begin();

    Serial.println("CheckConnection Starting");
    Serial.println();
    Serial.println("FIRST WITH THE DEFAULT ADDRESSES after power on");
    Serial.println("  Note that RF24 does NOT reset when Arduino resets - only when power is removed");
    Serial.println("  If the numbers are mostly 0x00 or 0xff it means that the Arduino is not");
    Serial.println("     communicating with the nRF24");
    Serial.println();
    radio.begin();
    radio.printDetails();
    Serial.println();
    Serial.println();
    Serial.println("AND NOW WITH ADDRESS AAAxR  0x41 41 41 78 52   ON P1");
    Serial.println(" and 250KBPS data rate");
    Serial.println();
    radio.openReadingPipe(1, thisSlaveAddress);
    radio.setDataRate( RF24_250KBPS );
    radio.printDetails();
    Serial.println();
    Serial.println();
}


void loop() {

}



...R