XIAO ESP32S3 & Wio-SX1262 arduino ide LoRa error

The Meshtastic features work well; however, I can’t use it for a simple P2P LoRa connection. I am using the simple LoRa code example to transmit and receive. Ended up with LoRa not initialized error. Do I need to define pins first?

#include <SPI.h>

#include <LoRa.h>

int counter = 0;

void setup() {

Serial.begin(9600);

while (!Serial);

Serial.println(“LoRa Sender”);

if (!LoRa.begin(915E6)) {

Serial.println("Starting LoRa failed!");

while (1);

}

}

void loop() {

Serial.print("Sending packet: ");

Serial.println(counter);

// send packet

LoRa.beginPacket();

LoRa.print("hello ");

LoRa.print(counter);

LoRa.endPacket();

counter++;

delay(5000);

}

HI there,

YEP… in a word :smile: :+1: seems as it works best that way, add some code to verify the pins You asign are the ones it knows , ie. CS in cases of SPI, or SS in Flash or SD card code. better to know for sure than jump without the Net , " Capisce :pinched_fingers:"

Best question of the week. IMO… also check @msfujino posts on the P2P scene.

HTH
GL :slight_smile: PJ :v:

ps. can you use the code tags and fix that, " </> " above just paste it in there so it’s readable, TNX. you’ll get better help too that way. :v:

If your LoRa module does not use the default pins (or if you’re unsure), you can define the pins for the LoRa module manually before initializing it. You can do this by using LoRa.setPins() in your setup function.

im supposed to get one for christmas

Thanks for the comments. Tried out several pins, had a seeed ranger helped out. Managed to create transmit and receiving code, both Lora initialised. One was transmitting, but the receiver did not receive the message. No idea what went wrong, but here are the codes for the transmit.

#include <Arduino.h>
#include <SX126x-Arduino.h>
#include <SPI.h>

// Pin Definitions (based on Seeed Studio Wiki)
const int LORA_RESET_PIN = 12;  // RESET
const int LORA_DIO1_PIN = 14;   // DIO1
const int LORA_BUSY_PIN = 13;   // BUSY
const int LORA_NSS_PIN = 15;    // NSS / CS
const int LORA_SCLK_PIN = 6;    // SCK
const int LORA_MISO_PIN = 8;    // MISO
const int LORA_MOSI_PIN = 7;    // MOSI

// LoRa Settings
#define RF_FREQUENCY 868E6   // Asia Frequency
#define TX_OUTPUT_POWER 22   // dBm

// Buffers
#define BUFFER_SIZE 64
static uint8_t TxdBuffer[BUFFER_SIZE];

// LoRa Radio Events
static RadioEvents_t RadioEvents;
hw_config hwConfig;

// Function Declarations
void OnTxDone(void);
void OnTxTimeout(void);

// LoRa Initialization
void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println("Initializing LoRa Transmitter...");

  // Configure LoRa Hardware
  hwConfig.CHIP_TYPE = SX1262_CHIP;
  hwConfig.PIN_LORA_RESET = LORA_RESET_PIN;
  hwConfig.PIN_LORA_NSS = LORA_NSS_PIN;
  hwConfig.PIN_LORA_SCLK = LORA_SCLK_PIN;
  hwConfig.PIN_LORA_MISO = LORA_MISO_PIN;
  hwConfig.PIN_LORA_MOSI = LORA_MOSI_PIN;
  hwConfig.PIN_LORA_DIO_1 = LORA_DIO1_PIN;
  hwConfig.PIN_LORA_BUSY = LORA_BUSY_PIN;

  // Initialize LoRa Hardware
  lora_hardware_init(hwConfig);

  // Setup Radio Event Callbacks
  RadioEvents.TxDone = OnTxDone;
  RadioEvents.TxTimeout = OnTxTimeout;

  // Initialize the Radio
  Radio.Init(&RadioEvents);
  Radio.SetChannel(RF_FREQUENCY);

  // Configure TX Settings
  Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, 0, 7, 1, 8, false, true, 0, 0, false, 3000);

  Serial.println("LoRa Transmitter Initialized.");
}

