How to detect RFM12B or NRF24L01 device presence in your code

I’m using day by day a custom board to debug and test my Arduino code. My board exposes an NRF24L01 connector that I’m using also to connect lot of devices (RFM12B, RFM69, MAX18355, SD, FLASH, …) mounted on a breakboard such as this one for example. Some time during test I’m switching from one board to another, it is quite useful to be able to check that the device is here and is responding.

How to do that ?

Well, as usual, reading data sheet of the device can help a lot. but I’ve already done this and the code is already written. Just read below on how to achieve this.

RFM12B Module

RFM12 datasheet has the ability to detect low voltage, we will be using this function to detect the device. For this example I’m using LowPowerLab rfm12b library written by Felix and tweaked from excellent one written by jcw from jeelabs, but you can use another one the same way.

The Low voltage threshold can be setup from 3.75V to 2.25V. So until you’re powering the RFM12B device from let’s say 2.4V to 3.6V (which is juste the datasheet recomendation) you will be able to detect the device. The trick is done in two phases.

  • Set threshold voltage to 3.75V, and check we’re under voltage.
  • then set threshold voltage to 2.25V and check we’re above under voltage

That’s it, simple and efficient not ?

NRF24L01 Module

NRF24L01 datasheet has the ability to set configuration parameters and read them back. We will be using this function to detect the device. For this example I’m using RF24 Arduino library from Stanley and tweaked from excellent one written by maniacbug, but you can use another one the same way, just check library functions and methods.

We will setup number of retries and read them back and also we will trie to set the module RF speed. Then read back the number of retries and check that speed command has been done successfully. The trick is done in three phases.

  • Set number of retries
  • Read back number of retries and compare with the value we set
  • Set the speed (baud rate) and check if it has been done correctly

Example code

The example code below is a function that return if the device detected is an RFM12B or NRF24L01 module. Of course two radios modules need to be declared and instancied for this, but if you use only one, juste instantiate the one you use and remove code from the unneeded module in the function.

Have fun detection, I will add detection for RF69 as soon as I will need it and read the datasheet.