Quick & Easy: Setting up WaveShare RP2040-LoRa-LF and Lora-HF Module in Arduino

Setup Arduino and Board

  • Install Arduino 2
  • Open Preferences (Ctrl + Comma)
  • Add to list https://github.com/earlephilhower/arduino-pico/releases/download/4.1.1/package_rp2040_index.json
  • Let it update
  • Open Board Manager (Ctrl + Shift + B)
  • Search for "Earle" and then install the lastest version of the library
  • Go to Tools > Board > Raspberry Pi Pico/RP2040/RP2350 > WaveShare RP2040 One
  • Open Library Manger (Ctrl + Shift + I)
  • Search for RadioLib and Click on Install
  • Flash the Sketches and Open Serial Monitor for both boards

Transmit Sketch

// https://nwgat.ninja/rp2040-lora
// Transmit

#include <SPI.h>
#include <RadioLib.h>

// SPI definition
#define LORA_SCK     14  // GP10
#define LORA_MISO    24   // GP8
#define LORA_MOSI    15  // GP11
#define LORA_SS      13  // GP9
#define LORA_RST     23  // GP23
#define LORA_DIO1    16  // GP16
#define LORA_BUSY    18  // GP18
#define LORA_ANT_SW  17  // GP17

SX1262 radio = new Module(LORA_SS, LORA_DIO1, LORA_RST, LORA_BUSY, SPI1);

//----------------------------------
void setup() {
  Serial.begin(9600);
  while (!Serial);

  // SPI configuration
  SPI1.setRX(LORA_MISO);
  SPI1.setTX(LORA_MOSI);
  SPI1.setSCK(LORA_SCK);
  SPI1.begin(false);

  // SX1262 init, Change 433.0 to whatever frequency your module support
  int state = radio.begin(433.0, 125.0, 12, 5, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 17, 14, 0);
  if (state != RADIOLIB_ERR_NONE) {
    Serial.print("LoRa init failed, Error code : ");
    Serial.println(state);
    while (true);
  }
  Serial.println("LoRa Ok.");
}

void loop() {
  Serial.println("Send message...");
  int state = radio.transmit("Hello, LoRa Ninja!");
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println("Transmission ok.");
  } else {
    Serial.print("Transmission error: ");
    Serial.println(state);
  }
  delay(5000);
}

Receive Sketch

// https://nwgat.ninja/rp2040-lora
// Receive

#include <RadioLib.h>
#include <SPI.h>

// SPI definition
#define LORA_SCK     14  // GP10
#define LORA_MISO    24   // GP8
#define LORA_MOSI    15  // GP11
#define LORA_SS      13  // GP9
#define LORA_RST     23  // GP23
#define LORA_DIO1    16  // GP16
#define LORA_BUSY    18  // GP18
#define LORA_ANT_SW  17  // GP17

SX1262 radio = new Module(LORA_SS, LORA_DIO1, LORA_RST, LORA_BUSY, SPI1);

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

    // SPI configuration
  SPI1.setRX(LORA_MISO);
  SPI1.setTX(LORA_MOSI);
  SPI1.setSCK(LORA_SCK);
  SPI1.begin(False);

  // initialize SX1262, Change 433.0 to whatever frequency your module support
  Serial.print(F("[SX1262] Initializing ... "));
  int state = radio.begin(433.0, 125.0, 12, 5, RADIOLIB_SX126X_SYNC_WORD_PRIVATE, 17, 14, 0);
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println(F("success!"));
  } else {
    Serial.print(F("failed, code "));
    Serial.println(state);
    while (true) { delay(10); }
  }

  // set the function that will be called
  // when new packet is received
  radio.setPacketReceivedAction(setFlag);

  // start listening for LoRa packets
  Serial.print(F("[SX1262] Starting to listen ... "));
  state = radio.startReceive();
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println(F("success!"));
  } else {
    Serial.print(F("failed, code "));
    Serial.println(state);
    while (true) { delay(10); }
  }

}

volatile bool receivedFlag = false;

#if defined(ESP8266) || defined(ESP32)
  ICACHE_RAM_ATTR
#endif
void setFlag(void) {
  // we got a packet, set the flag
  receivedFlag = true;
}

void loop() {
  // check if the flag is set
  if(receivedFlag) {
    // reset flag
    receivedFlag = false;

    // you can read received data as an Arduino String
    String str;
    int state = radio.readData(str);


    if (state == RADIOLIB_ERR_NONE) {
      // packet was successfully received
      Serial.println(F("[SX1262] Received packet!"));

      // print data of the packet
      Serial.print(F("[SX1262] Data:\t\t"));
      Serial.println(str);

      // print RSSI (Received Signal Strength Indicator)
      Serial.print(F("[SX1262] RSSI:\t\t"));
      Serial.print(radio.getRSSI());
      Serial.println(F(" dBm"));

      // print SNR (Signal-to-Noise Ratio)
      Serial.print(F("[SX1262] SNR:\t\t"));
      Serial.print(radio.getSNR());
      Serial.println(F(" dB"));

      // print frequency error
      Serial.print(F("[SX1262] Frequency error:\t"));
      Serial.print(radio.getFrequencyError());
      Serial.println(F(" Hz"));

    } else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
      // packet was received, but is malformed
      Serial.println(F("CRC error!"));

    } else {
      // some other error occurred
      Serial.print(F("failed, code "));
      Serial.println(state);

    }
  }
}

Notes

  • Must use SPI1 as SPI output
  • SPI1.begin must be set to (false)
  • Frequency can be set based on what board you use