IOT

Interfacing LoRa SX1278 with STM32 – Sender & Receiver

LoRa technology was developed by Cycleo, a French firm that was bought by Semtech in 2012. Semtech was a founding member of the LoRa Alliance, which today serves as the governing body for LoRa Technology. The LoRa Alliance is one of the most rapidly expanding technological partnerships. This non-profit organization has over 500 member firms that are dedicated to enabling large-scale deployment of Low Power Wide Area Networks (LPWAN) IoT through the development and promotion of the LoRaWAN open standard.

In this blog, we will learn how to interface the LoRa module SX1278 with the STM32 Bluepill microcontroller. The Ra-02 module employs the SX1278 IC and operates at 433MHz. It delivers the ideal balance of quality signal transmission using frequency hopping—that span a range of 420-450MHz. The LoRa SX1278 is compatible with any microcontroller that implements the SPI communication protocol. The SX1278 module is simple to connect to the STM32F103 microcontroller.

There are two examples in the tutorial. We’ll send a simple “Hello World” message from the LoRa Sender/Transmitter to the Receiver in the first example. In the second scenario, however, the sensor data will be sent wirelessly. The BME280 Barometric Pressure Sensor measures barometric pressure, temperature, humidity, and approximate elevation. The sensor data will be sent to the STM32 LoRa Receiver by the STM32 LoRa Sender.

Hardware Required

  • STM32F103C Bluepill Board
  • Ra-02 SX1278-LoRa Module
  • BME280-Barometric Pressure Sensor
  • 5V DC Adapter or 3.7V Battery
  • Jumper Wires
  • Breadboard

LoRa Module SX1278

The SX1278 Ra-02 Chip is manufactured by Semtech. Long-range spread spectrum communication is the primary use for the SX1278 RF module. It has a high sensitivity of -148 dBm and a power output of +20 dBm and can resist minimizing current usage. With great dependability, a transmission distance of over 5km can be reached. As a result, the SX1278 is ideal for IoT applications that need sending sensor data to the cloud over long distances.

SX1278 Module

The SX1278 IC from SEMTECH is used in this module, which operates at 433MHz. Frequency hopping will span a range of 420-450 MHz, giving you that perfect balance of excellent signal delivery. This long-range wireless capability comes in a compact (17 x 16mm) container with no antenna included.

SX1278 Pinout

SX1278 Pinout
  • Pin 1: ANT – This pin is used to connect to the antenna.
  • Pin 2, 9, 16: GND – Ground pin of common ground with power supply and controllers (pins 2, 9, 16).
  • Pin 3: 3.3V – In order to power up the device, pin 3 will be used
  • Pin 4: RESET – This pin is used to reset the module using an external signal.
  • Pin 5, 6, 7, 8, 10, 11: DIO0, DIO1, DIO2, DIO3, DIO4, DIO5 – The DIO pins are used to conduct general I/O functions through the module. As an interrupt pin, these pins can be customized.
  • Pin 12: SCK – The clock pulse during SPI communication is generated by the SCK pin.
  • Pin 13: MISO – MISO stands for Master in Slave Out and is used to send data from the Module to the Controller. SX1278 is the slave and the Master is the controller.
  • Pin 14: MOSI – MOSI means Master out Slave In. So, this pin will receive the data from Controller.
  • Pin 15: NSS – SS is a chip select/enable pin that aids in slave activation.

LoRa Frequency Allocation

The LoRa frequency is not permitted in all areas. I’m from India, where the unlicensed frequency range is between 865 and 867 MHz. As a result, I can only use this frequency range. Because the SX1278 operates at 433MHz, I am not permitted to utilize it for any reason other than academic research. Check your country’s permitted Ranges to make sure you’re allowed to utilize the exact frequency range. LoRa modules come in a variety of frequency bands, with the most popular being 433MHz, 915MHz, and 868MHz.

Interfacing LoRa SX1278 with STM32

Let’s connect the LoRa Module SX1278 to the STM32F103C Board to create a basic STM32 LoRa Sender Receiver Device. The following is the relationship between SX1278 and STM32F103:

