Arduino projects

Use Arduino to connect SX1278 (Ra-02) LoRa module

It is estimated that by 2020, we will have 2.5 billion devices connected to the Internet. In other words, the number is more than three times the population of the earth today. This is most likely to happen as the concepts of IoT and Industry 4.0, IoT cars and smart cities spread rapidly. We already have some wireless protocols such as BLE, Wi-Fi, Cellular, etc., but these technologies are not suitable for IoT sensor nodes because they need to transmit information over long distances without using too much power. This has led to the rise of LoRa technology, which can perform ultra-long distance transmission with low power consumption.

As ESP modules have become synonymous with Wi-Fi applications, this LoRa technology also has the ability to build a huge network like the Internet. We have previously built many IoT based projects using ESP8266 and Arduino, in this article we will learn about LoRa and how to use it with the Arduino development platform.

What is LoRa?

The term LoRa stands for Long Range . It is a wireless radio frequency technology introduced by a company called Semtech. This LoRa technology can be used to transmit bidirectional information over long distances without consuming much power. Remote sensors can use this property and transmit data by operating only small batteries.

Typically, LoRa can reach a distance of 15-20km (more on that later) and can last for years on batteries. Remember that LoRa, LoRaWAN, and LPWAN are three different terms that should not be confused with each other. We will briefly discuss them later in this article.

Learn about LoRa Technology

In any typical IoT solution provided for warehouse management or field monitoring, hundreds of sensor nodes will be deployed in the field which will monitor the important parameters and send them to the cloud platform for processing. But these sensors should be wireless and should use small batteries so that they can be carried. Wireless solutions like RF can send data to long distances but require more power for battery operation, while BLE on the other hand can work with very little power but cannot send data over long distances. So this is why LoRa is needed.

In LoRa, we can achieve high-distance communication without using a lot of power, thus overcoming the shortcomings of Wi-Fi and BLE communication. But how is it possible? If this is the case, why do BLE and RF still exist?

That’s because LoRa has its own disadvantages. In order to achieve the long distance of high-power LoRa bandwidth, it operates on very low bandwidth. The maximum bandwidth of Lora is about 5.5 kbps, which means that you can only send small amounts of data through LoRa. Therefore, you cannot send audio or video through this technology, and it is only suitable for transmitting less information, such as sensor values. The figure below shows where LoRa stands compared to Wi-Fi, Bluetooth, and Cellular cellular devices.

Use Arduino to connect SX1278 (Ra-02) LoRa module

Many people compare LoRa to Wi-Fi or Bluetooth, but these two are not even close to LoRa. Bluetooth is used to transfer information between two Bluetooth devices, and Wi-Fi is used to transfer information between access points (routers) and stations (mobile). But LoRa technology was not invented primarily to transfer data between two LoRa modules.

You can think of LoRa as more like cellular communication. The signal from one LoRa node goes through the LoRa gateway to another node, as shown in the figure below.

Use Arduino to connect SX1278 (Ra-02) LoRa module

These gateways then pass the information to the internet and finally to the end user through the API. Similarly, data from the user will also pass through the network servers and gateways to the node.

LoRa nodes usually run on batteries and consist of a radio module and a microprocessor. The microprocessor is used to read the data from the sensor and send it over the air through the radio module, which is then received by the LoRa gateway. LoRa gateways also have radio modules and microprocessors, but usually run on AC power because they require more power. A single LoRa gateway can listen to multiple LoRa nodes, and a single LoRa node can also send information to multiple gateways, so that the information from the node will be accessed by the gateway without being lost. When the information id is sent from the node to the gateway, it is called uplink, and when it is sent from the gateway to the node, it is called downlink.

LoRa belongs to the LPWAN category where LPWAN stands for Low Power Wide Area Network . It is not only LoRa that can run on LPWAN, we also have other technologies like Narrowband IoT (NB-IOT), Sigfox, etc. that are able to run in the same LPWAN. Once the LoRa technology was introduced, it required all manufacturers to follow certain protocols, so the LoRa Alliance was formed and then LoRaWAN was introduced. LoRaWAN is a modified form of LPWAN that specifies the protocol of how LoRa in the physical layer should be used to send and receive data between nodes, gateways, and the internet.

How far can I use LoRa communication?

Official information about LoRa claims it can reach up to 715 km line of sight. That’s when there are no obstacles between the node and the gateway. Few have even actually communicated up to 212 km ground to ground, and even up to 702 km using weather balloons.

LoRa SX1278 with Arduino

Enough theory above let’s build it ourselves and check how it works. Remember the part where I told you that two LoRa modules can’t communicate with each other? Ok I lied… Yes, the LoRaWAN protocol does not allow communication between two LoRa modules, but there is a technique called the Radio Head Packet Method, which follows the LoRaWAN protocol but allows us to communicate with two LoRa modules! So let’s use two LoRa modules and two Arduino boards to send data from one board and then receive it on the other. We will use an Arduino Uno on the transmitter end and an Arduino Nano on the receiving end.

The LoRa module that I have used here is the SX1278 Ra-02 which operates at 433MHz. The license-free frequency range here is from 865MHz to 867MHz, so I am not legally allowed to use a 433MHz frequency module for long periods of time other than for educational purposes. Again, check the range allowed in your country and make sure you can use a specific frequency range. There are different frequency ranges for LoRa modules, the most common ones are 433MHz, 915MHz, and 868MHz. The frequency at which the module operates will be mentioned after the module. You can also buy the LoRa as a module or just the chip. If you plan to get only the chip, then make sure your soldering skills are good as it is a good job to solder to the LoRa chip. Here are the versions of the module and chip soldered wires as shown below.

Use Arduino to connect SX1278 (Ra-02) LoRa module

The next important thing for your LoRa module is your antenna. Remember, you must use an antenna to operate the LoRa module, otherwise the output transmit power will damage the module. I am using a 433Mhz Lora module, so my antenna is rated for 433MHz, you must choose your antenna accordingly. My LoRa module and antenna are shown below.

Use Arduino to connect SX1278 (Ra-02) LoRa module

Transmitter – Connect LoRa SX1278 to Arduino UNO

For the transmitting end, we will use Arduino UNO and our LoRa module. The circuit diagram for connecting Arduino UNO with LoRa is shown below

Use Arduino to connect SX1278 (Ra-02) LoRa module

The LoRa module consists of 16 pins, 8 pins on each side. Out of these 16 pins, 6 are used by GPIO pins ranging from DIO0 to DIO5 and 4 are used by ground pins. The module operates on 3.3V, so the 3.3V pin on the LoRa is connected to the 3.3V pin on the Arduino UNO board. Then we connect the SPI pins on the LoRa to the SPI pins on the Arduino Board as shown above.

I used hookup wires to make the connection between Arduino UNO and LoRa Module. The setup is shown below. The whole setup can be powered by a power bank, making it mobile to test the range.

Use Arduino to connect SX1278 (Ra-02) LoRa module

Receiver – Connect LoRa SX1278 to Arduino Nano

For the receiving end, we will use LoRa module and Arduino Nano. You can use any Arduino board for transmitter and receiver but make sure you connect them accordingly. The circuit diagram for connecting Arduino Nano with LoRa is shown below

Use Arduino to connect SX1278 (Ra-02) LoRa module

The connections remain almost the same except for one minor change. The 3.3V pin of the LoRa module is not powered by the Arduino Nano but by an external 3.3V regulator. This is because the onboard regulator on the Arduino Nano is not able to provide enough current for the LoRa module to operate. Apart from this, the connections remain the same.

I made the connections on a breadboard and used the trusty old breadboard power supply we built earlier. Also note that the LoRa module I used doesn’t fit well on a breadboard, so I used two small breadboards to make the connections as shown below.

Use Arduino to connect SX1278 (Ra-02) LoRa module

Prepare Arduino ID for LoRa wireless communication

Once the hardware is ready, we can proceed to use the Arduino IDE. To use the LoRa module with the Arduino board, we already have the well-built LoRa library by Sandeep Mistry . In this article, we will include the library into the Arduino IDE and use the example sketch with slight modifications to make our LoRa modules communicate between them.

