English
English
简体中文
日本語

Arduino Quick Start

2. Devices & Examples

5. Extensions

6. Applications

Stamp LoRa-1262 IF Arduino Tutorial

1. Preparation

2. Notes

Antenna Connection
Connect a compatible antenna before using the LoRa module. Do not transmit without an antenna connected, as this may permanently damage the RF circuitry.
FPC Pin Order
Stamp-C5 DIP connects to Stamp LoRa-1262 IF using a 0.5 mm, 12P FPC cable. Check the connector orientation and Pin1 position before inserting the cable. An incorrect pin order may damage the hardware.
Power Supply
The FPC interface on Stamp-C5 DIP supplies 3.3V to the LoRa module. Current consumption increases significantly during LoRa transmission, so a stable USB or external 5V power supply is recommended.
Pin Compatibility
Stamp LoRa-1262 IF communicates over SPI. The examples below use Stamp C5 as the main controller. When using another controller or carrier board, update the pin definitions in the code to match the actual wiring.

The signal connections between Stamp-C5 DIP and Stamp LoRa-1262 IF are as follows:

Stamp LoRa-1262 IF Pin Stamp-C5 GPIO
SX_ANT_SW G23
LORA_IRQ G0
SX_BUSY G24
SX_NRST G25
GND GND
SPI_MISO G26
SPI_MOSI G27
SX_NSS G11
GND GND
SPI_CLK G12

3. Example Programs

This example uses two sets of Stamp-C5 DIP and Stamp LoRa-1262 IF for LoRa point-to-point communication. The transmitter sends strings containing a counter, and the receiver outputs the received data and signal information over the serial port.

The LoRa parameters are as follows:

Macro Default Description
LORA_FREQ 868.0f Carrier frequency in MHz
LORA_BW 125.0f Bandwidth in kHz
LORA_SF 12 Spreading factor
LORA_CR 5 Coding rate, corresponding to 4/5
LORA_SYNC_WORD 0x34 Sync word for identifying matching LoRa packets
LORA_TX_POWER 22 Transmit power in dBm
LORA_PREAMBLE 20 Preamble length in symbols

The transmitter and receiver should use the same frequency, bandwidth, spreading factor, coding rate, sync word, and preamble length.

3.1 Transmitter

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#include <SPI.h>
#include <RadioLib.h>

// Stamp-C5 DIP FPC to Stamp LoRa-1262 IF connections.
#define LORA_MOSI_PIN   GPIO_NUM_27
#define LORA_MISO_PIN   GPIO_NUM_26
#define LORA_SCK_PIN    GPIO_NUM_12
#define LORA_NSS_PIN    GPIO_NUM_11
#define LORA_BUSY_PIN   GPIO_NUM_24
#define LORA_IRQ_PIN    GPIO_NUM_0
#define LORA_RST_PIN    GPIO_NUM_25
#define LORA_ANT_SW_PIN GPIO_NUM_23

// These settings must match on the transmitter and receiver.
#define LORA_FREQ       868.0f
#define LORA_BW         125.0f
#define LORA_SF         12
#define LORA_CR         5
#define LORA_SYNC_WORD  0x34
#define LORA_TX_POWER   22
#define LORA_PREAMBLE   20

SX1262 radio = new Module(LORA_NSS_PIN, LORA_IRQ_PIN, RADIOLIB_NC, LORA_BUSY_PIN);

volatile bool transmittedFlag = false;
int transmissionState = RADIOLIB_ERR_NONE;
uint32_t packetCount = 0;

void IRAM_ATTR setFlag()
{
    transmittedFlag = true;
}

bool initRadio()
{
    // Select the transmit path for the sender.
    // SX_ANT_SW is active high on the S014-IF RF path.
    pinMode(LORA_ANT_SW_PIN, OUTPUT);
    digitalWrite(LORA_ANT_SW_PIN, HIGH);

    // Reset manually because the Module reset pin is RADIOLIB_NC.
    pinMode(LORA_RST_PIN, OUTPUT);
    digitalWrite(LORA_RST_PIN, LOW);
    delay(10);
    digitalWrite(LORA_RST_PIN, HIGH);
    delay(10);

    SPI.begin(LORA_SCK_PIN, LORA_MISO_PIN, LORA_MOSI_PIN, LORA_NSS_PIN);
    const int state = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR,
                                  LORA_SYNC_WORD, LORA_TX_POWER, LORA_PREAMBLE,
                                  3.0f, true);
    if (state != RADIOLIB_ERR_NONE) {
        Serial.printf("[SX1262] init failed, code: %d\n", state);
        return false;
    }
    radio.setPacketSentAction(setFlag);
    return true;
}

void setup()
{
    Serial.begin(115200);
    delay(1000);
    Serial.println(F("Stamp C5-DIP LoRa-1262 IF transmitter"));

    if (!initRadio()) {
        while (true) delay(1000);
    }

    Serial.println(F("[SX1262] init success"));
    transmissionState = radio.startTransmit("Transmitter Ready");
}