void loop() {
  // Prepare and Send Data
  const char* message = "Hello LoRa!";
  memset(TxdBuffer, 0, BUFFER_SIZE);
  strncpy((char*)TxdBuffer, message, BUFFER_SIZE);

  Serial.printf("Sending Message: %s\n", message);
  Radio.Send(TxdBuffer, strlen(message));

  // Wait before sending the next message
  delay(3000);  // Adjust this value as needed
}

// Event Callbacks
void OnTxDone(void) {
  Serial.println("Transmission Complete.");
}

void OnTxTimeout(void) {
  Serial.println("Transmission Timeout.");
}

and here is the receiving code

#include <Arduino.h>
#include <SX126x-Arduino.h>
#include <SPI.h>

// Pin Definitions
const int LORA_RESET_PIN = 12;  // RESET
const int LORA_DIO1_PIN = 14;   // DIO1
const int LORA_BUSY_PIN = 13;   // BUSY
const int LORA_NSS_PIN = 15;    // NSS / CS
const int LORA_SCLK_PIN = 6;    // SCK
const int LORA_MISO_PIN = 8;    // MISO
const int LORA_MOSI_PIN = 7;    // MOSI

// LoRa Settings
#define RF_FREQUENCY 868E6   // Asia Frequency
#define RX_TIMEOUT_VALUE 3000

// Buffers
#define BUFFER_SIZE 64
static uint8_t RcvBuffer[BUFFER_SIZE];
static uint16_t BufferSize = BUFFER_SIZE;

// LoRa Radio Events
static RadioEvents_t RadioEvents;
hw_config hwConfig;

// Function Declarations
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
void OnRxTimeout(void);
void OnRxError(void);

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

  Serial.println("Initializing LoRa Receiver...");

  // Configure LoRa Hardware
  hwConfig.CHIP_TYPE = SX1262_CHIP;
  hwConfig.PIN_LORA_RESET = LORA_RESET_PIN;
  hwConfig.PIN_LORA_NSS = LORA_NSS_PIN;
  hwConfig.PIN_LORA_SCLK = LORA_SCLK_PIN;
  hwConfig.PIN_LORA_MISO = LORA_MISO_PIN;
  hwConfig.PIN_LORA_MOSI = LORA_MOSI_PIN;
  hwConfig.PIN_LORA_DIO_1 = LORA_DIO1_PIN;
  hwConfig.PIN_LORA_BUSY = LORA_BUSY_PIN;

  // Initialize LoRa Hardware
  lora_hardware_init(hwConfig);

  // Setup Radio Event Callbacks
  RadioEvents.RxDone = OnRxDone;
  RadioEvents.RxTimeout = OnRxTimeout;
  RadioEvents.RxError = OnRxError;

  // Initialize the Radio
  Radio.Init(&RadioEvents);
  Radio.SetChannel(RF_FREQUENCY);

  // Configure RX Settings
  Radio.SetRxConfig(MODEM_LORA, 0, 7, 1, 0, 8, 0, false, 0, true, 0, 0, false, true);

  Serial.println("LoRa Receiver Initialized. Listening...");
  Radio.Rx(0);
}

void loop() {
  Radio.IrqProcess();
}

// Event Callbacks
void OnRxDone(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr) {
  BufferSize = size;
  memcpy(RcvBuffer, payload, BufferSize);

  Serial.printf("Received Message: %.*s\n", size, payload);
  Serial.printf("RSSI: %d dBm | SNR: %d\n", rssi, snr);

  // Resume Listening
  Radio.Rx(0);
}

void OnRxTimeout(void) {
  Serial.println("Receive Timeout.");
  Radio.Rx(0);
}

void OnRxError(void) {
  Serial.println("Receive Error.");
  Radio.Rx(0);
}

Update 1. @Hendra Kusumah suggested using [](GitHub - chandrawi/LoRaRF-Arduino: Arduino library for basic transmitting and receiving data using LoRa and FSK modulation . He tested on Heltec V3 (which uses ESP32S3 and the same SX1262.
While it worked for the Heltec module, it did not work for this one. It seems the transmitter seems to be transmitting fine. The receiver did not.