I started looking at the nRF24L01 chips while planning a controller for the sprinkler in my back yard. They seemed like a pretty good option for low-cost, low-power wireless Arduino communication. They’re not perfect, but when you can buy a 10-pack of modules for twelve bucks on eBay it’s easy to settle for not perfect. And I got excited at the thought of building a mesh-style network of sensors and actuators using RF24Network. Perhaps a little too excited — my plans have become far more ambitious and involve retrofitting nrf modules on to all of the existing Arduino doodads already in my home, and some more new sensor modules out in the garden.
But first things first — order a couple, wire them up, and see if I can make them work.
nRF24L01+ on Arduino Uno
A basic network with two nodes is pretty straightforward. I used maniacbug’s getting started guide and a diagram of the nRFL01 module to connect the module to my Arduino as follows:
|Signal||Radio pin||Arduino pin|
|VCC||2||3.3V (NOT 5V)|
Note that the default connections for CE/CSN are pins 9 and 10, but I ended up using 8 and 9. This is because I was intending to build a base station with an Ethernet module, which is hard-coded to use pin 10 for it’s SS.
I used a couple of small IDC line sockets from Jaycar and some ribbon cable to breakout the pins on the nRF24L01 modules, and a breadboard to manage the interconnects hooking two modules up to two separate Arduino boards. Then installed the RF24 library. Loaded up the GettingStarted example sketch, modified the pins in the radio initialisation call (8 and 9 instead of 9 and 10) and loaded it on each Arduino in turn. This sketch then requires opening a serial monitor on one of the boards and putting it in Transmit mode. With luck that’s all that’s needed to get ping packets flowing from one board to the other and back again.
Once I was happy that the basic stuff worked, I used this pinout to solder a module on to a short prototyping shield, mounted it on an Ethernet-enabled Arduino compatible board, and my wireless base station was good to go!
nRF24L01+ on ATTiny84 w/ Arduino
I’ve been experimenting with the very small ATTiny85 chips for a while now, and love them for basic Arduino projects that only need one or two I/O pins. They seem ideal for remote sensor nodes, but the six pins required for the nRF24L01 completely exhaust the ATTiny85’s capabilities (although neat hacks exist to drive the chip with only three pins). Its larger sibling the ATTiny84 comes in a 14-pin package, though, and has a maximum of eleven pins available. So I ordered a couple of those with a view to building nodes in my network with them.
Getting this thing working properly had me thoroughly confused for a while. The ATtiny chips do not do hardware SPI. The data sheet has SPI pins indicated, but as I found out only after soldering one to a protoboard they’re intended for the ICSP interface, not to act as an SPI master. And there’s a few different methods out there to get around this fact. And to make matters worse during my testing I accidentally hooked one of my radio modules to the 5V line on my ICSP programmer and fried it without noticing, which lead to a few days of frustrated mucking around wondering why libraries weren’t working…
Eventually I settled on the arduino-tiny core for my ATtiny, and Stephen Crane’s SPI library, a cross-platform library that uses the regular SPI bus on chips that support it, and SPI over the USI bus on ATtiny chips. In the end the process was pretty simple.
- Install Steve’s SPI library, as well as his forks of the RF24 driver and RF24Network library.
- Open the RF24Network helloworld_rx sketch. Comment out all of the Serial calls. Change the radio initialisation to use pins 8 and 9 for CE and CSN.
- Burn the ATtiny84 1MHz core to a chip, and upload the sketch. I use High-Low Tech’s guide for working with these chips, and a Tiny AVR Programmer from SparkFun.
- Remove the ATtiny from the programmer, install it in a breadboard, and connect an nRF24L01 module to it. The ATtiny84 datasheet comes in handy for this, as well as the arduino-tiny documentation for pin mappings. In this table, “Chip pin” refers to the pin numbers on the chip, and “Arduino pin” refers to logical Arduino pins. Note that Arduino’s DO and DI are not the same as MOSI and MISO that are labelled on the chip.
Signal Radio pin Chip pin Arduino pin GND 1 14 GND VCC 2 1 VCC CE 3 11 D8 CSN 4 12 D9 SCK 5 9 SCK MOSI 6 8 DO MISO 7 7 DI INT 8 (not connected) (not connected)
- Add power to VCC and GND. The radio module is rated to a maximum 3.6V. I powered my circuit with 2.4V from a couple of Ni-Cad batteries, but the 3.3V output from another Arduino will do in a pinch.
I then took the radio module shield I’d made previously, and loaded the RF24Network helloworld_tx example sketch. Open the serial monitor, and you should see the base module sending packets out, and getting a response from the ATtiny.
And that’s the basic stuff. Right now I’m in the process of building a simple network node based around the ATtiny84. It’s mostly ready, but I’m still in the process of writing useful code.