Node to Node communication on LoRa with Arduino Nano and ESP32

Node to Node communication on LoRa with Arduino Nano and ESP32

What is LoRa?

LoRa, short for long-range, is the physical layer or the wireless modulation utilized to create a long-range communication link. LoRa is based on Chirp Spread Spectrum (CSS) modulation, which maintains the same low power characteristics as FSK modulation but significantly increases the communication range.

Material Required is as follows:

  1. Arduino Nano
  2. ESP32
  3. 2x LoRa Bee modules
  4. 4x 3.2K ohm Resistor
  5. 74LVC125A IC
  6. Jumper Wires

Circuit Diagram

circuit diagram of LoRa with Arduino Nano and ESP32

circuit diagram of esp

Pin Details

ESP32

LoRa BEE

LoRa BEE

physical pin

ESP32 Pin

MISO

  4

        D19(MISO)

 

RESET

 5

        D14

MOSI

11

        D23(MOSI)

D0  

 12

        D2

NSS

 17

        D5

SCK

 18

        D22(SCLK)

3V3

  1

         3V3

GND

 10

         GND

 Arduino Nano

LoRa BEE

LoRa BEE

physical pin

74LVC125A

Arduino Nano

MISO

  4

       12

 

       ----

RESET

 5

       ----

       D10

MOSI

11

         3

       ----

D0  

 12

      -----

       D2

NSS

 17

         8

       ----

SCK

 18

        6

       ----

3V3

  1

      14

      ----

GND

 10

       7

      ----

 

Arduino Nano

74LVC125A

MISO

        11

MOSI

         2

NSS

         9

SCK

         5

3V3

        14

GND

          7

 

For node to node communication, the Nano will be configured as the Transmitter and the ESP32 will be configured as the receiver.

Step 1: Installing ESP32 Add-on in Arduino IDE

To install the ESP32 board in your Arduino IDE, follow below steps.

1. In your Arduino IDE, go to File> Preferences.

2. Enter https://dl.espressif.com/dl/package_esp32_index.json into the “Additional Board Manager URLs” field as shown in the figure below. Then, click the “OK” button.

    3. Go to Tools > Board > Boards Manager

       

      4. Search for “ESP32” and Click on the install button

        5. The installation may take a while depending on your network performance. Once done, close the board manager

           

          Step 2: Code and Library

          Download as Zip file of Arduino-LoRa Library refer below link.

          https://www.arduino.cc/reference/en/libraries/lora/

          Installing library

          1. In the Arduino IDE, navigate to Sketch > Include Library > Add .ZIP Library.

             

            2. At the top of the drop-down list, select the option to "Add. ZIP Library''.

              Step 3: Transmission Code

              1. In your Arduino IDE, go to Tools > Board > Arduino Nano.

                2. Type in the code below and click on the Upload button to upload the code in the Arduino IDE.

                 

                  #include <SPI.h>

                  #include <LoRa.h>

                  //define the pins used by the transceiver module

                  #define ss 10

                  #define rst 9

                  #define dio0 2

                   

                  int counter = 0;

                   

                  void setup() {

                    //initialize Serial Monitor

                    Serial.begin(115200);

                    while (!Serial);

                    Serial.println("LoRa Sender");

                   

                    //setup LoRa transceiver module

                    LoRa.setPins(ss, rst, dio0);

                   

                    //replace the LoRa.begin(---E-) argument with your location's frequency

                    //433E6 for Asia

                    //866E6 for Europe

                    //915E6 for North America

                    while (!LoRa.begin(866E6)) {

                      Serial.println(".");

                      delay(500);

                    }

                    // Change sync word (0xF3) to match the receiver

                    // The sync word assures you don't get LoRa messages from other LoRa transceivers

                    // ranges from 0-0xFF

                    LoRa.setSyncWord(0xF3);

                    Serial.println("LoRa Initializing OK!");

                  }

                   

                  void loop() {

                    Serial.print("Sending packet: ");

                    Serial.println(counter);

                   

                    //Send LoRa packet to the receiver

                    LoRa.beginPacket();

                    LoRa.print("hello ");

                    LoRa.print(counter);

                    LoRa.endPacket();

                   

                    counter++;

                   

                    delay(10000);

                  }

                   

                  3. After uploading code open Arduino serial monitor to observe the data being transmitted.

                     

                    Step 4: Receiver Code

                    1. In your Arduino IDE, go to Tools > Board > ESP32 Dev Module.

                    2. Type in the code below and click on the Upload button to upload the code in the Arduino IDE.


                      #include <SPI.h>

                      #include <LoRa.h>

                       

                      //define the pins used by the transceiver module

                      #define ss 5

                      #define rst 14

                      #define dio0 2

                       

                      void setup() {

                        //initialize Serial Monitor

                        Serial.begin(115200);

                        while (!Serial);

                        Serial.println("LoRa Receiver");

                       

                        //setup LoRa transceiver module

                        LoRa.setPins(ss, rst, dio0);

                       

                        //replace the LoRa.begin(---E-) argument with your location's frequency

                        //433E6 for Asia

                        //866E6 for Europe

                        //915E6 for North America

                        while (!LoRa.begin(866E6)) {

                          Serial.println(".");

                          delay(500);

                        }

                         // Change sync word (0xF3) to match the receiver

                        // The sync word assures you don't get LoRa messages from other LoRa transceivers

                        // ranges from 0-0xFF

                        LoRa.setSyncWord(0xF3);

                        Serial.println("LoRa Initializing OK!");

                      }

                       

                      void loop() {

                        // try to parse packet

                        int packetSize = LoRa.parsePacket();

                        if (packetSize) {

                          // received a packet

                          Serial.print("Received packet '");

                       

                          // read packet

                          while (LoRa.available()) {

                            String LoRaData = LoRa.readString();

                            Serial.print(LoRaData);

                          }

                       

                          // print RSSI of packet

                          Serial.print("' with RSSI ");

                          Serial.println(LoRa.packetRssi());

                        }

                      }

                      1. After uploading code open Arduino serial monitor to observe the data being received.
                      2.  

                        1. In receiver end, you can see the received packet

                         

                        Troubleshooting

                        After uploading   code if you are not receiving any packet into the receiver node follow the below steps

                        1. Make sure both node sender and receiver are  working the same

                          Frequency. You can check so, by making sure the below line is the same in both receiver and transmitter code

                          while (!LoRa.begin(866E6))

                          2. Make sure the baud rate is set to 115200 in the Serial monitor for both the devices

                          Frequently Asked Questions

                          Back to blog

                          Leave a comment

                          Please note, comments need to be approved before they are published.

                          You may also like to read