Tag Archives: nRF24

nRF24L01, Java and a PC

This article describes the ability to send and receive data using the nRF24L01 through the Java programming language from a PC.

The notion behind the idea came from a number of projects I was working upon where I had Arduino devices sending and receiving data through the nRF24L01 but needed a second Arduino to validate that my data was being transmitted correctly.  In addition, I wanted to have my PC involved in the communications for a variety of purposes such as dashboards and as a proxy to the Internet.  As a result of this, I asked myself how that might be achieved?  The result is as shown in the following block diagram:


We have an Arduino Nano which has a USB port combined with an nRF24L01 on a circuit board.  There will be more on this later but it is literally just two components with a cost of less than $5.  We will call this the “Serial nRF24”.  It attaches to the PC through a USB cable.  To the PC, it then looks like a standard serial device with its own COM port.  Data written to the COM port arrives at the Serial nRF24 while data written by the Serial nRF24 is available to be read from the COM port.

A breadboard layout for the Serial nRF24 may be:



Note that we have chosen CE to be pin 10 and CSN to be pin 9.  The other pin selections are required.  If you decide to use other pins, ensure you update the Arduino program to reflect your selection.

Running on the Serial nRF24 is a pre-written Arduino program (sketch) that is supplied with this article.  There is nothing more to be said about this program other than you load it up one time.

Finally, we have a Java JAR file that you make available on the PC.  This JAR file contains a class called NRF24Serial which exposes APIs that can be called by your own Java apps that run on the PC.  Your Java apps have no idea how the NRF24Serial class is implemented but, between you and me, the class reads and writes to the COM port which thus reads and writes to the Serial nRF24 which in turn interacts with other nRF24s wirelessly.

The NRF24Serial java class has the following API:

NRF24Serial(String portString)

Construct an instance of the class where the Serial nRF24 is found on the specified COM port.

NRF24Serial(SerialPort port)

Construct an instance of the class using the pre-populated SerialPort object as the interface to Serial nRF24.

void startListening()

Become a receiver and start listening for incoming packets of data.

void stopListening()

Stop being a receiver and allow us to transmit outgoing packets of data.

void setTXAddr(String addr)

Specify the target address of the device to which we wish to transmit data.

void setRXAddr(String addr)

Specify our address against which other devices can transmit data to us.

void read(byte data[])

Read data received over the network and place in the data array.

void write(byte data[])

Write data over the network to the partner device.

void setRate(int rate)

Set our communication rate over the wireless network.  Values are:

  • 1 – 1 MBps
  • 2 – 2 MBps

void dump()

Dump the details of the nRF24L01 device.

void test()

Test that we have a connection to our own nRF24L01 device and that all is well.

The Serial nRF24 Protocol

As we have seen, the Java API connects to the  Serial nRF24 using USB, but what does the actual serial protocol look like?  What if we want to drive the Serial nRF24 using a library other than Java?

Here we delve into the protocol used by Serial nRF24.

The protocol is based on an “AT” set of commands.  Commands are sent as ASCII text messages and responses are received.  Unless otherwise noted, all commands return one of two messages:

  • “OK” – The command executed correctly.
  • “FAIL” – The command failed.  Following the FAIL indicator is a text string providing more details of the failure.

Command: AT

No parameters.  Tests connection to the Serial nRF24.

Command: AT+RATE


Set the over the air transmission rate.  The following values are supported:

  • 1 – Set the transmission rate to be 1 MBps
  • 2 – Set the transmission rate to be 2 MBps

Command: AT+DIR


Set the direction of the Serial nRF24.  There are two possibilities:

  • TX – Make the Serial nRF24 a transmitter of data.
  • RX – Make the Serial nRF24 a receiver of data.

Command: AT+RXADDR

AT+RXADDR=Up to 5 characters (5*8 = 40 bits)

Set the address that the Serial nRF24 will appear as to other transmitters.  This is our identity on the network.

Command: AT+TXADDR

AT+TXADDR=Up to 5 characters (5*8 = 40 bits)

Set the address that the Serial nRF24 will send data to on the next transmission.



Set the channel used for communications.

Command: AT+WRITE


Transmit data.  The length of the data is specified as a parameter.  The actual data to be transmitted is sent immediately after the command.

Command: AT+READ


Read data.  The length of the data to be read is specified as a parameter.  The actual data read is returned  immediately after the command.

Command: AT+DUMP


Dump the current state of the Serial nRF24 to the serial stream.  This is used exclusively for debugging.



Set the power amplification level.  The lower the level, the less power consumed by the device.  Values are:

  • MIN
  • LOW
  • HIGH
  • MAX



Returns “OK” if we have detected a carrier and “FAIL” otherwise.

Command: AT+BAUD


Set the baud rate that the Serial nRF24 uses to communicate over serial.  The default baud is 9600.

Included dependencies

This project makes extensive use of the following Open Source modules


The project can be downloaded from Github.

See also

Attaching a nRF24 to a breadboard


The nRF24 is a great little communications device however it is far from breadboard friendly.  It has pins which are spaced 0.1″ apart in the horizontal … but also in the vertical … as such, you can’t simply plug it into a breadboard or into strip board for construction.



One solution to this problem is to buy female headers which have exceptionally long legs which are designed for stack-ability.  Items like this can be bought from eBay (example supplier) for about $4 for 10 (40 cents each).



These can then be cut to be the correct length which, for our purposes is 4 pins wide and then the pins can be bent twice to produce new right angles.



The nRF24 (or other device with similar pins) can then be plugged into the header and the pins from the header will now fit perfectly into your breadboard or strip board.



Here we see a nRF24 happily placed on a board.



An additional suggestions is to place a drip of cyanoacrylate between the touching headers to bond them forever but that is not a requirement for success.

Of course, this technique should work for any module/device which has a similar pin-out such as the ESP8266-1.

See also: