Usage Example for ESP32-LoRa Wireless Transmission

Last revision 2026/01/06

This article details the setup process for ESP32 and LoRa modules, including hardware and software preparation, wiring diagrams, pin configurations, and sample code for data transmission and reception.

Hardware Preparation

Software Preparation

Wiring Diagram

TEL0121-Wiring diagram

Connection:

  • Joint ESP32 board and LoRa module together.
  • Connect ESP32 board to PC via USB line.

Other Preparation Work

Pin Connection Configuration Guide

Item Details
Default Pin Connections CS Pin → D4, RESET Pin → D2
Custom Pin Assignment Method Modify the program and specify other IO interfaces by passing parameters to the initialization function
Initialization Function Prototype init(uint8_t NSSPin = NSS_PIN, uint8_t NRESETPin = RESET_PIN)

Supplementary Notes:

  • NSSPin/NRESETPin: Function input parameters for specifying custom CS/RESET pins.
  • NSS_PIN/RESET_PIN: Default macro definitions corresponding to the default pins D4/D2.

Sample Code

Download the master program and the slave program to two ESP32 board respectively.

The Slave program code: LoRa module receives data, prints data/RSSI and toggles onboard LED.

/*!
 * @file receiverTest.ino
 * @brief DFRobot's received data
 * @n [Get the module here]
 * @n This example is receive.
 * @n [Connection and Diagram]
 *
 * @copyright   [DFRobot](https://www.dfrobot.com), 2016
 * @copyright   GNU Lesser General Public License
 *
 * @author [yangyang]
 * @version  V1.0
 * @date  2017-04-10
 */

#include <DFRobot_LoRa.h>

DFRobot_LoRa lora;

uint8_t len;
uint8_t rxBuf[32];

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

    pinMode(LED_BUILTIN, OUTPUT);
    Serial.println("Receiver Test");

    while(!lora.init()) {
        Serial.println("Starting LoRa failed!");
        delay(100);
    }

    lora.rxInit();
}
void loop()
{
    if(lora.waitIrq()) {   // wait for RXDONE interrupt
        lora.clearIRQFlags();
        len = lora.receivePackage(rxBuf);  // receive data
        Serial.write(rxBuf, len);
        Serial.println();
        lora.rxInit();    // wait for packet from master

        // print RSSI of packet
        Serial.print("with RSSI ");
        Serial.println(lora.readRSSI());

        static uint8_t i;
        i = ~i;
        digitalWrite(LED_BUILTIN, i);
    }
}

The Master program code: LoRa module sends specified data cyclically, counts and prints send times

/*!
 * @file sendTest.ino
 * @brief DFRobot's send data
 * @n [Get the module here]
 * @n This example is send.
 * @n [Connection and Diagram]
 *
 * @copyright   [DFRobot](https://www.dfrobot.com), 2016
 * @copyright   GNU Lesser General Public License
 *
 * @author [yangyang]
 * @version  V1.0
 * @date  2017-04-10
 */

#include <DFRobot_LoRa.h>

DFRobot_LoRa lora;

uint8_t counter = 0;
uint8_t sendBuf[] = "HelloWorld!";

/* The default pin:
 *      SS:D4
 *      RST:D2 (If you are using the FireBeetle Board-ESP8266 motherboard controller, the RST defaults to D3)
 */

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

    while(!lora.init()) {
        Serial.println("Starting LoRa failed!");
        delay(100);
    }
}
void loop()
{
    Serial.print("Sending packet: ");
    Serial.println(counter);

    // send packet
    lora.sendPackage(sendBuf, 11); // sending data
    lora.idle();    // turn to standby mode

    counter++;
#if 0
    if(counter%10 == 0) {
        lora.sleep();
        delay (5000);// sleep 5 seconds
    }
#endif

    delay(500);
}

Result

Two ESP32 motherboards successfully communicate and receive data.

TEL0121-The master sends data

TEL0121-The slave receives data

Was this article helpful?

TOP