A while ago I started playing with a couple of nRF24L01 modules. I found 2 Arduino libraries, the Mirf library which is available on the Arduino playground and Maniacbugs RF24 library. I spent quite some time getting these things to work without much success. Eventually I got them to work with the Mirf library. I later made several sketches to control my Rover 5 and kept using the Mirf library. Today I made another attempt to get my nRF24L01 modules to work with the RF24 library and this time I succeeded. I made 2 sketches, one for the receiver and one for the transmitter. The transmitter sketch transmits 2 analog values from a joystick to the receiver. These are 2 very basic sketches using the RF24 library. They might be helpful for people like me who haven’t much experience with programming. I found the example sketches that come with the RF24 library quite difficult to understand.
Transmitter sketch:
/* http://www.bajdi.com This sketch transmits 2 analog values through an nRF24L01 wireless module. */ #include <SPI.h> #include "nRF24L01.h" #include "RF24.h" int joystick[2]; RF24 radio(8,7); const uint64_t pipe = 0xE8E8F0F0E1LL; void setup(void) { Serial.begin(9600); radio.begin(); radio.openWritingPipe(pipe); } void loop(void) { joystick[0] = analogRead(A0); joystick[1] = analogRead(A1); radio.write( joystick, sizeof(joystick) ); }
This is the receiver sketch:
/* http://www.bajdi.com This sketch receives 2 analog values through an nRF24L01 wireless module. */ #include <SPI.h> #include "nRF24L01.h" #include "RF24.h" int joystick[2]; RF24 radio(48,49); const uint64_t pipe = 0xE8E8F0F0E1LL; void setup(void) { Serial.begin(9600); radio.begin(); radio.openReadingPipe(1,pipe); radio.startListening(); } void loop(void) { if ( radio.available() ) { // Dump the payloads until we've gotten everything bool done = false; while (!done) { // Fetch the payload, and see if this was the last one. done = radio.read( joystick, sizeof(joystick) ); Serial.println(joystick[0]); Serial.println(joystick[1]); } } else { Serial.println("No radio available"); } }
Hi, I’ve gone down a similar route to you – I could get the NRFs working with Mirf and ManiacBugs RF24 library, but I didn’t understand the whole “role” thing, so I shelved a project, but seeing your two sketches makes me think it’s time I dusted off those radios and rewrite my sketches!
Many thanks for making sense of RF24, this is a great starting point for me!
Dude, thank you so much for this example. Now things are starting to make sense.
Hello,
I am having trouble getting the analog readings on the receiver end.
I am using UNO for TX, and NANO328 for RX.
the only change i made in the code was:
RF24 radio(10,9); on both TX and RX (which from my understanding they represent the CE/CS pins.)
on the TX end i added:
Serial.println(joystick[0]); // to verify TX is reading the A0
Serial.println(joystick[1]); // to verify TX is reading the A1
Results:
on the RX end serial monitor all i am getting is.
-1
-1
-1
-1
-1
…
any help is greatly appreciated
note that i also got the same results with your other example sketch using the Mirf library.
I had the same problem.
If u use NANO as o TX and UNO as RX it should by work fine (works for me)
If both sketches don’t work you probably have not connected the modules properly.
BTW: the 3V3 on a Nano comes from the FTDI chip, it only gives 3V3 when USB is connected. I’m not sure if the 3V3 line from the FTDI chip is strong enough for the nRF24L01 modules.
Thank you for your response. I also thought the nano might have been the problem. i will rewire it from scratch this week and use both uno for both TX/RX. and see how that works.
I was wondering if can can possibly post the pin-out for the nrf24L01 module that match this sketch for the RF24 to avoid confusion on my part. I does not have to be a diagram text will do just fine.
Thank you.
I always look at this page to see the pin out: http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
Thank you. very much.
Works Great When i used UNO for both TX/RX, for the record with the NANO and after playing with it for a while i got it to transmit but never had success receiving using the nano even with PaLevel set low on the transceiver.
So it might be the 3V3 issue like you said but i really find it weird that the module uses more power to receive than to transmit~~~.
Can you explain what the line
const uint64_t pipe = 0xE8E8F0F0E1LL;
is there for?
Cheers
Good stuff here on the Nrf24L01+ .
From what I have understood about
“const uint64_t pipe = 0xE8E8F0F0E1LL;”
Simply put : It tells the 2 radio’s to stay tuned to the same frequency.
Hmm, that’s a sort of an address the chip needs to work. It’s all explained in the datasheet of the nRF24L01 if you want to know the details.
Great thanks!
Hi.
I got stupid question: what’s mean this number “2” in this line int joystick[2]; ?
P.S. sry 4 my english
An array with 2 variables, take a look here: http://arduino.cc/en/Reference/Array
THX.
Do you have any idea how to modify a RX sketch to obtain signal PPM like in RC controller?
I try to create RC system to controll quadrocopter (if its posible)
RC systems use a different 2.4GHz technology, they are not compatible with the nRF24L01 modules.
OK.
I don’t wont use a standard RC system (transmitter and receiver).
i wont create RX and TX on two arduino with nRF24 so that the signal was compatible with PPM.
I must connect PPM signal to MultiWii board to controll quad.
http://www.magnumhobbies.com/catalog/MH10312%20Multiwii%2032U4se2.1qX.pdf
on page 3 is instruction how to connect receiver to board.
I think the PPM is the best solution but i don’t know how to do it.
It is possibly?
Why shouldn’t it be possible? I think there is an Arduino library that does what you want. Take a look here: http://rcarduino.blogspot.com
But I would not recommend using the nRF24L01 modules to control a quadcopter. I don’t think they are made to work in an environment with a lot of electromagnetic noise. The 4 motors will probably cause a lot of interference.
The RF24 library, have mutiples errors!!!!
1)GettingStarted.cpp:21:18: fatal error: RF24.h no file
Looks like you haven’t installed the library, or put it in the wrong folder. http://arduino.cc/en/Guide/Libraries
Rafic
“with the NANO and after playing with it for a while i got it to transmit but never had success receiving using the nano ”
Having 2 nano setups side by side on 2 computers, I have to type something in the Serial Monitor on the receive one before it will receive. It appears after reset, something isn’t set up correctly until you send it a character or two on the serial. Haven’t checked yet if it’s the serial chip on the nano, or code side not working until a char comes in.
Nice work and thanx for making these modules code so simple.I am trying to send four values instead of two but at the receiver end, i received strange values.I also change the joystick size from 2 to 4 but problem not solved.Will u guide me?
Can u explain me
const uint64_t pipe = 0xE8E8F0F0E1LL;
How u determine this address.
Waiting for your kind reply.
Post your code on the Arduino forums. Much easier place to help you 🙂 I just used the pipe address from the examples that come with the library. You’ll need to read the datasheet to understand how the addressing works.
I spent 2 days working with 2 NRF (the little ones) to get them running on an UNO and MEGA with RF24-Lib.
Everythings seems fine so far – all tranceivers are correctly recognized …
… MEGA seems to sent something, but always with errors.
… UNO received supposal always correct and transmitt back ,but MEGA doesnt receve anything.
I tried a lot of things (more or less output power, change several channels – might be WLAN ist disurbing)
… but nothing comes to an satisfying result.
An hour ago i read about “… put a little capacitor of 100nF between Vcc and GND at the tranceivers board on MEGA’s side.”
You won’t beleave it: This little thing causes a little (no – a great !) wonder !
Now Mega works fine with UNO.
[…] nRF24L01 2.4GHz Radio/Wireless Transceivers How-To RF24 library revisited […]
Thanks for these sketches! the RF24 examples were driving me nuts – I dropped these in, modified to suit what I wanted to do and my project started working like a champ!
That’s nice to hear, good luck with your project 🙂
Hello Mr Bajdi
Thanks a million for these two simple sketches. I too found RF24’s example very very confusing.
Mr Stanis I too am trying the same idea of building the remote controller system using NRF24L01s. I think it will work just fine and theoretically it should work. I am building the source code from scratch to understand PID and other features. I tried to do the experiment with NRF905s but they didn’t work. Now after reading this blog I think the main culprit was mega’s 3V3 power supply. I will try by adding a 100nf capacitor or by building a separate power supply for nrf24l01. Who know maybe nrf905s will work out just fine
Once again Mr Bajdi thanks for the help.
Hi, thx for your sketches. What can I do to control more leds?
Sorry, I havent seen your project on web page. I have seen your project (led control) on arduino forum.
Hi,
I am also trying to interface the nrf module with nano, and i tried everything (including bypass capacitor) but it does not work. I also tried with external 3.3v supply still it does not work.
i m using rf24 library with 9 as ce and 10 as csn. the modules work fine with uno.
i m using the getting started example provided by rf24 library. do i need to alter the program to run with nano?
No it should work the same with Nano and Uno, as they use the same micro controller (ATmega328). With a Nano you must definitely use an external 3.3V power supply. The 3.3V of the Nano comes from the FTDI chip and can’t supply enough power to the nRF24L01 module.
when i compile the recieve sketch apprear an error like this:
a.ino: In function ‘void loop()’:
a.ino:31:13: error: void value not ignored as it ought to be
please help me what is my problem
Just in case anybody else stumbles upon this… You get this error about ‘void value’ because ‘read’ was changed to return nothing (before that it returned success value).
Just change
if ( radio.available() )
{
// Dump the payloads until we’ve gotten everything
bool done = false;
while (!done)
{
// Fetch the payload, and see if this was the last one.
done = radio.read( joystick, sizeof(joystick) );
Serial.println(joystick[0]);
Serial.println(joystick[1]);
}
}
to something like
while ( radio.available() )
{
// Dump the payloads until we’ve gotten everything
// Fetch the payload, and see if this was the last one.
radio.read( joystick, sizeof(joystick) );
Serial.println(joystick[0]);
Serial.println(joystick[1]);
}
hi
im use a code
error line done = radio.read( joystick, sizeof(joystick) ); error
“void value not ignored as it ought to be”
The answer is right above your question.