void loop()
{
    if (!transmittedFlag) return;
    transmittedFlag = false;

    if (transmissionState == RADIOLIB_ERR_NONE) {
        Serial.println(F("[SX1262] transmission finished"));
    } else {
        Serial.printf("[SX1262] transmit failed, code: %d\n", transmissionState);
    }

    radio.finishTransmit();
    delay(1000);

    String payload = "Stamp C5-DIP LoRa-1262 #" + String(packetCount++);
    Serial.printf("[SX1262] sending: %s\n", payload.c_str());
    transmissionState = radio.startTransmit(payload);
}

3.2 Receiver

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#include <SPI.h>
#include <RadioLib.h>

// Stamp-C5 DIP FPC to Stamp LoRa-1262 IF connections.
#define LORA_MOSI_PIN   GPIO_NUM_27
#define LORA_MISO_PIN   GPIO_NUM_26
#define LORA_SCK_PIN    GPIO_NUM_12
#define LORA_NSS_PIN    GPIO_NUM_11
#define LORA_BUSY_PIN   GPIO_NUM_24
#define LORA_IRQ_PIN    GPIO_NUM_0
#define LORA_RST_PIN    GPIO_NUM_25
#define LORA_ANT_SW_PIN GPIO_NUM_23

// These settings must match on the transmitter and receiver.
#define LORA_FREQ       868.0f
#define LORA_BW         125.0f
#define LORA_SF         12
#define LORA_CR         5
#define LORA_SYNC_WORD  0x34
#define LORA_TX_POWER   22
#define LORA_PREAMBLE   20

SX1262 radio = new Module(LORA_NSS_PIN, LORA_IRQ_PIN, RADIOLIB_NC, LORA_BUSY_PIN);

volatile bool receivedFlag = false;

void IRAM_ATTR setFlag()
{
    receivedFlag = true;
}

bool initRadio()
{
    pinMode(LORA_ANT_SW_PIN, OUTPUT);
    // Select the receive path for the receiver.
    digitalWrite(LORA_ANT_SW_PIN, LOW);

    // Reset manually because the Module reset pin is RADIOLIB_NC.
    pinMode(LORA_RST_PIN, OUTPUT);
    digitalWrite(LORA_RST_PIN, LOW);
    delay(10);
    digitalWrite(LORA_RST_PIN, HIGH);
    delay(10);

    SPI.begin(LORA_SCK_PIN, LORA_MISO_PIN, LORA_MOSI_PIN, LORA_NSS_PIN);
    const int state = radio.begin(LORA_FREQ, LORA_BW, LORA_SF, LORA_CR,
                                  LORA_SYNC_WORD, LORA_TX_POWER, LORA_PREAMBLE,
                                  3.0f, true);
    if (state != RADIOLIB_ERR_NONE) {
        Serial.printf("[SX1262] init failed, code: %d\n", state);
        return false;
    }
    radio.setPacketReceivedAction(setFlag);
    return true;
}

void setup()
{
    Serial.begin(115200);
    delay(1000);
    Serial.println(F("Stamp C5-DIP LoRa-1262 IF receiver"));

    if (!initRadio()) {
        while (true) delay(1000);
    }

    Serial.println(F("[SX1262] init success"));
    const int state = radio.startReceive();
    if (state != RADIOLIB_ERR_NONE) {
        Serial.printf("[SX1262] receive start failed, code: %d\n", state);
        while (true) delay(1000);
    }
    Serial.println(F("[SX1262] listening"));
}

void loop()
{
    if (!receivedFlag) return;
    receivedFlag = false;

    String payload;
    const int state = radio.readData(payload);
    if (state == RADIOLIB_ERR_NONE) {
        Serial.println(F("[SX1262] packet received"));
        Serial.printf("[SX1262] data: %s\n", payload.c_str());
        Serial.printf("[SX1262] RSSI: %.2f dBm\n", radio.getRSSI());
        Serial.printf("[SX1262] SNR: %.2f dB\n", radio.getSNR());
        Serial.printf("[SX1262] frequency error: %.2f Hz\n", radio.getFrequencyError());
    } else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
        Serial.println(F("[SX1262] CRC error"));
    } else {
        Serial.printf("[SX1262] receive failed, code: %d\n", state);
    }

    radio.finishReceive();
    radio.startReceive();
}

4. Compile and Upload

  • Select the device port, paste the example program above, and click the Upload button in the upper-left corner of the Arduino IDE. Wait for the program to compile and upload to the device.

5. LoRa Transmission and Reception Test

The transmitter sends a string containing a counter once per second. The receiver prints the received string and displays information such as RSSI.

  • Example serial output from the transmitter:
Stamp C5-DIP LoRa-1262 IF transmitter
[SX1262] init success
[SX1262] transmission finished
[SX1262] sending: Stamp C5-DIP LoRa-1262 #0
  • Example serial output from the receiver:
Stamp C5-DIP LoRa-1262 IF receiver
[SX1262] init success
[SX1262] listening
[SX1262] packet received
[SX1262] data: Stamp C5-DIP LoRa-1262 #0
[SX1262] RSSI: -42.00 dBm
[SX1262] SNR: 9.25 dB
Page Tools
PDF
On This Page