SX1278 STM32F103C Connection

The diagram is shown below. On the breadboard, you can assemble the circuit pair. One circuit will function as a transmitter or sender, while the other will function as a receiver.

Interfacing LoRa SX1278 with STM32

LoRa Library for STM32

The STM32F103 Board does not support the Arduino LoRa Library. That is why the Arduino IDE has a customized library for the STM32F103 Board. For sending and receiving data through LoRa radios, the STM32 LoRa library is needed.

This library directly exposes the LoRa radio, allowing you to send data to any radio in range using the same radio parameters. There is no addressing and all data is broadcast.

Download: STM32 LoRa Library

Sender & Receiver Code

Sender Code

#include <SPI.h>
#include <LoRa_STM32.h>

#define SS PA4
#define RST PB0
#define DI0 PA1

#define TX_P 17

#define BAND 433E6
#define ENCRYPT 0x78

int counter = 0;

void setup() {
Serial.begin(115200);
while (!Serial);

Serial.println(“LoRa Sender”);

LoRa.setTxPower(TX_P);
LoRa.setSyncWord(ENCRYPT);

LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND))
{
Serial.println(“Starting LoRa failed!”);
while (1);
}
}

void loop() {
Serial.print(“Sending packet: “);
Serial.println(counter);

// send packet
LoRa.beginPacket();
LoRa.print(“hello “);
LoRa.print(counter);
LoRa.endPacket();

counter++;

delay(5000);
}

Receiver Code

#include <SPI.h>
#include <LoRa_STM32.h>

#define SS PA4
#define RST PB0
#define DI0 PA1

#define TX_P 17
#define BAND 433E6
#define ENCRYPT 0x78

void setup()
{
Serial.begin(115200);
while (!Serial);


Serial.println(“LoRa Receiver”);
LoRa.setTxPower(TX_P);
LoRa.setSyncWord(ENCRYPT);

LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND))
{
Serial.println(“Starting LoRa failed!”);
while (1);
}
}

void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();

if (packetSize) {
// received a packet
Serial.print(“Received packet ‘”);

// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}

// print RSSI of packet
Serial.print(“‘ with RSSI “);
Serial.println(LoRa.packetRssi());
}
}

Result

Sending Sensor Data Wirelessly using LoRa Module

Now let’s look at the second example. The Sender Circuit will be interfacing SX1278 with STM32 and BME280 sensor in this example. The BME280 is an integrated environmental sensor with high linearity and accuracy for pressure, humidity, and temperature readings, designed primarily for mobile applications.

STM32F103C LoRa

Since the sensor uses the I2C protocol, its SDA and SCL pins are connected to PB7 and PB6, respectively. The BME280 sensor reading will be sent wirelessly from the STM32 LoRa Sender to the STM32 LoRa Receiver.

Project PCB Gerber File & PCB Ordering Online

If you don’t want to put the circuit together on a breadboard and instead want a PCB for your project, here is the PCB for you. EasyEDA’s online Circuit Schematics & PCB Design tool was used to create the PCB Board for the STM32 LoRa SX1278 Sender & Receiver. Below is a picture of the Sender PCB.

Similarly, the image of the Receiver PCB is given below.

Below is the Gerber File for the PCB. You can order the PCB by downloading the Gerber File from https://www.nextpcb.com/

Sender PCB Gerber File: Sender PCB Gerber File

Receiver PCB Gerber File: Receiver PCB Gerber File

You can now visit the official NextPCB website by going to https://www.nextpcb.com/. As a result, you’ll be taken to the NextPCB website.

  • You can now upload the Gerber File to the Website and place an order. The PCB quality is excellent. That is why the majority of people entrust NextPCB with their PCB and PCBA needs.

Source Code/Program

Below is the LoRa SX1278 STM32 Sender and Receiver Code. A BME280 Library is required for the Sender Code.Download the BME280 Library from this link: BME280 Library

STM32 LoRa Sender Receiver

Sender Code

#include <Wire.h>
#include <SPI.h>
#include <LoRa_STM32.h>

