Getting Started¶
This is a Circuitpython driver library for the nRF24L01(+) transceiver.
Originally this code was a Micropython module written by Damien P. George & Peter Hinch which can still be found here
The Micropython source has since been rewritten to expose all the nRF24L01’s features and for Circuitpython compatible devices (including linux-based SoC computers like the Raspberry Pi). Modified by Brendan Doherty & Rhys Thomas.
- Authors: Damien P. George, Peter Hinch, Rhys Thomas, Brendan Doherty
Features currently supported¶
- Change the address’s length (can be 3 to 5 bytes long)
- Dynamically sized payloads (max 32 bytes each) or statically sized payloads
- Automatic responding acknowledgment (ACK) packets for verifying transmission success
- Append custom payloadsto the acknowledgment (ACK) packets for instant bi-directional communication
- Mark a single payload for no acknowledgment (ACK) from the receiving nRF24L01 (see
ask_no_ack
parameter forsend()
andwrite()
functions) - Invoke the “re-use the same payload” feature (for manually re-transmitting failed transmissions that remain in the TX FIFO buffer)
- Multiple payload transmissions with one function call (see documentation on the
send()
function and try out the Stream example) - Context manager compatible for easily switching between different radio configurations
using
The with statement
blocks (not available inrf24_lite.py
version) - Configure the interrupt (IRQ) pin to trigger (active low) on received, sent, and/or
failed transmissions (these 3 events control 1 IRQ pin). There’s also virtual
representations of these interrupt events available (see
irq_dr
,irq_ds
, &irq_df
attributes) - Invoke sleep mode (AKA power down mode) for ultra-low current consumption
- cyclic redundancy checking (CRC) up to 2 bytes long
- Adjust the nRF24L01’s builtin automatic re-transmit feature’s parameters (
arc
: number of attempts,ard
: delay between attempts) - Adjust the nRF24L01’s frequency channel (2.4-2.525 GHz)
- Adjust the nRF24L01’s power amplifier level (0, -6, -12, or -18 dBm)
- Adjust the nRF24L01’s RF data rate (250kbps, 1Mbps, or 2Mbps)
- An nRF24L01 driven by this library can communicate with a nRF24L01 on an Arduino driven by the TMRh20 RF24 library. See the nrf24l01_2arduino_handling_data.py example.
- fake BLE module for sending BLE beacon advertisments from the nRF24L01 as outlined by Dmitry Grinberg in his write-up (including C source code).
- MulticeiverTM mode (up to 6 TX nRF24L01 “talking” to 1 RX nRF24L01 simultaneously). See the Multiceiver Example
Dependencies¶
This driver depends on:
- Adafruit CircuitPython
- Bus Device (specifically the
spi_device
)
Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.
Note
This library supports Python 3.4 or newer, but Python 3.7 introduced
the function time.monotonic_ns() which returns an arbitrary time “counter”
as an int
of nanoseconds. However, this function is not used in the
example scripts for backward compatibility reasons. Instead, we used
monotonic()
which returns an arbitrary time “counter” as
a float
of seconds. CircuitPython firmware supports both functions as of
v4.0.
Installing from PyPI¶
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:
pip3 install circuitpython-nrf24l01
To install system-wide (this may be required in some cases):
sudo pip3 install circuitpython-nrf24l01
To install in a virtual environment in your current project:
mkdir project-name && cd project-name
python3 -m venv .env
source .env/bin/activate
pip3 install circuitpython-nrf24l01
Pinout¶
The nRF24L01 is controlled through SPI so there are 3 pins (SCK, MOSI, & MISO) that can only be connected to their counterparts on the MCU (microcontroller unit). The other 2 essential pins (CE & CSN) can be connected to any digital output pins. Lastly, the only optional pin on the nRf24L01 GPIOs is the IRQ (interrupt; a digital output that’s active when low) pin and is only connected to the MCU via a digital input pin during the interrupt example. The following pinout is used in the example codes of this library’s example directory.
nRF2401 | Raspberry Pi | ItsyBitsy M4 |
---|---|---|
GND | GND | GND |
VCC | 3V | 3.3V |
CE | GPIO4 | D4 |
CSN | GPIO5 | D5 |
SCK | GPIO11 (SCK) | SCK |
MOSI | GPIO10 (MOSI) | MOSI |
MISO | GPIO9 (MISO) | MISO |
IRQ | GPIO12 | D12 |
Tip
User reports and personal experiences have improved results if there is a capacitor of 100 mirofarads [+ another optional 0.1 microfarads capacitor for added stability] connected in parrallel to the VCC and GND pins.
Using The Examples¶
See examples for testing certain features of this the library. The examples were developed and tested on both Raspberry Pi and ItsyBitsy M4. Pins have been hard coded in the examples for the corresponding device, so please adjust these accordingly to your circuitpython device if necessary.
To run the simple example, navigate to this repository’s “examples” folder in the terminal. If you’re working with a CircuitPython device (not a Raspberry Pi), copy the file named “nrf24l01_simple_test.py” from this repository’s “examples” folder to the root directory of your CircuitPython device’s CIRCUITPY drive. Now you’re ready to open a python REPR and run the following commands:
>>> from nrf24l01_simple_test import *
nRF24L01 Simple test.
Run slave() on receiver
Run master() on transmitter
>>> master()
Sending: 5 as struct: b'\x05\x00\x00\x00'
send() successful
Transmission took 36.0 ms
Sending: 4 as struct: b'\x04\x00\x00\x00'
send() successful
Transmission took 28.0 ms
Sending: 3 as struct: b'\x03\x00\x00\x00'
send() successful
Transmission took 24.0 ms
What to purchase¶
See the store links on the sidebar or just google “nRF24L01+”. It is worth noting that you generally want to buy more than 1 as you need 2 for testing – 1 to send & 1 to receive and vise versa. This library has been tested on a cheaply bought 6 pack from Amazon.com, but don’t take Amazon or eBay for granted! There are other wireless transceivers that are NOT compatible with this library. For instance, the esp8266-01 (also sold in packs) is NOT compatible with this library, but looks very similar to the nRF24L01+ and could lead to an accidental purchase.
Power Stability¶
If you’re not using a dedicated 3V regulator to supply power to the nRF24L01, then adding capcitor(s) (100 µF + an optional 0.1µF) in parrellel (& as close as possible) to the VCC and GND pins is highly recommended. Stablizing the power input provides significant performance increases. More finite details about the nRF24L01 are available from the datasheet (referenced here in the documentation as the nRF24L01+ Specification Sheet)
About the nRF24L01+PA+LNA modules¶
You may find variants of the nRF24L01 transceiver that are marketed as “nRF24L01+PA+LNA”. These modules are distinct in the fact that they come with a detachable (SMA-type) antenna. They employ seperate RFX24C01 IC with the antenna for enhanced Power Amplification (PA) and Low Noise Amplification (LNA) features. While they boast greater range with the same functionality, they are subject to a couple lesser known (and lesser advertised) drawbacks:
Stronger power source. Below is a chart of advertised current requirements that many MCU boards’ 3V regulators may not be able to provide (after supplying power to internal components).
Specification Value Emission mode current(peak) 115 mA Receive Mode current(peak) 45 mA Power-down mode current 4.2 µA Needs shielding from electromagnetic interference. Shielding usually works best when it has a path to ground (GND pin), but this connection to the GND pin is not required.
See also the Testing nRF24L01+PA+LNA module
nRF24L01(+) clones and counterfeits¶
This library does not directly support clones/counterfeits as there is no way for the library to differentiate between an actual nRF24L01+ and a clone/counterfeit. To determine if your purchase is a counterfeit, please contact the retailer you purchased from (also reading this article and its links might help). The most notable clone is the Si24R1. I could not find the Si24R1 datasheet in english. Troubleshooting the SI24R1 may require replacing the onboard antenna with a wire. Furthermore, the Si24R1 has different power amplifier options as noted in the RF_PWR section (bits 0 through 2) of the RF_SETUP register (address 0x06) of the datasheet. While the options’ values differ from those identified by this library’s API, the underlying commands to configure those options are almost identical to the nRF24L01. Other known clones include the bk242x (also known as RFM7x).
Contributing¶
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming. To contribute, all you need to do is fork this repository, develop your idea(s) and submit a pull request when stable. To initiate a discussion of idea(s), you need only open an issue on the aforementioned repository (doesn’t have to be a bug report).
Future Project Ideas/Additions¶
The following are only ideas; they are not currently supported by this circuitpython library.
- There’s a few blog posts by Nerd Ralph demonstrating how to use the nRF24L01 via 2 or 3 pins (uses custom bitbanging SPI functions and an external circuit involving a resistor and a capacitor)
- network linking layer, maybe something like TMRh20’s RF24Network
- implement the Gazelle-based protocol used by the BBC micro-bit (makecode.com’s radio blocks).
Sphinx documentation¶
Sphinx is used to build the documentation based on rST files and comments in the code. First, install dependencies (feel free to reuse the virtual environment from above):
python3 -m venv .env
source .env/bin/activate
pip install Sphinx sphinx-rtd-theme
Now, once you have the virtual environment activated:
cd docs
sphinx-build -E -W -b html . _build
This will output the documentation to docs/_build
. Open the index.html in your browser to
view them. It will also (due to -W) error out on any warning like the Github action, Build CI,
does. This is a good way to locally verify it will pass.