nRF24L01 and STM8S microcontrollers
Two RF24L01 and STM8S.
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.
- Microcontrolleur reference: Mainstream Value line 8-bit MCU with 8 Kbytes Flash, 16 MHz CPU, integrated EEPROM
- RF transceiver reference: Ultra low power 2.4GHz RF Transceiver
Getting used to the RF module
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.
We'll interface the module as following (see the application note in the datasheet):
|3||CE||Sends data or enable receiving|
|4||CSN||SPI Chip Select|
|8||IRQ||Module interruption flag|
We connected the two component this way:
|STM8S Side||24L01 Side|
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.
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.