Random Nerd Tutorials
Shares

ESP32 with LoRa using Arduino IDE – Getting Started

Shares

In this tutorial we’ll explore the basic principles of LoRa, and how it can be used with the ESP32 for IoT projects using the Arduino IDE. To get you started, we’ll also show you how to create a simple LoRa Sender and LoRa Receiver with the RFM95 transceiver module.

Introducing LoRa

For a quick introduction to LoRa, you can watch the video below, or you can scroll down for a written explanation.


What is LoRa?

LoRa is a wireless data communication technology that uses a radio modulation technique that can be generated by Semtech LoRa transceiver chips.

This modulation technique allows long range communication of small amounts of data (which means a low bandwidth), high immunity to interference, while minimizing power consumption. So, it allows long distance communication with low power requirements.

LoRa Frequencies

LoRa uses unlicensed frequencies that are available worldwide. These are the most widely used frequencies:

  • 868 MHz for Europe
  • 915 MHz for North America
  • 433 MHz band for Asia

Because these bands are unlicensed, anyone can freely use them without paying or having to get a license. Check the frequencies used in your country.

LoRa Applications

LoRa long range and low power features, makes it perfect for battery-operated sensors and low-power applications in:

  • Internet of Things (IoT)
  • Smart home
  • Machine-to-machine communication
  • And much more…

So, LoRa is a good choice for sensor nodes running on a coil cell or solar powered, that transmit small amounts of data.

Keep in mind that LoRa is not suitable for projects that:

  • Require high data-rate transmission;
  • Need very frequent transmissions;
  • Or are in highly populated networks.

LoRa Topologies

You can use LoRa in:

  • Point to point communication
  • Or build a LoRa network (using LoRaWAN for example)

Point to Point Communication

In point to point communication, two LoRa enabled devices talk with each other using RF signals.

For example, this is useful to exchange data between two ESP32 boards equipped with LoRa transceiver chips that are relatively far from each other or in environments without Wi-Fi coverage.

Unlike Wi-Fi or Bluetooth that only support short distance communication, two LoRa devices with a proper antenna can exchange data over a long distance.

You can easily configure your ESP32 with a LoRa chip to transmit and receive data reliably at more than 200 meters distance (you can get better results depending on your enviroment and LoRa settings). There are also other LoRa solutions that easily have a range of more than 30Km.

LoRaWAN

You can also build a LoRa network using LoRaWAN.

The LoRaWAN protocol is a Low Power Wide Area Network (LPWAN) specification derived from LoRa technology standardized by the LoRa Alliance. We won’t explore LoRaWAN in this tutorial, but for more information you can check the LoRa Alliance and The Things Network websites.

How can LoRa be useful in your home automation projects?

Let’s take a look at a practical application.

Imagine that you want to measure the moisture in your field. Although, it is not far from your house, it probably doesn’t have Wi-Fi coverage. So, you can build a sensor node with an ESP32 and a moisture sensor, that sends the moisture readings once or twice a day to another ESP32 using LoRa.

The later ESP32 has access to Wi-Fi, and it can run a web server that displays the moisture readings.

This is just an example that illustrates how you can use the LoRa technology in your ESP32 projects.

Note: we teach how to build this project on our “Learn ESP32 with Arduino IDE” course. It is Project 4 on the Table of Contents: LoRa Long Range Sensor Monitoring – Reporting Sensor Readings from Outside: Soil Moisture and Temperature. Check the course page for more details.

ESP32 with LoRa

In this section we’ll show you how to get started with LoRa with your ESP32 using Arduino IDE. As an example, we’ll build a simple LoRa Sender and a LoRa Receiver.

The LoRa Sender will be sending a “hello” message followed by a counter for testing purposes. This message can be easily replaced with useful data like sensor readings or notifications.

To follow this part you need the following components:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Preparing the Arduino IDE

There’s an add-on for the Arduino IDE that allows you to program the ESP32 using the Arduino IDE and its programming language. Follow one of the next tutorials to prepare your Arduino IDE to work with the ESP32, if you haven’t already.

Installing the LoRa Library

There are several libraries available to easily send and receive LoRa packets with the ESP32. In this example we’ll be using the arduino-LoRa library by sandeep mistry.