#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define NSS PA4
#define RST PB0
#define DI0 PA1

#define TX_P 17
#define BAND 433E6
#define ENCRYPT 0x78

int counter = 0;
String LoRaMessage = “”;

#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;

void setup()
{
Serial.begin(115200);
while (!Serial);

Serial.println(F(“LoRa Sender”));

//LoRa.setTxPower(TX_P);
LoRa.setSyncWord(ENCRYPT);

LoRa.setPins(NSS, RST, DI0);
if (!LoRa.begin(BAND))
{
Serial.println(F(“Starting LoRa failed!”));
while (1);
}
if (!bme.begin(0x76))
{
Serial.println(“Could not find a valid BME280 sensor, check wiring!”);
while (1);
}
}

void loop()
{
float temperature = bme.readTemperature();
float pressure = bme.readPressure() / 100.0F;
float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
float humidity = bme.readHumidity();

Serial.print(F(“Sending packet: “));
Serial.println(counter);

Serial.print(F(“Temperature = “));
Serial.print(temperature);
Serial.println(F(“*C”));

Serial.print(F(“Pressure = “));
Serial.print(pressure);
Serial.println(F(“hPa”));

Serial.print(F(“Approx. Altitude = “));
Serial.print(altitude);
Serial.println(F(“m”));

Serial.print(F(“Humidity = “));
Serial.print(humidity);
Serial.println(F(“%”));

Serial.println();

LoRaMessage = String(counter) + “/” + String(temperature) + “&” + String(pressure) + “#” + String(altitude) + “@” + String(humidity);

// send packet
LoRa.beginPacket();
LoRa.print(LoRaMessage);
LoRa.endPacket();

counter++;

delay(3000);
}

Receiver Code

#include <SPI.h>
#include <LoRa_STM32.h>

#define SS PA4
#define RST PB0
#define DI0 PA1

#define TX_P 17
#define BAND 433E6
#define ENCRYPT 0x78


String counter;
String temperature;
String pressure;
String altitude;
String humidity;

void setup()
{
Serial.begin(115200);
while (!Serial);

Serial.println(“LoRa Receiver”);
//LoRa.setTxPower(TX_P);
LoRa.setSyncWord(ENCRYPT);

LoRa.setPins(SS, RST, DI0);
if (!LoRa.begin(BAND))
{
Serial.println(“Starting LoRa failed!”);
while (1);

}
}

void loop() {
// try to parse packet
int pos1,pos2,pos3,pos4;

int packetSize = LoRa.parsePacket();
if (packetSize)
{
// received a packet
Serial.print(“Received packet: “);
String LoRaData = LoRa.readString();
Serial.print(LoRaData);
// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
// print RSSI of packet
Serial.print(“‘ with RSSI “);
Serial.println(LoRa.packetRssi());


pos1 = LoRaData.indexOf(‘/’);
pos2 = LoRaData.indexOf(‘&’);
pos3 = LoRaData.indexOf(‘#’);
pos4 = LoRaData.indexOf(‘@’);

counter = LoRaData.substring(0, pos1);
temperature = LoRaData.substring(pos1+1,pos2);
pressure = LoRaData.substring(pos2+1,pos3);
altitude = LoRaData.substring(pos3+1,pos4);
humidity = LoRaData.substring(pos4+1,LoRaData.length());


Serial.print(F(“Packet No. = “));
Serial.println(counter);


Serial.print(F(“Temperature = “));
Serial.print(temperature);
Serial.println(F(“*C”));

Serial.print(F(“Pressure = “));
Serial.print(pressure);
Serial.println(F(“hPa”));

Serial.print(F(“Approx. Altitude = “));
Serial.print(altitude);
Serial.println(F(“m”));


Serial.print(F(“Humidity = “));
Serial.print(humidity);
Serial.println(F(“%”));
Serial.println();
}
}

Results

Conclusion
I hope all of you understand how to Interfacing LoRa SX1278 with STM32 – Sender & Receiver. We MATHA ELECTRONICS will be back soon with more interesting blogs.

Leave a Reply

Your email address will not be published.