Establishing P2P communication between two LoRa sx1262 modules installed on a Raspberry Pi Pico

Hi everyone,

I am currently working on a project involving a Raspberry Pi Pico and a LoRa SX1262 module. The goal is to establish communication between the Pico and the LoRa module using C++.

Here’s what I’ve done so far:

  1. Setup and Development Environment:
  • I am using the Pico SDK with CMake for building the project.
  • The code compiles and builds successfully without any errors. A .uf2 file is generated as expected.
  1. Uploading the Code:
  • I copy the .uf2 file to the Pico by entering bootloader mode.
  • After uploading, the Pico disconnects and re-enumerates.
  1. Issues:
  • When I open the serial monitor using the screen command on my Linux terminal, I see no output from the program.
  • The test program works perfectly, so I know the cable, power, and serial setup are not the issue.
  1. MicroPython Test Results:
  • I previously tested communication between the Pico and SX1262 module using MicroPython, and it worked as expected. Meaning the hardware connections and the module are functional.

Test code that works as expected

#include <pico/stdlib.h>
#include <stdio.h>

int main() {
    stdio_init_all();  // Initialize stdio (USB)
    while (true) {
        printf("Hello, World!\n");
        sleep_ms(1000);
    }
    return 0;
}

Test code that doesn’t work

// define pins to be used
#define SPI_PORT spi0
#define SPI_MISO 12 
#define SPI_MOSI 11
#define SPI_SCK 10

#define RFM_NSS 3 
#define RFM_RST 15	
#define RFM_DIO0 20	
#define RFM_DIO1 21	
#include <pico/stdlib.h>

// include the library
#include <RadioLib.h>

// include the hardware abstraction layer
#include "hal/RPiPico/PicoHal.h"


// create a new instance of the HAL class
PicoHal* hal = new PicoHal(SPI_PORT, SPI_MISO, SPI_MOSI, SPI_SCK);

// now we can create the radio module
// NSS pin:  26
// DIO0 pin:  14
// RESET pin:  22
// DIO1 pin:  15

SX1276 radio = new Module(hal, RFM_NSS, RFM_DIO0, RFM_RST, RFM_DIO1);

int main() {
	spi_init(SPI_PORT, 1000*1000);
	gpio_set_function(SPI_MISO, GPIO_FUNC_SPI);
	gpio_set_function(SPI_MOSI, GPIO_FUNC_SPI);
	gpio_set_function(SPI_SCK, GPIO_FUNC_SPI);
	// initialize just like with Arduino
  printf("[SX1276] Initializing ... ");
  int state = radio.begin();
  if (state != RADIOLIB_ERR_NONE) {
    printf("failed, code %d\n", state);
    return(1);
  }
  printf("success!\n");

  // loop forever
  for(;;) {
    // send a packet
    printf("[SX1276] Transmitting packet ... ");
    state = radio.transmit("Hello World!");
    if(state == RADIOLIB_ERR_NONE) {
      // the packet was successfully transmitted
      printf("success!\n");

      // wait for a second before transmitting again
      hal->delay(500);

    } else {
      printf("failed, code %d\n", state);

    }

  }

  return(0);
}

What I Need Help With:

  • How can I debug this issue further?
  • Are there common pitfalls when using RadioLib with the SX1262 module that I might have overlooked?
  • Is there a way to verify that the SX1262 module is being initialized properly in my code?
  • If anyone has working demo code for this setup, that would be incredibly helpful!

The Pinout diagram of the LoRa module, found here.

Please check if you have a sufficient power supply. Use an external power supply if needed.

Just a quick look here.

The way I would debug things like this is to comment out everything but the basic print and loop.

Then if that works ok, uncomment on thing at a time until it breaks.

Side note: Im not a fan of setting up anything (except a very basic value assign) in global code. Rather, define the global variables, then create an init or setup function to create the objects pointers etc.

Looking at your code, a quick question

SX1276 radio = new Module(hal, RFM_NSS, RFM_DIO0, RFM_RST, RFM_DIO1);

Sould that be
SX1276 radio =
OR
SX1276 *radio =

Normally “new” returns points to the created object.

Thanks for the suggestion.
I managed to get an error code by making a small change in the code. I don’t believe that the power supply is an issue but will verify this later.

Thank you for your suggestion!

Initially, I wasn’t getting any output, which led me to suspect that the pin configuration might be incorrect. Regarding your question, I have to admit that I’m not entirely sure. The code I’m using is a demo program from the RadioLib repository.

Your question made me realize something important: the LoRa module I’m working with is an SX1262, but the demo code was set up for an SX1276. I’ve corrected that and now receive an error code, so I’ll be troubleshooting that next.

Thanks again for your help!

1 Like

Hi @Caracarn,

Welcome to the forum! Glad to see that you’re already making a bit of progress here. Let us know if you run into anymore speedbumps in the meantime.

1 Like