Tuesday, February 19, 2013

Setup Nordic nRF24L01 RF modules to both Arduino UNO and Raspberry Pi


In my process of building a Wireless Sensor Network, I obtained  a few low cost RF module from Inhaos that are compatible with Nordic Semiconductor nRF24L01. My plans are to have these low cost RF modules replaces the wires for my sensor network on Arduino and connect them to Raspberry Pi as the IP gateway to the rest of the Internet.

Raspberry Pi with nRF24L01-PA-LNA with BT module and USB Wifi


This entry will show you on how to setup both Arduino UNO & Raspberry Pi to connect to these nRF24L01 modules.

Some basic information on these low cost RF modules, they communicate using SPI to the microcontroller and works on raw speed of 250Kbps, 1Mbps and 2Mbps.

I'm using the following hardware & software:-


Raspberry Pi Model B running Adafruit's Occidentalis V0.2 ( http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2 ) that support SPI.

Nordic Semiconductor Datasheet / Product Info ( http://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24L01P )

RF modules used here :-
------------------

nRF24L01+ PA+LNA+2dBi ant

nRF24L01+ with PA & LNA and 2 dBi antenna ( http://www.elecfreaks.com/wiki/index.php?title=2.4G_Wireless_nRF24L01p_with_PA_and_LNA ), these are long range RF modules acting as hub on the Raspberry Pi.

Inhaos RF2400P


Inhaos RF2400P (with 5dBm RF power output) on BEKEN Chipset ( http://www.inhaos.com/product_info.php?products_id=35 ). They are compatible with the nRF24L01 but with higher output power of 5dBm.

nRF24L01+


Too many to choose from, just google for the above keyword on eBay or choose the cheapest as they sells in bulk of 10 pieces for a very low cost...

*** The main differences between the regular nRF24L01+ modules and the Inhaos RF2400P modules are that the RF2400P have a higher output power of 5dBm but could not do 250Kbps speed.

I'm running my setup using 1Mbps speed.

Setting up Arduino 

Connect using jumper wires from the above pin to the nRF24L01+ modules
Upload the sketches/program to the Arduino, download it here from Dropbox. 


Arduino UNO with nRF24L01+/PA/LNA/2dBi ant


Arduino connections to the nRF24L01 modules :-

Arduino Pin 11 to RF Module Pin 6 ( MOSI)
Arduino Pin 12 to RF Module Pin 7 ( MISO )
Arduino Pin 13 to RF Module Pin 5 ( SCK )
Arduino Pin 7 to RF Module Pin 4 ( CSN )
Arduino Pin 8 to RF Module Pin 3 ( CE )
Arduino 3.3V to RF Module Pin 2 ( VCC / 3.3V ) *** RF Module can only take 3.3V 
Arduino GND to RF Module Pin 1 ( GND )
RF Module Pin 8 (IRQ) is not connected

*** All the other pins are 5V tolerant

Optional LCD Module to display the received payload & payload length :-

I'm using Arduino Pin 7 & 8 for the SPI because my LCD module is using the following pins :-

Arduino Pin 9 - LCD Pin 4
Arduino Pin 10 - LCD Pin 6
Arduino Pin 3 - LCD Pin 11
Arduino Pin 4 - LCD Pin 12
Arduino Pin 5 - LCD Pin 13
Arduino Pin 6 - LCD Pin 14

Arduino Pin 2 - connect to buzzer ( for sound feedback when it received a valid packet )


Setting up Raspberry Pi

Upgrade your Raspberry Pi firmware to Adafruit Occidentalis V0.2 that support hardware SPI.

Download the above firmware and flash your SD card according to the instruction on Adafruit/Raspberry Pi website.

Refer to the two Raspberry Pi GPIO pinout and the physical pinout.

Raspberry Pi GPIO
GPIO with pin functions


RPi GPIO9    (Pin 21)  to RF Module Pin 7 ( MISO )
RPi GPIO10  (Pin 19)  to RF Module Pin 6 ( MOSI )
RPi GPIO 11 (Pin 23)  to RF Module Pin 5 ( SCK )
RPi GPIO8    (Pin 24)  to RF Module Pin 3 ( CE )
RPi GPIO 25 (Pin 22)  to RF Module Pin 4 ( CSN )
RPI 3.3V       (Pin 17)  to RF Module Pin 2 ( VCC/3.3V )
RPi Gnd        (Pin 25)  to RF Module Pin 1 (GND)



Instructions on using github version for RPi :-

Using git, issue the following instructions on Raspberry Pi command prompt :-

$ git clone https://github.com/stanleyseow/RF24.git
$ cd RF24
$ cd librf24-rpi/librf24
$ make                                <--- Compile the files
$ sudo make install              <--- This will install the librf24.so.1.0 into the shared libraries
$ sudo ldconfig -v | grep librf
     librf24.so.1 -> librf24.so.1.0

$ cd ../examples/
$ make

To run the programs, type 

$ sudo ./rpi-hub 
or 
$ sudo ./scanner

*** You need sudo/root to access the spidev0.0 device

Instruction on Arduino :-

Got examples/RF24/rpi_hub_arduino ( make sure the pins matches the above Arduino pinout )

Compile and upload to the Arduino

Optional :- Connect a small buzzer to digital Pin2 to hear a beep when the packet is returned to sender


If everything is working, you should see a screenshot below :-

rpi-hub screenshot


Summary Links :-

- Raspberry Pi firmware :- Adafruit Occidentalis-v0.2

- RF24 library (Arduino & RPi libs) on Github https://github.com/stanleyseow/RF24

Datasheets :-
- Nordic nRF24L01+ Datasheet :- nRF24L01+ DS

Forum :-
- Raspberry Pi Forum on nRF24L01 Click here
- Arduino Forum on maniacbug nRf24L01 driver Click here


24 comments:

  1. fantastic article! Thanks.

    just to provide feedback: maybe it could be clearer that the RF24 library for the Arduino sketch is NOT from the tar file. I know it's simple but all the names are the same and it could be confusing at times.

    Once more, thank you

    ReplyDelete
  2. The 2nd links (Arduino sketch in zip) on the Summary Links is the actual file, the one included inside the tar files are from the previous author that I left it there....

    For future updates, I will separate out both the files...

    ReplyDelete
  3. so how do you change the pi code to make it read data not just ping?
    and how do you have it read data?

    ReplyDelete
    Replies
    1. Pls see the other examples.... what data do you want to read ?

      Delete
  4. Hi,

    I tried tar -zxvf nrf24-rpi.tar.gz

    and got the following error

    gzip: stdin: unexpected end of file
    tar: child returned status 1
    tar: Error is not recoverable: exiting now

    is there something I am doing wrong?

    ReplyDelete
  5. I hv tested the tar.gz file mentioned and it is working fine...Pls re-download it and try again.. In the future, I will migrate my code hosting to github so you will always get the latest copy of the codes...

    ReplyDelete
  6. Hi Stanley and congrats for the post!

    Can you help me on this:
    - I made all the wiring, double checked connections,etc.
    - I downloaded the Arduino libraries from the second link
    - I took a look at maniacbug programs. I could find that you have replaced the 9/10 pins to 8/9 pins.So now I have them right
    - what should be the baud rate? 57600 or 9600? I tried both with no result.
    - My arduino console lists:
    "Got payload size=127 value=ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ"
    - from the ssh connection to my Raspberry after running the rf24bb, I get: "Now sending length 8...Failed, response time out".

    Can you suggest me what should I do?

    ReplyDelete
  7. Hi, if you hv trouble, I would suggest that you make sure the nRF is working between TWO arduino before moving to Arduino & RPi...

    Those pins can be changed to any unused pins... does not matter... baud rate is usually 57600 but that does not matter too, just for output only...

    What was the output of the below ? Did u get values or just 00000

    STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
    RX_ADDR_P0-1 = 0xf0f0f0f0d2 0xf0f0f0f0e1
    RX_ADDR_P2-5 = 0xe2 0xe3 0xf1 0xf2
    TX_ADDR = 0xf0f0f0f0d2
    RX_PW_P0-6 = 0x20 0x20 0x20 0x20 0x20 0x20
    EN_AA = 0x3f
    EN_RXADDR = 0x3f
    RF_CH = 0x4c
    RF_SETUP = 0x07
    CONFIG = 0x0f
    DYNPD/FEATURE = 0x3f 0x04
    Data Rate = 1MBPS
    Model = nRF24L01+
    CRC Length = 16 bits
    PA Power = PA_MAX

    ReplyDelete
  8. Hi Stanley,

    Indeed I did got some values, not zeros, this is why it's strange. I get no errors until I execute the rf24bb script.

    I am wainting for a second arduino to be shipped next week. I hoped I could make it work only with an RPi and one Arduino.

    As for the nrf24l01+ modules, they should work as they are brand new, didn't used them before.

    ReplyDelete
  9. except the LCD, which doesnt matter for me, I have the same configuration

    ReplyDelete
  10. Could you post to the Raspberry Pi forum on my Summary Links, easier to reply and more people can help you out bcos there is a delay when I need to approve and publish the comments...

    ReplyDelete
  11. Hi, I have two nRF24L01+ connected to two different RPi's and I cannot get them to talk.

    The start of the output to rpi-hub is as follows:

    pi@raspberrypi ~/devel/RF24/librf24-rpi/librf24/examples $ sudo ./rpi-hub
    SPI device = /dev/spidev0.0
    SPI speed = 8000000
    CE GPIO = 24
    STATUS = 0xff RX_DR=1 TX_DS=1 MAX_RT=1 RX_P_NO=7 TX_FULL=1
    RX_ADDR_P0-1 = 0xffffffffff 0x0000000000
    RX_ADDR_P2-5 = 0x00 0x00 0x00 0x00
    TX_ADDR = 0xffffffffff
    RX_PW_P0-6 = 0x00 0x00 0x00 0x00 0x00 0x00
    EN_AA = 0xff
    EN_RXADDR = 0xff
    RF_CH = 0x00
    RF_SETUP = 0x00
    CONFIG = 0x00
    DYNPD/FEATURE = 0xff 0xff
    Data Rate = 1MBPS
    Model = nRF24L01
    CRC Length = Disabled
    PA Power = PA_MAX

    So I have lots of zeros... It does see the nRF24's however.

    When I run any of the example programs I am getting very strange results (like the receiving of replies even when the other device is switched off).

    I noticed that the radio initialization in the example CPP files says the final parameter is the CSN but the output when running the examples says that the specified value is pin number for CE. (I have tried using both the CE and CSN values for this last parameter but neither of them worked).

    I currently do not have an Arduino to play with but I do have two on order which I should be receiving within the next week os so (3.3v/5v selectable Arduino nano clones being shipping from China) so can try testing with the Arduino's when they arive.

    However, in the meantime I would like to try and solve the problem with the RPi's as ultimately one side of the RF link will be on a RPi.

    Any ideas as to where I begin to try and resolve my problem. I have checked the pin connections and they are all correct as per your post. Also, is there a particular reason why I am getting all zeros for the values in the initial output of the example programs?

    Thanks,

    Dave

    ReplyDelete
  12. One other thing I should mention... I am using Raspbian with the latest kernel/firmware via rpi-update. Apparently this now fully supports SPI but could this be the cause of my problems still?

    Thanks,

    Dave

    ReplyDelete
  13. Hi, I just setup my 2nd RPi and got them talking to each other using examples: rpi-hub and sendto_hub and working fine on both of them...

    From yr output, yr SPI is still NOT talking to the radios yet...

    If the MISO,MOSI and CLK is correct, make sure the following :-
    nRF CSN(4) connects to GPIO8
    nRF CE(3) connects to GPIO24 (for yr case, my case, I like to use GPIO25 that is just next to GPIO8 )

    Too much confusion on the CSN/CE pins....

    ReplyDelete
  14. Hi, MANY THANKS!!!

    After reversing the pins as you suggested (and after a reboot of the RPi's) it seems to be working fine.

    ping/pong tests are successful.

    rpi-hub/sendto-hub seems to work but there's some errors being reported on the sender side. See outputs below...

    rpi-hub Output:

    F24/examples/pingpair/
    ROLE: Ping out
    SPI device = /dev/spidev0.0
    SPI speed = 8000000
    CE GPIO = 25
    STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
    RX_ADDR_P0-1 = 0xf0f0f0f0e1 0xf0f0f0f0d2
    RX_ADDR_P2-5 = 0xe2 0xe3 0xe4 0xe5
    TX_ADDR = 0xf0f0f0f0e1
    RX_PW_P0-6 = 0x20 0x20 0x20 0x20 0x20 0x20
    EN_AA = 0x3f
    EN_RXADDR = 0x3f
    RF_CH = 0x4c
    RF_SETUP = 0x07
    CONFIG = 0x0f
    DYNPD/FEATURE = 0x00 0x04
    Data Rate = 1MBPS
    Model = nRF24L01+
    CRC Length = 16 bits
    PA Power = PA_MAX
    Now sending 3683426314...ok...Got response 3683426314, round-trip delay: 56
    Now sending 3683427373...ok...Got response 3683427373, round-trip delay: 50
    Now sending 3683428426...ok...Got response 3683428426, round-trip delay: 38
    Now sending 3683429467...ok...Got response 3683429467, round-trip delay: 51
    Now sending 3683430521...ok...Got response 3683430521, round-trip delay: 55
    Now sending 3683431578...ok...Got response 3683431578, round-trip delay: 57
    etc...

    ALL LOOKING GOOD SO FAR! :)

    sendto-hub Output:

    SPI device = /dev/spidev0.0
    SPI speed = 8000000
    CE GPIO = 25
    STATUS = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
    RX_ADDR_P0-1 = 0xf0f0f0f0e3 0x7365727631
    RX_ADDR_P2-5 = 0xe2 0xe3 0xe4 0xe5
    TX_ADDR = 0xf0f0f0f0e3
    RX_PW_P0-6 = 0x20 0x20 0x20 0x20 0x20 0x20
    EN_AA = 0x3f
    EN_RXADDR = 0x3e
    RF_CH = 0x4c
    RF_SETUP = 0x06
    CONFIG = 0x0f
    DYNPD/FEATURE = 0x3f 0x04
    Data Rate = 1MBPS
    Model = nRF24L01+
    CRC Length = 16 bits
    PA Power = PA_MAX

    Output below :
    outBuffer: E3,000,383,886,777 len: 18
    Send failed
    inBuffer: E3,000,383,886,777
    inBuffer --> rtt: 123
    outBuffer: E3,001,915,793,335 len: 18
    Send failed
    inBuffer: E3,001,915,793,335
    inBuffer --> rtt: 114
    outBuffer: E3,002,386,492,649 len: 18
    Send failed
    inBuffer: E3,002,386,492,649
    inBuffer --> rtt: 116
    outBuffer: E3,003,421,362,027 len: 18
    Send failed
    outBuffer: E3,004,690,059,763 len: 18
    Send failed
    inBuffer: E3,004,690,059,763
    inBuffer --> rtt: 116
    etc...

    Why does it say "Send failed" even though it appears to be working?

    Once again, many thanks for getting me this far!

    Dave

    ReplyDelete
  15. Send failed usually means the Auto-Ack was not replied...

    Yr rtt seems high, I am getting an average of 40-43 on RPi and on Arduino getting like 24ms...

    I got this example code to run hub/client on both RPi and Arduino UNO avoiding RX on Pipe0... any RPI/arduino combo will run...

    ReplyDelete
  16. Hmmm... I set my SPI speed to 16000000 which didn't work so I put it back down to 8000000 but it still no longer works!

    I have rebooted the RPi's but still no joy!

    Tell me I haven't fried my radios?!!? :)

    ReplyDelete
  17. Ahh it's alive again now... Left both RPi's off for 10 seconds and powered back up. Phew!

    RTT is now about 39-55... Not sure why it's dropped now but hey, it's looking good.

    Thanks again Stanley for your kind help.

    Dave

    ReplyDelete
  18. In the future, you shd post yr findings/issues to the forum at http://www.raspberrypi.org/phpBB3/viewtopic.php?f=45&t=17061&start=125 where there are a lot more users to reply ...

    ReplyDelete
  19. Hi Stanley,

    Could you help me!!!!

    I´m doing a project for comunication two arduinos using transiver (NRF24L01 library RF24) for monitor temperature I made test and all was ok, but when I put the Arduino ethernet shield board and made the conection of the antena NRF24L01 in ethernet bord My project did not work anymore, can be the antenna connection on the board is different from ethernet connection I used the arduino uno
    Tks by help

    ReplyDelete
  20. Pls search all the message or post yr question here... http://arduino.cc/forum/index.php/topic,62222.135.html

    ReplyDelete
  21. When I try to run the example file, I get this message:
    ~/test/RF24/librf24-rpi/librf24/examples $ sudo ./rpi-hub
    can't open device: No such file or directory

    What is the problem?

    Thanks, this is the best site ever ! :)

    ReplyDelete
  22. Make sure yr RPi hv drivers for the SPI support..

    I'm using Raspberry Pi Model B running Adafruit's Occidentalis V0.2 ( http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2 ) that support SPI.

    See this link for Raspbian :- http://www.brianhensley.net/2012/07/getting-spi-working-on-raspberry-pi.html

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...