Open your Arduino IDE, and go to Sketch > Include Library > Manage Libraries and search for “LoRa“. Select the LoRa library highlighted in the figure below, and install it.

Getting LoRa Tranceiver Modules

To send and receive LoRa messages with the ESP32 we’ll be using the RFM95 transceiver module. All LoRa modules are transceivers, which means they can send and receive information. You’ll need 2 of them.

You can also use other compatible modules like Semtech SX1276/77/78/79 based boards including: RFM96W, RFM98W, etc…

Alternatively, there are ESP32 boards with LoRa and OLED display built-in like the ESP32 Heltec Wifi Module, or the TTGO board.

Before getting your LoRa transceiver module, make sure you check the correct frequency for your location. You can visit the following web page to learn more about RF signals and regulations according to each country. For example, in Portugal we can use a frequency between 863 and 870 MHz or we can use 433MHz. For this project, we’ll be using an RFM95 that operates at 868 MHz.

Preparing the RFM95 Transceiver Module

If you have an ESP32 development board with LoRa built-in, you can skip this step.

The RFM95 transceiver isn’t breadboard friendly. A common row of 2.54mm header pins won’t fit on the transceiver pins. The spaces between the connections are shorter than usual.

There are a few options that you can use to access the transceiver pins.

  • You may solder some wires directly to the transceiver;
  • Break header pins and solder each one separately;
  • Or you can buy a breakout board that makes the pins breadboard friendly.

We’ve soldered a header to the module as shown in the figure below.

This way you can access the module’s pins with regular jumper wires, or even put some header pins to connect them directly to a stripboard or breadboard.

Antenna

The RFM95 transceiver chip requires an external antenna connected to the ANA pin.

You can connect a “real” antenna, or you can make one yourself by using a conductive wire as shown in the figure below. Some breakout boards come with a special connector to add a proper antenna.

The wire length depends on the frequency:

  • 868 MHz: 86,3 mm (3.4 inch)
  • 915 MHz: 81,9 mm (3.22 inch)
  • 433 MHz: 173,1 mm (6.8 inch)

For our module we need to use a 86,3 mm wire soldered directly to the transceiver’s ANA pin. Note that using a proper antenna will extend the communication range.

Important: you MUST attach an antenna to the module.

Wiring the RFM95 LoRa Transceiver Module

The RFM95 LoRa transceiver module communicates with the ESP32 using SPI communication protocol. So, we’ll use the ESP32 default SPI pins. Wire both ESP32 boards to the corresponding transceiver modules as shown in the next schematic diagram:

Here’s the connections between the RFM95 LoRa transceiver module and the ESP32:

  • ANA: Antenna
  • GND: GND
  • DIO3: don’t connect
  • DIO4: don’t connect
  • 3.3V: 3.3V
  • DIO0: GPIO 2
  • DIO1: don’t connect
  • DIO2: don’t connect
  • GND: don’t connect
  • DIO5: don’t connect
  • RESET: GPIO 14
  • NSS: GPIO 5
  • SCK: GPIO 18
  • MOSI: GPIO 23
  • MISO: GPIO 19
  • GND: don’t connect

Note: the RFM95 transceiver module has 3 GND pins. It doesn’t matter which one you use, but you need to connect at least one.

For practical reasons we’ve made this circuit on a stripboard. It’s easier to handle, and the wires don’t disconnect. You may use a breadboard if you prefer.

The LoRa Sender Sketch

Open your Arduino IDE and copy the following code. This sketch is based on an example from the LoRa library. It transmits messages every 10 seconds using LoRa. It sends a “hello” followed by a number that is incremented in every message.

Let’s take a quick look at the code.

It starts by including the needed libraries.

#include <SPI.h>
#include <LoRa.h>

Then, define the pins used by your LoRa module. If you’ve followed the previous schematic, you can use the pin definition used in the code. If you’re using an ESP32 board with LoRa built-in, check the pins used by the LoRa module in your board and make the right pin assignment.

#define ss 5
#define rst 14
#define dio0 2

You initialize the counter variable that starts at 0;

int counter = 0;

In the setup(), you initialize a serial communication.

