Friday, 10 July 2020

Nordic nRF24L01P on Raspberry Pi

Raspberry Pi nRF24L01P



This chip has been around for ages. There's tons of information for the Arduino to get this working and there's a few modified libraries from the original RF24 library.
Brennen Ball put together a good introduction to the chip over 10 years ago and can still be downloaded at http://www.diyembedded.com/tutorials/nrf24l01_0/nrf24l01_tutorial_0.pdf

For the Raspberry Pi the libraries I tried were a bit out of date and were a bit chip specific. The TMRh20 driver uses the old BCM2835 calls and fails to compile. 
In the end I decided to just write this from the specification and can be found on my Github at https://github.com/AidanHolmes/NordicRF24.

The library builds some useful tools to help test radio devices on a Raspberry Pi using the WiringPi library (up to Pi4) at the moment.

The library is also compatible with Arduino devices when built with the hardware interface for the Arduino API.

Prerequisites


Raspberry Pi

SPIDEV is needed on the Raspberry Pi environment. Installed as standard and enabled through raspi-config.

WiringPi library is required with a call to apt-get (note that WiringPi is no longer supported after Pi 4 so will need alternative libraries for future Pi hardware)

2 of my libraries are also required for the hardware and graphics support (yes there's no graphics for a radio, but the build is work-in-progress to improve)

> git clone https://github.com/AidanHolmes/graphicslib ~/graphicslib
> git clone https://github.com/AidanHolmes/PiHardware ~/hardware

Put these into a sibling directory to the main radio code for RF24

Finally run

> git clone https://github.com/AidanHolmes/NordicRF24 ~/NordicRF24

This should create the actual driver code (along with some work-in-progress MQTT libraries)

if you've run this from your home directory you should have something like

/home/pi/graphicslib
/home/pi/hardware
/home/pi/NordicRF24

Arduino

Building on the Arduino doesn't require any specific libraries. Everything is available within the git repositories for the Raspberry Pi. The Arduino API provides everything else.

As above, extract the libraries using git clone to a build folder (or home directory as provided below)
git clone https://github.com/AidanHolmes/graphicslib ~/graphicslib
git clone https://github.com/AidanHolmes/PiHardware ~/hardware
git clone https://github.com/AidanHolmes/NordicRF24 ~/NordicRF24

Pi hardware repository is a bit of a misnomer as more support is now included for Arduino. Note that it needs extracting to a directory called 'hardware' to keep it generic. 

I personally build on my MS Windows machine and to facilitate this there is a basic .bat file to extract all the relevant files into an 'arduino' project folder.
Within the NordicRF24 directory, change to the 'arduino' folder.
> build.bat
If you are on Linux then run
> make source

Building

Raspberry Pi

> cd ~/NordicRF24
> make all

All dependencies should be built and you will have the following binaries
  • rf24cmd - reset radios and print status
  • rf24ping - simple listener or sender application for a radio
  • rf24send - use the buffered library to send text from sender to listener
  • rf24drvtest - cmd line send and receive tool
The tools are useful to check the hardware is running as expected and will provide outputs of hardware information.

Arduino

After running the build.bat then the .ino project file can be loaded into the Arduino IDE. Build this using the required settings for your hardware. This has been built with the Arduino Nano, Bluepill and Teensy LC boards.
The arduino.ino project is a version of the rf24drvtest and will communicate with a Raspberry Pi using that binary. 

Driver Test


The driver test executable can be built on the Pi or on Arduino. They are compatible to communicate with each other. Simple text is exchanged and demonstrates the use of the RF24PacketDriver interface that can be reused on any project. 
All projects listen on address C0C0C0C0C0 as a broadcast address.

On a Raspberry Pi run
> sudo ./rf24drvtest -c 27 -i 17 -s 1 -i 76 -a A1A1A1A1A1
Note that the -c should match the pin used for CE and -i is the IRQ pin.

On the Arduino the code should start and output to serial. The default code will use address A0A0A0A0A0 and run at 1MB/s on channel 76.

To communicate with another device, enter the address, add a space, then enter text up to 27 characters in length. Press return to send (note that it's a Newline that triggers the send).
Text should appear on the receiving device.

Troubleshooting

RF24 devices are cheap and fickle. There are many theories as to why they may or may not work. Firstly there are dozens of China clones and it's rare you have a 100% genuine device. That said, I've had epoxy coated chips (no markings) work better than labelled devices. The follow guidance should help:
  • Ensure 3.3v is used to power the RF24 devices
  • Capacitors may be required to stabilise the 5v - use YL-105 adapter boards for an all-in-one regulator and capacitor solution
  • Avoid USB power - Arduino Nano running even with YL-105 is unstable unless running using a 5v external power supply or 4.5v battery supply. This could be a grounding issue
  • Ensure MISO and MOSI are wired up correctly
  • PL+LNA+SMA versions are more fickle than the basic versions. They will draw more power and the basic regulator on Arduino devices will struggle to power the chips

Unique to this library

You can control everything with this library. Pipes can be fully configured whereas the RF24 driver implemented things like a set address length for all pipes.

All the class calls are documented on Github at https://github.com/AidanHolmes/NordicRF24/blob/master/NordicRF24.md

The library supports the IRQ pin interrupts and calls a thread or interrupt within the NordicRF24 class to receive data.

The driver abstracts the underlying GPIO, timer and SPI capability of hardware into plug-in drivers (build time, not runtime).