You are here

nRF24L01 and STM8S microcontrollers

Not so frequently asked questions and stuff: 

Same article in French here.

RF24L01 et STM8S
Two RF24L01 and STM8S.

Introduction

It's very easy to buy on ebay or elsewhere some 2.4 GHz RF transceiver using the Nordic RF24L01 chip, ranging around $3. Let's test a couple of them with two STM8S microcontrollers.

We'll use some dev boards from STM: the STM8SVLDISCOVERY. It includes a STM8S003K3 controller with a USB programmer. That's everything we need to make the stuff work without any soldering.

Getting used to the RF module

Des trames SPI entre un STM8S et un RF42L01.
SPI signals between a STM8S and a RF24L01.

We interact with the RF24L01 using a standard SPI link. (CS/CLK/MISO/MOSI). One GPIO (CE) allows switching between the STANDBY and active SEND/RECEIVE modes. Another one (IRQ) allows us to know when something important happens (packet received, packet sent, packet sent but lost).

The module works with 1 to 32 byte packets. It automatically handles reassembling them on the reception side, and re-emit them when they are lost. It also includes some CRC code to be sure the data is valid. Many other options are available, so read the datasheet to get stars in your eyes.

The module is not full duplex, so you have to alternate between send and receive modes.

Wiring

RF24L01_module_pineout

We'll interface the module as following (see the application note in the datasheet):

Pin Signal Description?
1 (square) GND Ground
2 VCC 3.3V
3 CE Sends data or enable receiving
4 CSN SPI Chip Select
5 SCK SPI Clock
6 MOSI Master data
7 MISO Module Data
8 IRQ Module interruption flag

We connected the two component this way:

STM8S Side 24L01 Side
PC3 CE
PC4 CSN
PC7 MISO
PC6 MOSI
PC5 SCK
PC2 IRQ

Code example

The example code we wrote send two 32 bit integers from the master to the slave. Then the slave effectuates the basic math operations and sends the results.

This kind of example allows you to be sure that the byte stream is reassembled correctly (watch your byte sex is you use another platform), and that the packets are the right ones (i.e. that you get the latest and not the penultimate one or one stored something in a FIFO or a buffer).

The code was written using IAR workbench for STM 1.40.1.

It is available on GitHub here.

License: MIT.

Range testing

We tested the range of the module. We measured a range of about one hundred meters outside. The range inside is obviously less.

Conclusion: for their price, theses modules are great.