Go Down

Topic: NRF24L01 simple example? (Read 32661 times) previous topic - next topic

roineust

Feb 14, 2014, 08:28 pm Last Edit: Feb 15, 2014, 02:50 am by roineust Reason: 1
Hello people!

I have 2 Arduino Unos and 2 NRF24L01.

The NRF2401 is probably an older or a close, but a far away relative, of the NRF24L01 (or is it not?) , but, for the NRF2401, i could find a simple example, that for a beginner, such as me, makes understanding the process of transmitting characters or numbers, from transmitter to receiver, just as simple as it is with the Arduino bluetooth examples.

Here is the page, where the simple examples are, for the NRF2401:

http://playground.arduino.cc/InterfacingWithHardware/Nrf2401

My question is this:

I have been looking for many hours, for a simple example of an NRF24L01 Arduino IDE code, of a transmitter and a receiver to send chars and ints,  but all i could find were tutorials, that go way too deep into the ocean, for my level of experience, dealing with hardcore C communication programming, with pointer calling and protocols and what not.

Does anyone know of a simple script and examples for the NRF24L01,  just as there is for the NRF2401? Just a little 'L' difference in the name and such a big  gap for the newbies?

Would be much obliged, for a good link with beginners level reference!
Thanks!

JoeN

#1
Feb 18, 2014, 07:56 am Last Edit: Feb 18, 2014, 08:02 am by JoeN Reason: 1
Turns out, I decided to do a project with this radio tonight.  They work great, so far.

This is the specific device I am working with:  http://www.ebay.com/itm/161193948590

The project is based on the tutorial information and source code here:  http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo

This is a great tutorial and leaves very little to be imagined.  I changed it to simply send a series of ones and zeros from my transmitter to my receiver as my first proof of concept and toggle an LED based on the incoming int.

Sender code (based on the tutorial and source code):

Code: [Select]
/*

RadioSend

- CONNECTIONS: nRF24L01 Modules See:

http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
  1 - GND
  2 - VCC 3.3V !!! NOT 5V
  3 - CE to Arduino pin 9
  4 - CSN to Arduino pin 10
  5 - SCK to Arduino pin 13
  6 - MOSI to Arduino pin 11
  7 - MISO to Arduino pin 12
  8 - UNUSED

Send a series of zeros and ones to the receiver radio.

*/

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

#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe

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

int high[1];
int low[1];

void setup()
{
 radio.begin();
 radio.openWritingPipe(pipe);

 high[0] = 1;
 low[0] = 0;
}

void loop()
{
 radio.write(high, sizeof(high));

 delay(1000);  
 
 radio.write(low, sizeof(low));

 delay(1000);  
}


Receiver code (based on the tutorial and source code):

Code: [Select]
/*

RadioReceive

- CONNECTIONS: nRF24L01 Modules See:

http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
  1 - GND
  2 - VCC 3.3V !!! NOT 5V
  3 - CE to Arduino pin 9
  4 - CSN to Arduino pin 10
  5 - SCK to Arduino pin 13
  6 - MOSI to Arduino pin 11
  7 - MISO to Arduino pin 12
  8 - UNUSED

Receive a series of zeros and ones to the sender radio.

*/

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

#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe

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

int received[1];

void setup()   /****** SETUP: RUNS ONCE ******/
{
 pinMode(8, OUTPUT);
 
 delay(1000);

 radio.begin();
 radio.openReadingPipe(1,pipe);
 radio.startListening();;
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
 if ( radio.available() )
 {
   // Read the data payload until we've received everything
   bool done = false;
   while (!done)
   {
     // Fetch the data payload
     done = radio.read( received, sizeof(received) );

     if (received[0]==0)
       digitalWrite(8, LOW);
     else
       digitalWrite(8, HIGH);
   }
 }
}


The devices (wired based on the pins selected in the tutorial.  Don't let the fact that these use bare chips fool you, they use the exact same Arduino "pins" that the tutorial suggests using):

Click for detail

I will never ask you to do anything that I wouldn't do myself.

microwhatt

Hi JoeN !
Would you be so kind and try to help me with my Project ?
Its using the same NRF24L01 Chip..

Any help would be apreciated ! :)

Kind regards
Frank

Robin2

I got my nRF24s working with this Tutorial

I suggest you use the TMRh20 version of the RF24 library - it solves some problems from the ManiacBug version

The pair of programs in this link may be useful.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

vermasachin

Thanks for putting this simple example. Was finally able to make it work.
Being new to Arduino world, may be I am doing something wrong but I dont see consistent results from the nrf24l chip. The output is not consistent ie. sometime the same code (which ran successfully last time) does not work again adding more frustation. I have used the base board to avoid voltage problem but still not able to understand why the out is not consistent.
Please advise on how to troubleshoot the root cause of the issue.

Thanks
Sachin

Steveiboy

Place a 10uF cap on the 3.3v power pin in the nrf24l01 break out board that normally cures missing bits of data issues

Robin2

Please advise on how to troubleshoot the root cause of the issue.
If the suggestion by @StevoiBoy does not solve the problem please post the pair of programs that YOU have uploaded to your Arduinos.

Also post some examples of the faulty output so we know exactly what you mean.

I have not found it necessary to use 10µF capacitors with my genuine Arduinos but they have been essential on my breadboard Atmega 328s.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

SamBrownADK

Just a quick question:  in the above source code JoeN posted, he has

"const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe"

Does this mean that both the sender and the recipent have a virtual private conversation?  In other words, other devices can hear what they transmit between each other but ignore it since they are not "0xE8E8F0F0E1LL"  Is my understanding correct?  Thank you.

Robin2

#8
Mar 07, 2017, 12:24 am Last Edit: Mar 07, 2017, 12:33 am by Robin2
Quote
"const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe"

Does this mean that both the sender and the recipent have a virtual private conversation?  In other words, other devices can hear what they transmit between each other but ignore it since they are not "0xE8E8F0F0E1LL"  Is my understanding correct?
First, it will be easier to understand if you think of that number as the address of the receiver. Pipes are a different thing altogether - they allow a receiver to accept messages for up to 6 different addresses and keep them segregated. Like receiving mail for the residents in an apartment block.

And, yes, your understanding is basically correct. An nRF24 listening on a particular channel will hear every transmission on that channel but will ignore anything that is not addressed to it.

This more recent Simple nRF24L01+ Tutorial may be helpful. I deliberately made it as simple as possible. And the examples do work.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

Does this mean that both the sender and the recipent have a virtual private conversation?  In other words, other devices can hear what they transmit between each other but ignore it since they are not "0xE8E8F0F0E1LL"
The conversation is by no means private.

Any node that listens to 0xE8E8F0F0E1LL can read the messages and will be undetectable if it disables autoack (global or for this specific address).
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

SamBrownADK

Thank you!  I will read up on it before I try to experiment with it.

Robin2

The conversation is by no means private.

Any node that listens to 0xE8E8F0F0E1LL can read the messages and will be undetectable if it disables autoack (global or for this specific address).
Very good point. I had overlooked that.

@SamBrownADK, if you choose your own addresses carefully it is unlikely that someone will be able to guess them as there are about 2555 options.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

#12
Mar 07, 2017, 11:34 am Last Edit: Mar 07, 2017, 12:08 pm by Whandall
if you choose your own addresses carefully it is unlikely that someone will be able to guess them as there are about 2555 options.
Well, you do not have to guess much.

You can program the NRF to ignore crc and use 2 byte addresses, then make the MSB of this address the preamble byte (there are only two of them and the selection is ruled by the highest bit of the first address byte).

So a brute force attack only has 256 possibilities left, six of them can be tested at once.

The captured packet allows access to the original 5 byte address used.

More here: http://travisgoodspeed.blogspot.de/2011/02/promiscuity-is-nrf24l01s-duty.html
or here: http://samy.pl/keysweeper/
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Robin2

Well, you do not have to guess much.
So why did they bother to provide for 5 byte addresses.

(By the way, that is meant as a comment, not a question).

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

Any wireless communication is prone to eavesdropping.

NRF's are more secure than standard WiFi, because there is no builtin promiscuous mode.

If you want to have secure communication, you will have to use encryption.
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

Go Up