Overview
In this article, we will learn Interfacing of NRF24L01 Wireless Transceiver Module with STM32 Microcontroller. We will make a Sender and Receiver Circuit using a pair of NRF24L01 + STM32F103C Bluepill Board. The NRF24L01 is employed on a good sort of applications that need wireless control. They are transceivers which suggest that every module can transmit and receive data. These modules are very cheap and you can use them with any microcontroller like Arduino or STM32F103C.
We will connect the NRF24L01 Module with STM32 & using some example code we will build a wireless network. In the first example, we will send a simple “Hello World” text from Transmitter to Receiver. In the second example, we will interface the BME280 Barometric Pressure Sensor with STM32 & NRF24L01. We will then send the Humidity, Temperature, Pressure & Altitude data from Transmitter/Sender to Receiver.
Bill of Materials
We need following components for this tutorial. All the components can be easily purchased from Amazon. The components purchase link is given below.
| S.N. | Components Name | Quantity | Purchase Links |
|---|---|---|---|
| 1 | STM32F103C Microcontroller | 2 | Amazon | AliExpress |
| 2 | NRF24L01 PA+LNA Module | 2 | Amazon | AliExpress |
| 3 | BME280 | 1 | Amazon | AliExpress |
| 4 | 5V Power Supply | 2 | Amazon | AliExpress |
| 5 | Connecting Wires | 20 | Amazon | AliExpress |
| 6 | Breadboard | 1 | Amazon | AliExpress |
NRF24L01 Module
The nRF24L01 is a wireless transceiver module, i.e. each module can both send & receive data. It works within the frequency of 2.4GHz. This frequency falls under the ISM band and is legal to use in most countries for engineering projects. The modules when operated efficiently can cover a distance of 100 meters.
The second version of this module, i.e. nRF24L01 PA+LNA comes with an SMA connector, duck-antenna & special RFX2401C chip which integrates the PA + LNA. This range extender chip along with a duck-antenna helps the module achieve a significantly larger transmission range about 1000m. The PA stands for Power Amplifier which boosts the power of the signal being transmitted from the nRF24L01+ chip. The LNA stands for the Low-Noise Amplifier which takes the extremely weak and uncertain signal from the antenna and amplifies it to a more useful level.
The module operates at 3.3V but its SPI pins are 5V tolerable. Each module has an address range of 125 and every module can communicate with 6 other modules hence mesh networking can be established with this module. The NRF24L01 module works with the help of SPI communications hence you can use NRF24L01 with any microcontroller with SPI Pins like STM32 or Arduino Boards.
If you want to learn more about this module, then you can follow our previous projects based on nRF24L01 Modules:
1. Wireless Sensor Data Monitoring with nRF24L01 & Arduino: Check Here
2. Wifi Gateway & Sensor Node with nRF24L01 Arduino & ESP8266: Check Here
Interfacing NRF24L01 Transceiver Module with STM32
Now let us interface NRF24L01 Transceiver Module with STM32 microcontroller and build our own transmitter and receiver. The schematic for transmitter and receiver circuit is given below. You can assemble a pair of circuit on breadboard.
The connection between NRF24L01 & STM32F103C Bluepill Boards is given below.
NRF24L01 CSN ………………………………………… PA4 of STM32F103C
NRF24L01 MOSI ………………………………………… PA7 of STM32F103C
NRF24L01 GND ………………………………………… GND of STM32F103C
NRF24L01 CE ………………………………………… PB0 of STM32F103C
NRF24L01 SCK ………………………………………… PA5 of STM32F103C
NRF24L01 MISO ………………………………………… PA6 of STM32F103C
NRF24L01 STM32 Library
The STM32 Board doesn’t support nRF24L01 RadioHead Library (#include <RH_NRF24.h>). Hence we need to use RF24 Library. This library is designed to be maximally compliant with the intended operation of the chip & built against the standard SPI library.
The library supports two header files:
|
1 2 |
#include <nRF24L01.h> #include <RF24.h> |
You can download the library from the following Github link. Add the library to the Arduino IDE.
Simple Sender & Receiver Code
Now let us test the simple communication between STM2 NRF24L01 Transmitter and Receiver. The transmitter circuit will send “Hello World” along with the packet number. You can display transmitted and received message on Serial Monitor.
Sender Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(PB0, PA4); // CE, CSN on Blue Pill const uint64_t address = 0xF0F0F0F0E1LL; int counter = 0; void setup() { Serial.begin(9600); radio.begin(); //Starting the Wireless communication radio.openWritingPipe(address); //Setting the address where we will send the data radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. radio.stopListening(); //This sets the module as transmitter } void loop() { char text[] = " Hello World"; char str[50]; sprintf(str,"%s %d",text,counter); radio.write(&str, sizeof(str)); Serial.println(str); counter++; delay(2000); } |
Receiver Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(PB0, PA4); // CE, CSN on Blue Pill const uint64_t address = 0xF0F0F0F0E1LL; boolean button_state = 0; void setup() { Serial.begin(9600); radio.begin(); Serial.print("ADDRESS :"); radio.openReadingPipe(0, address); //Setting the address at which we will receive the data radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver. radio.startListening(); //This sets the module as receiver } void loop() { if (radio.available()) //Looking for the data. { Serial.println("Radio is sniffing"); char text[32] = ""; //Saving the incoming data radio.read(&text, sizeof(text)); //Reading the data Serial.println(text); } } |
After uploading code on both the microcontroller, you can open both Serial Monitor. It will display the Sender and Receiver data with the Packet number.
Sending Sensor Data Wirelessly to Receiver using NRF24L01 & STM32
Let us go through the second example now. In this example, we will be Interfacing NRF24L01 with STM32 and BME280 sensor to the Sender Circuit. You can connect a BME280 Barometric Pressure Sensor to the Sender Circuit. The Sensor uses I2C Protocol for communication and measures the environmental temperature, humidity, Atmospheric Pressure, and Altitude. Below is the circuit of the transmitter.
The Sensor works on I2C Protocol so its I2C Pins, i.e SDA & SCL is connected to PB7 & PB6 respectively. We will send the BME280 Sensor reading wirelessly from STM32 NRF24L01 Sender to STM32 NRF24L01 Receiver.
Sender Code
The Sender Circuit requires a BME280 Library. You can get the library from this link: BME280 Library. Add the library to the Arduino Library folder.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
#include <SPI.h> #include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BME280.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(PB0, PA4); // CE, CSN on Blue Pill const uint64_t address = 0xF0F0F0F0E1LL; int counter = 0; float temperature; float humidity; float altitude; float pressure; #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME280 bme; struct MyData { int counter; float temperature; float humidity; float altitude; float pressure; }; MyData data; void setup() { Serial.begin(9600); radio.begin(); //Starting the Wireless communication radio.openWritingPipe(address); //Setting the address where we will send the data radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver. radio.stopListening(); //This sets the module as transmitter if (!bme.begin(0x76)) { Serial.println("Could not find a valid BME280 sensor, check wiring!"); while (1); } } void loop() { data.counter = counter; data.temperature = bme.readTemperature(); data.pressure = bme.readPressure() / 100.0F; data.altitude = bme.readAltitude(SEALEVELPRESSURE_HPA); data.humidity = bme.readHumidity(); Serial.print("Packet No. = "); Serial.println(data.counter); Serial.print("Temperature = "); Serial.print(data.temperature); Serial.println("*C"); Serial.print("Pressure = "); Serial.print(data.pressure); Serial.println("hPa"); Serial.print("Approx. Altitude = "); Serial.print(data.altitude); Serial.println("m"); Serial.print("Humidity = "); Serial.print(data.humidity); Serial.println("%"); Serial.println(); radio.write(&data, sizeof(MyData)); Serial.println("Data Packet Sent"); Serial.println(""); counter++; delay(3000); } |
Receiver Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(PB0, PA4); // CE, CSN on Blue Pill const uint64_t address = 0xF0F0F0F0E1LL; struct MyData { int counter; float temperature; float humidity; float altitude; float pressure; }; MyData data; void setup() { Serial.begin(9600); radio.begin(); Serial.print("ADDRESS :"); radio.openReadingPipe(0, address); //Setting the address at which we will receive the data radio.setPALevel(RF24_PA_MIN); //You can set this as minimum or maximum depending on the distance between the transmitter and receiver. radio.startListening(); //This sets the module as receiver } int recvData() { if ( radio.available() ) { radio.read(&data, sizeof(MyData)); return 1; } return 0; } void loop() { if(recvData()) { Serial.println("Data Received:"); Serial.print("Packet No. = "); Serial.println(data.counter); Serial.print("Temperature = "); Serial.print(data.temperature); Serial.println("*C"); Serial.print("Pressure = "); Serial.print(data.pressure); Serial.println("hPa"); Serial.print("Approx. Altitude = "); Serial.print(data.altitude); Serial.println("m"); Serial.print("Humidity = "); Serial.print(data.humidity); Serial.println("%"); Serial.println(); } } |
After uploading code, you can open Serial Monitor. The Serial Monitor will display the transmitted and received data.
You can make the Wifi Gateway to post the data to the Server. Here is an example post for doing this: STM32 NRF24L01 Sensor Node with ESP32 NRF24L01 Gateway














2 Comments
what is the point of using stm 32 if we need arduino? I want to do it with cubeide
could you find how to do