I recently bought 4 Nrf24L01 modules, 2 small modules with internal antenna and 2 bigger ones with a preamp, amp and external antenna. You can find the small modules for less then 5$ a piece on Ebay. It’s the cheapest way make a wireless connection between 2 Arduino’s. These modules are based one the Nordic Semiconductor nRF24L01+ chip and use the SPI bus and 2 extra pins, you can find more information and how to connect them here. I found 2 libraries for these chips, the Mirf library on the Arduino playground and Maniacbugs RF24 library. I had some trouble getting these things to work, I searched the Arduino forum and it seems that I wasn’t the only one having issues with these modules and an Arduino Mega 2560. You can read about it here. After a lot of head scratching I got all 4 of the modules to work. I then tried to make some own sketches using the RF24 library and failed miserably. I have lot to learn when it comes to coding 🙁
I then tried out the Mirf library and had a bit more success. I managed to write 2 sketches (transmitter and receiver) that sent the analog value from one Arduino to the other. I think this is the minimum code you need to get them to work. Well it’s a start… I hope to use these modules to remote control a “robot”.
Here are the sketches:
The following sketch goes in to the transmitter. (I used my Arduino Mega 2560 with a potentiometer connected to analog pin A0)
// http://www.bajdi.com // Nrf24L01 connected to Mega 2560 // Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo // Transmit analog value from pin A0 to the receiver #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> int rate; void setup(){ Serial.begin(9600); Mirf.cePin = 48; //ce pin on Mega 2560, REMOVE THIS LINE IF YOU ARE USING AN UNO Mirf.csnPin = 49; //csn pin on Mega 2560, REMOVE THIS LINE IF YOU ARE USING AN UNO Mirf.spi = &MirfHardwareSpi; Mirf.init(); Mirf.setRADDR((byte *)"clie1"); Mirf.payload = sizeof(rate); Mirf.config(); } void loop(){ rate = analogRead(A0); Mirf.setTADDR((byte *)"serv1"); Mirf.send((byte *) &rate); while(Mirf.isSending()){ } }
The following sketch goes in to the receiver Arduino. (from this Arduino we can read the analog value through the serial monitor)
// http://www.bajdi.com // Nrf24L01 connected to Arduino Uno // Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo // Receives analog value from transmitter and display it on serial monitor #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> int rate; void setup(){ Serial.begin(9600); Mirf.spi = &MirfHardwareSpi; Mirf.init(); Mirf.setRADDR((byte *)"serv1"); Mirf.payload = sizeof(rate); Mirf.config(); } void loop(){ while(!Mirf.dataReady()){ } Mirf.getData((byte *) &rate); Serial.println(rate); delay(100); }
With some small changes to the above sketch we can use the value read from the potentiometer (rate) and map it to a value between 0 and 255 to dim a led (analogwrite function).
// http://www.bajdi.com // Nrf24L01 connected to Arduino Uno // Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo // Receives analog value from transmitter and maps it to a PWM range (0-255) to dim a led #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> int rate; int ledValue; const int led = 3; void setup(){ Serial.begin(9600); pinMode(led, OUTPUT); Mirf.spi = &MirfHardwareSpi; Mirf.init(); Mirf.setRADDR((byte *)"serv1"); Mirf.payload = sizeof(rate); Mirf.config(); } void loop(){ while(!Mirf.dataReady()){ } Mirf.getData((byte *) &rate); ledValue = (map( rate, 0, 1024, 0, 255 ) ); analogWrite(led, ledValue); Serial.println(ledValue); delay(10); }
[…] wireless radio modules 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 […]
I have two Arduino Mega and I am new in this field so let me consult with you following questions
(1) do i need to edit or revise any one of the *.h files?
#include #include #include #include
(2) for transmitter, are you connecting the (CE) to 48, (CSN) to 49, (MISO)to 50, (MOSI) to 51, (SCK) to 52
thanks
You need to install the appropriate libraries. And CE and CSN are pin 48 and 49 in the sketch that goes into the Mega. You can change it if you wish to do so.
my LED is not working properly and I am still still scratching my head … Then I saw this link
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1245058973
At the bottom of link, it says something that I have to put below definition somewhere, but i am not very clear what is it refering? can you advise? do i need to edit it?
#if defined(__AVR_ATmega128__) || defined(__AVR_ATmega1280__)
#define SCK_PIN 52
#define MISO_PIN 50
#define MOSI_PIN 51
#define SS_PIN 53
#endif
The pin defining thing in that link is only valid if you are using an old version of the IDE. Have you tried the ping example that comes with the Mirf library? BTW, you should also read this: http://arduino.cc/forum/index.php/topic,82906.0.html
I changed back to use the Maniacbugs RF24 library, and try the “pingpair” program, and both Mega board and Uno board are working without problems. However, when I add anything LED, either the LED_remote code by Maniacbug or this link’s potentiometer LED, then things become very unstable. As you link http://arduino.cc/forum/index.php/topic,82906.0.html point out, I feel the power supplier for nRF24L01+ may need to be carefully concerned. This is the work of my next step …
Hello Bajdi,
for the receiver codes, two lines are missing, which are
Mirf.cePin = 48; //ce pin on Mega 2560
Mirf.csnPin = 49; //csn pin on Mega 2560
For my case, if I miss these two lines, the code won’t work..
Thanks
Yes, if you are using a mega you need those 2 lines. I was using a mega and an Uno so didn’t need those 2 lines for the Uno sketch 🙂
hey, I spent a lot of time to get working these modules, but my IDE cannot compile the sketch:
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp: In member function ‘void Nrf24l::init()’:
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:85: error: ‘OUTPUT’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:85: error: ‘pinMode’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp: In member function ‘void Nrf24l::ceHi()’:
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:285: error: ‘HIGH’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:285: error: ‘digitalWrite’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp: In member function ‘void Nrf24l::ceLow()’:
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:289: error: ‘LOW’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:289: error: ‘digitalWrite’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp: In member function ‘void Nrf24l::csnHi()’:
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:293: error: ‘HIGH’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:293: error: ‘digitalWrite’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp: In member function ‘void Nrf24l::csnLow()’:
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:297: error: ‘LOW’ was not declared in this scope
…\arduino-1.0.1\libraries\Mirf\Mirf.cpp:297: error: ‘digitalWrite’ was not declared in this scope
Could you help me somehow?
Thanks
The Mirf library on the Arduino playground has not been updated to Arduino 1.0 or later. Have a look on the Arduino forums, there is a topic about the Mirf library and how to update it so it works in Arduino 1.01.
[…] code used in this post is slightly changed version of the code from http://www.bajdi.com/playing-with-nrf24l01-modules/ Like this:LikeBe the first to like this. This entry was posted in Tutorials and tagged Ardunio, […]
What is the data transmission range for the modules with the internal antenna
Approximately 25 to 30 meters with line of sight.
Hi. Is it posible to have several radios?
exampel, 4 sensors sending data to one resiever?
-Andy
It is possible to let thousands of radios talk to each other using the RF24Network library. http://maniacbug.github.com/RF24Network/
Tanks admin 🙂
I can’t really see how the code with the potentiometer will work. you have not connected the potentiometer to a analog pin?
Wait know I can see. It’s the receiving code you have made, but where is the sending code?
Damn I must be so stupid. I just saw the first code is the transmitting code. LOL
[…] http://www.bajdi.com/playing-with-nrf24l01-modules/ […]
It works perfectly. Now finally I can do other sketches.
Thanks a lot.
i follow the codes for the transmitter and receiver to send the value of A0 ( which is my LM35 temperature sensor) but all i get is 0 and -1 …
How we connect the nrf24l01 with arduino nano? pin out…. etc?
An Arduino Nano uses the same micro controller as an Uno, so it uses the same pins. http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
With this device, I can communicate UNOS 3 ?? Servo 1 and 2 Slaves?
Yes, no problem. Have a look at the RF24network library: https://github.com/maniacbug/RF24Network
Hi,
I have successfully been able to get 2 units to talk to each other and once I soldered it all into a mini board it works very well. One thing I don’t fully understand is the Mirf.setRADDR & Mirf.setTADDR. I am trying to send to multiple units from a single server for example an acknowledgement bit.
Do I have to to set all the setRADDR to different whilst keeping the TADDR the same, I just cant work it out. Any help would be much appreciated.
Here is a nasty sketch of my network;
(SERVER) (Node 1)
(Node 2)
(Node 3)
(Node 4)
How does the server send info to all 4 separate nodes or is there a way to blast the info to all devices at once?
Thanks
Chambo
I stopped using the mirf library a long time ago. I use a fork of the RF24network library: http://tmrh20.github.io/RF24Network/index.html
hi im tring to play with two chanels how to do such thing?
sorry for my bad english..
yaniv :]