Serial.begin(115200);
while (!Serial);

Set the pins for the LoRa module.

LoRa.setPins(ss, rst, dio0);

And initialize the transceiver module with a specified frequency.

while (!LoRa.begin(866E6)) {
  Serial.println(".");
  delay(500);
}

You might need to change the frequency to match the frequency used in your location. Choose one of the following options:

  • 433E6
  • 866E6
  • 915E6

LoRa transceiver modules listen to packets within its range. It doesn’t matter where the packets come from. To ensure you only receive packets from your sender, you can set a sync word (ranges from 0 to 0xFF).

LoRa.setSyncWord(0xF3);

Both the receiver and the sender need to use the same sync word. This way, the receiver ignores any LoRa packets that don’t contain that sync word.

Next, in the loop() you send the LoRa packets. You initialize a packet with the beginPacket() method.

LoRa.beginPacket();

You write data into the packet using the print() method. As you can see in the following two lines, we’re sending a hello message followed by the counter.

LoRa.print("hello ");
LoRa.print(counter);

Then, close the packet with the endPacket() method.

LoRa.endPacket();

After this, the counter message is incremented by one in every loop, which happens every 10 seconds.

counter++;
delay(10000);

Testing the Sender Sketch

Upload the code to your ESP32 board. Make sure you have the right board and COM port selected.

After that, open the Serial Monitor, and press the ESP32 enable button. You should see a success message as shown in the figure below. The counter should be incremented every 10 seconds.

The LoRa Receiver Sketch

Now, grab another ESP32 and upload the following sketch (the LoRa receiver sketch). This sketch listens for LoRa packets with the sync word you’ve defined and prints the content of the packets on the Serial Monitor, as well as the RSSI. The RSSI measures the relative received signal strength.

This sketch is very similar to the previous one. Only the loop() is different.

You might need to change the frequency and the sycnword to match the one used in the sender sketch.

In the loop() the code checks if a new packet has been received using the parsePacket() method.

int packetSize = LoRa.parsePacket();

If there’s a new packet, we’ll read its content while it is available.

To read the incoming data you use the readString() method.

while (LoRa.available()) {
  String LoRaData = LoRa.readString();
  Serial.print(LoRaData); 
}

The incoming data is saved on the LoRaData variable and printed in the Serial Monitor.

Finally, the next two lines of code print the RSSI of the received packet in dB.

Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());

Testing the LoRa Receiver Sketch

Upload this code to your ESP32. At this point you should have two ESP32 boards with different sketches: the sender and the receiver.

Open the Serial Monitor for the LoRa Receiver, and press the LoRa Sender enable button. You should start getting the LoRa packets on the receiver.

Congratulations! You’ve built a LoRa Sender and a LoRa Receiver using the ESP32.

Taking It Further

Now, you should test the communication range between the Sender and the Receiver on your area. The communication range greatly varies depending on your environment (if you live in a rural or urban area with a lot of tall buildings). To test the communication range you can add an OLED display to the LoRa receiver and go for a walk to see how far you can get a communication (this is a subject for a future tutorial).

In this example we’re just sending an hello message, but the idea is to replace that text with useful information.

Wrapping Up

In summary, in this tutorial we’ve shown you the basics of LoRa technology:

  • LoRa is a radio modulation technique;
  • LoRa allows long-distance communication of small amounts of data and requires low power;
  • You can use LoRa in point to point communication or in a network;
  • LoRa can be especially useful if you want to monitor sensors that are not covered by your Wi-Fi network and that are several meters apart.

We’ve also shown you how to build a simple LoRa sender and LoRa receiver. These are just simple examples to get you started with LoRa. We’ll be adding more projects about this subject soon, so stay tuned!

This is an excerpt from our course: Learn ESP32 with Arduino IDE. If you like ESP32 and you want to learn more, we recommend enrolling in Learn ESP32 with Arduino IDE course.

Thanks for reading.


20 Easy Raspberry Pi Projects

Our new book is a beginner-friendly collection of electronics projects, perfectly suited for hobbyists, parents, kids, parents, and educators looking to level up their hardware skills - GET THE BOOK »

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

Add Your Reply