To add the library, open the Arduino IDE and go to Sketch -> Include Library -> Manage Libraries . Then search for ” LoRa Radio ” and find the library made by Sandeep Mistry and click ” Install “. Wait for the installation to finish and you should see something like this at the end

Use Arduino to connect SX1278 (Ra-02) LoRa module

Now restart the Arduino IDE, open the LoRa Example program using File -> Example -> LoRa , then open the LoRa Receiver and LoRa Sender Program as shown below.

Use Arduino to connect SX1278 (Ra-02) LoRa module

Both the programs are also given at the end of this article for your reference. The complete programs are commented. The sending program sends a “hello” every 5 seconds and the value of the counter is incremented. The receiver then receives it and prints on the serial monitor with the RSSI value.

One important aspect that you have to change is the LoRa.begin() function. By default, the program uses “LoRa.begin(915E6) “, which means it sets the LoRa module to 915MHz. If your module is actually 915MHz, that’s fine too, but mine is working at 433MHz here, as I said before, so you have to change it as shown in the following image

  1. LoRa.begin(433E6)

Copy code

You can visit the library documentation to learn what each function in the library means and how to change it.

Likewise, the frequency must be changed on the receiver program. Once the program is ready, upload the program to the corresponding development board after ensuring that the connections are correct and the antenna is connected to the LoRa module.

Wireless communication between LoRa and Arduino

After uploading the program, open the serial monitor of both Arduino boards. The serial monitor of the transmitter should show the values ​​sent when the receiver will display it on its serial monitor. The screen should look something like this.

Use Arduino to connect SX1278 (Ra-02) LoRa module

You should also pay attention to the RSSI value of each message received by the LoRa module. The term RSSI stands for Received Signal Strength Indicator. The value is always negative, in our case it is around -68. The closer this value is to zero, the stronger the signal strength is. For example, if I separate the two devices far apart, the signal strength will drop.

With this setup I was able to receive the signal about 500 meters with obstacles in between. For line of sight communication and better antennas this value should be high. Hope you understood this project and enjoyed making something with it, if you have any questions leave your questions on the work and I’ll do my best to answer them.

The following is the code used in this article

  1. LORA code for Transmitting Side
  2. #include <SPI.h>
  3. #include <LoRa.h>
  4. int counter = 0;
  5. void setup() {
  6.   Serial.begin(9600);
  7.   while (!Serial);
  8.   Serial.println(“LoRa Sender”);
  9.   if (!LoRa.begin(433E6)) {
  10.     Serial.println(“Starting LoRa failed!”);
  11.     while (1);
  12.   }
  13.   LoRa.setTxPower(20);
  14. }
  15. void loop() {
  16.   Serial.print(“Sending packet: “);
  17.   Serial.println(counter);
  18.   // send packet
  19.   LoRa.beginPacket();
  20.   LoRa.print(“hello”);
  21.   LoRa.print(counter);
  22.   LoRa.endPacket();
  23.   counter++;
  24.   delay(5000);
  25. }
  26. LORA code for Receiver Side
  27. #include <SPI.h>
  28. #include <LoRa.h>
  29. void setup() {
  30.   Serial.begin(9600);
  31.   while (!Serial);
  32.   Serial.println(“LoRa Receiver”);
  33.   if (!LoRa.begin(433E6)) {
  34.     Serial.println(“Starting LoRa failed!”);
  35.     while (1);
  36.   }
  37. }
  38. void loop() {
  39.   // try to parse packet
  40.   int packetSize = LoRa.parsePacket();
  41.   if (packetSize) {
  42.     // received a packet
  43.     Serial.print(“Received packet ‘”);
  44.     // read packet
  45.     while (LoRa.available()) {
  46.       Serial.print((char)LoRa.read());
  47.     }
  48.     // print RSSI of packet
  49.     Serial.print(“‘ with RSSI “);
  50.     Serial.println(LoRa.packetRssi());
  51.   }
  52. }

Copy code

Back to top button