jgromes / RadioLib

Universal wireless communication library for embedded devices
https://jgromes.github.io/RadioLib/
MIT License
1.48k stars 375 forks source link

Raspberry Pico + SX 1262 #729

Closed Steffen54321 closed 1 year ago

Steffen54321 commented 1 year ago

Hi,

I am new to the LoRa/ microcontroller business and try to make 2 raspberry pico to communicate via LoRa or FSK. Therfore i bought a Waveshare SX1262 Shield (https://www.waveshare.com/pico-lora-sx1262-868m.htm) and use the Arduino IDE with Arduino MBED RP2040 Board chosen. When compiling the SX126X examples of RadioLib, I get the failed, code -2. I changed the Ports as shown in the waveshare link (SX1262 radio = new Module(5, 26, 20, 4);) What am I doing wrong? Thanks alot in advance!

jgromes commented 1 year ago

I don't have either the RP2040 or the waveshare module, but looking at the pinout diagram, I think you may have used the physical device pin numbers (1 - 40) as opposed to the actual GPIO pin number (GPxy). Can you try Module(3, 20, 15, 2);?

Steffen54321 commented 1 year ago

Thanks for the fast response! But i still get: failed, code -2

jgromes commented 1 year ago

In that case please fill in the "module not working" template, especially post the debug output.

Steffen54321 commented 1 year ago

Sketch that is causing the module fail

//SX126x_Transmit Example Code
// include the library
#include <RadioLib.h>

// SX1262 has the following connections:
// NSS pin:   10      3
// DIO1 pin:  2       20
// NRST pin:  3       15
// BUSY pin:  9       2  
SX1262 radio = new Module(3, 20, 15, 2);

// or using RadioShield
// https://github.com/jgromes/RadioShield
//SX1262 radio = RadioShield.ModuleA;

// or using CubeCell
//SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);

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

  // initialize SX1262 with default settings
  Serial.print(F("[SX1262] Initializing ... "));
  int state = radio.begin();
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println(F("success!"));
  } else {
    Serial.print(F("failed, code "));
    Serial.println(state);
    while (true);
  }

  // some modules have an external RF switch
  // controlled via two pins (RX enable, TX enable)
  // to enable automatic control of the switch,
  // call the following method
  // RX enable:   4
  // TX enable:   5
  /*
    radio.setRfSwitchPins(4, 5);
  */
}

void loop() {
  Serial.print(F("[SX1262] Transmitting packet ... "));

  // you can transmit C-string or Arduino string up to
  // 256 characters long
  // NOTE: transmit() is a blocking method!
  //       See example SX126x_Transmit_Interrupt for details
  //       on non-blocking transmission method.
  int state = radio.transmit("Hello World!");

  // you can also transmit byte array up to 256 bytes long
  /*
    byte byteArr[] = {0x01, 0x23, 0x45, 0x56, 0x78, 0xAB, 0xCD, 0xEF};
    int state = radio.transmit(byteArr, 8);
  */

  if (state == RADIOLIB_ERR_NONE) {
    // the packet was successfully transmitted
    Serial.println(F("success!"));

    // print measured data rate
    Serial.print(F("[SX1262] Datarate:\t"));
    Serial.print(radio.getDataRate());
    Serial.println(F(" bps"));

  } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
    // the supplied packet was longer than 256 bytes
    Serial.println(F("too long!"));

  } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
    // timeout occured while transmitting packet
    Serial.println(F("timeout!"));

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

  }

  // wait for a second before transmitting again
  delay(1000);
}

Hardware setup Raspberry Pi Pico + Waveshare LoRa Header (https://www.waveshare.com/pico-lora-sx1262-868m.htm) + Arduino IDE MBED OS RP2040

Debug mode output Serial: failed, code -2

Additional info

jgromes commented 1 year ago

Please provide the debug mode output as shown on this wiki page: https://github.com/jgromes/RadioLib/wiki/Debug-mode

Steffen54321 commented 1 year ago

What do I have to add to the code to get the Debug informations via Serial? I already uncomment in the BuildOpt.h

jgromes commented 1 year ago

Did you upload the changed version into the RP2040?

Steffen54321 commented 1 year ago

Yes. I uncomment in BuildOpt.h and upload the above example code to the RP2040. The Serial Output is: [SX1262] Initializing ... failed, code -2

jgromes commented 1 year ago

In that case it's looking like at least the BUSY pin is not connected. I suggest to double check the pin numbers listed on the manufacturer site actually correspond to RP2040 pins in Arduino IDE.

GUVWAF commented 1 year ago

I got the RP2040 with SX1262 Waveshare hat to initialize when using SPI1, since that is the interface connected to the pins you need. I didn't test your script, but maybe it works when calling Module(3, 20, 15, 2, SPI1, RADIOLIB_DEFAULT_SPI_SETTINGS).

Steffen54321 commented 1 year ago

Thanks for your response. Sadly, that didnt work. Compilation error: 'SPI1' was not declared in this scope

GUVWAF commented 1 year ago

That's a limitation of ArduinoCore-mbed then, as it seems to only support one SPI interface: https://github.com/arduino/ArduinoCore-mbed/blob/main/variants/RASPBERRY_PI_PICO/pins_arduino.h#L72

I'm using https://github.com/maxgerhardt/platform-raspberrypi with PlatformIO.

jgromes commented 1 year ago

@Steffen54321 it seems like there was no updates to this, so I'm going to have to close this as inactive. Feel free to reopen later if more information becomes available.

MarkusLoew commented 1 year ago

Hi,

I face the same issue with the Waveshare SX1262 Shield (https://www.waveshare.com/pico-lora-sx1262-868m.htm). I'm using the shield with a Raspberry Pi Pico W. I tried SX1262 radio = new Module(3, 20, 15, 2); following the pin out and the advice above.

The last debug error messages are

_SX126x not found! (10 of 10 tries) RADIOLIB_SX126X_REG_VERSIONSTRING: 0000320 01 00 00 00 34 32 00 20 30 33 00 20 01 00 00 00 | ....42. 03. .... Expected string: SX1261 No SX126x found! failed, code -2

Debug output below.

Using Module(3, 20, 15, 2, SPI1, RADIOLIB_DEFAULT_SPI_SETTINGS); as suggested previously compiles and uploads, but results in the same error messages and debug output.

Thanks for your help.

Sketch that is causing the module fail

/*
   RadioLib SX126x Transmit Example

   This example transmits packets using SX1262 LoRa radio module.
   Each packet contains up to 256 bytes of data, in the form of:
    - Arduino String
    - null-terminated char array (C-string)
    - arbitrary binary data (byte array)

   Other modules from SX126x family can also be used.

   For default module settings, see the wiki page
   https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem

   For full API reference, see the GitHub Pages
   https://jgromes.github.io/RadioLib/
*/

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

// SX1262 has the following connections:
// NSS pin:   10
// DIO1 pin:  2
// NRST pin:  3
// BUSY pin:  9
SX1262 radio = new Module(3, 20, 15, 2);

// or using RadioShield
// https://github.com/jgromes/RadioShield
//SX1262 radio = RadioShield.ModuleA;

// or using CubeCell
//SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);

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

  // initialize SX1262 with default settings
  Serial.print(F("[SX1262] Initializing ... "));
  int state = radio.begin();
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println(F("success!"));
  } else {
    Serial.print(F("failed, code "));
    Serial.println(state);
    while (true);
  }

  // some modules have an external RF switch
  // controlled via two pins (RX enable, TX enable)
  // to enable automatic control of the switch,
  // call the following method
  // RX enable:   4
  // TX enable:   5
  /*
    radio.setRfSwitchPins(4, 5);
  */
}

void loop() {
  Serial.print(F("[SX1262] Transmitting packet ... "));

  // you can transmit C-string or Arduino string up to
  // 256 characters long
  // NOTE: transmit() is a blocking method!
  //       See example SX126x_Transmit_Interrupt for details
  //       on non-blocking transmission method.
  int state = radio.transmit("Hello World!");

  // you can also transmit byte array up to 256 bytes long
  /*
    byte byteArr[] = {0x01, 0x23, 0x45, 0x56, 0x78, 0xAB, 0xCD, 0xEF};
    int state = radio.transmit(byteArr, 8);
  */

  if (state == RADIOLIB_ERR_NONE) {
    // the packet was successfully transmitted
    Serial.println(F("success!"));

    // print measured data rate
    Serial.print(F("[SX1262] Datarate:\t"));
    Serial.print(radio.getDataRate());
    Serial.println(F(" bps"));

  } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
    // the supplied packet was longer than 256 bytes
    Serial.println(F("too long!"));

  } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
    // timeout occured while transmitting packet
    Serial.println(F("timeout!"));

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

  }

  // wait for a second before transmitting again
  delay(1000);
}

Hardware setup Raspberry Pi Pico W with attached Waveshare Pico-LoRa-SX1262 node module.

Debug mode output

CMD 80
DATW 0 0

CMD 80
DATW 0 0

CMD 80
DATW 0 0

CMD 80
DATW 0 0

CMD 80
DATW 0 0

CMD 80
DATW 0 0

CMD 80
DATW 0 0

CMD 1D 3 20
DATR 0 0 0 1 0 0 0 0 0 0 0 34 0 32 0 0 0 20 0 30 0 33 0 0 0 20 0 1 0 0 0 0 0 0

SX126x not found! (10 of 10 tries) RADIOLIB_SX126X_REG_VERSION_STRING: 0000320 01 00 00 00 34 32 00 20 30 33 00 20 01 00 00 00 | ....42. 03. .... Expected string: SX1261 No SX126x found! failed, code -2

Additional info (please complete):

GUVWAF commented 1 year ago

I also had to set the correct pins for SPI1 and begin it as follows:

SPI1.setSCK(10);
SPI1.setTX(11);
SPI1.setRX(12);
pinMode(3, OUTPUT);
digitalWrite(3, HIGH);
SPI1.begin(false);
jgromes commented 1 year ago

@MarkusLoew are you using the correct SPI bus? Since you didn't provide any to the Module constructor, RadioLib will assume the default SPI and initialize it. If you want something different, see https://github.com/jgromes/RadioLib/wiki/Basics#non-standard-spi-setup

MarkusLoew commented 1 year ago

Success!

The code from @GUVWAF https://github.com/jgromes/RadioLib/issues/729#issuecomment-1582304326 regarding the non-standard SPI setup did the trick!

Thank you very much for your quick help @GUVWAF and @jgromes , much appreciated!

10:24:03.925 -> Start
10:24:03.925 -> [SX1262] Initializing ... CMD   80  
10:24:03.925 -> DATW    0   AA  
10:24:03.925 -> 
10:24:04.018 -> CMD 80  
10:24:04.018 -> DATW    0   A2  
10:24:04.018 -> 
10:24:04.018 -> CMD C0  
10:24:04.018 -> DATR    0   22  0   22  
10:24:04.018 -> 
10:24:04.018 -> CMD 1D  3   20  
10:24:04.018 -> DATR    0   A2  0   53  0   58  0   31  0   32  0   36  0   31  0   20  0   56  0   32  0   44  0   20  0   32  0   44  0   30  0   32  0   0   
10:24:04.018 -> 
10:24:04.018 -> Found SX126x: RADIOLIB_SX126X_REG_VERSION_STRING:
10:24:04.018 -> 0000320 53 58 31 32 36 31 20 56 32 44 20 32 44 30 32 00 | SX1261 V2D 2D02.
10:24:04.018 -> 
10:24:04.018 -> M   SX126x
10:24:04.018 -> CMD 80  
10:24:04.018 -> DATW    0   AA  
10:24:04.018 -> 
10:24:04.115 -> CMD 80  
10:24:04.115 -> DATW    0   A2  
10:24:04.115 -> 
10:24:04.115 -> CMD C0  
10:24:04.115 -> DATR    0   22  0   22  
10:24:04.146 -> 

[...]

10:24:04.178 -> 
10:24:04.178 -> CMD D   8   E7  
10:24:04.178 -> DATW    18  A2  
10:24:04.178 -> 
10:24:04.178 -> success!
10:24:04.178 -> [SX1262] Waiting for incoming transmission ... CMD  80  
10:24:04.178 -> DATW    0   A2  
10:24:04.178 -> 

[...] Later, I get error code -705, which is probably a different, unrelated issue.

10:24:04.178 -> 
10:24:04.597 -> CMD C0  
10:24:04.597 -> DATR    0   26  0   0   
10:24:04.597 -> 
10:24:04.597 -> CMD 12  
10:24:04.597 -> DATR    0   A6  0   0   0   0   
10:24:04.597 -> 
10:24:04.597 -> failed, code -705
10:24:04.597 -> [SX1262] Waiting for incoming transmission ... CMD  80  
10:24:04.597 -> DATW    0   A6  
10:24:04.597 -> 
10:24:04.597 -> failed, code -705
10:24:04.597 -> [SX1262] Waiting for incoming transmission ... CMD  80  
10:24:04.597 -> DATW    0   A2  
10:24:04.597 -> 

The working _sx1262receive code I ended up with for completeness:

#include <RadioLib.h>

/*
   RadioLib SX126x Receive Example

   This example listens for LoRa transmissions using SX126x Lora modules.
   To successfully receive data, the following settings have to be the same
   on both transmitter and receiver:
    - carrier frequency
    - bandwidth
    - spreading factor
    - coding rate
    - sync word
    - preamble length

   Other modules from SX126x family can also be used.

   For default module settings, see the wiki page
   https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem

   For full API reference, see the GitHub Pages
   https://jgromes.github.io/RadioLib/
*/

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

// SX1262 has the following connections:
// NSS pin:   10
// DIO1 pin:  2
// NRST pin:  3
// BUSY pin:  9
SX1262 radio = new Module(3, 20, 15, 2, SPI1, RADIOLIB_DEFAULT_SPI_SETTINGS);

// or using RadioShield
// https://github.com/jgromes/RadioShield
//SX1262 radio = RadioShield.ModuleA;

// or using CubeCell
//SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);

void setup() {
  SPI1.setSCK(10);
  SPI1.setTX(11);
  SPI1.setRX(12);
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
  SPI1.begin(false);

  Serial.begin(9600);
  // "Initializing" and "Found" messages not showing without waiting for serial connection
  while (!Serial)
  {
    ; // Wait for serial to connect
  }

  Serial.println("Start");

  // initialize SX1262 with default settings

  Serial.print(F("[SX1262] Initializing ... "));
  int state = radio.begin();
  if (state == RADIOLIB_ERR_NONE) {
    Serial.println(F("success!"));
  } else {
    Serial.print(F("failed, code "));
    Serial.println(state);
    while (true)
      ;
  }
}

void loop() {
  Serial.print(F("[SX1262] Waiting for incoming transmission ... "));

  // you can receive data as an Arduino String
  // NOTE: receive() is a blocking method!
  //       See example ReceiveInterrupt for details
  //       on non-blocking reception method.
  String str;
  int state = radio.receive(str);

  // you can also receive data as byte array
  /*
    byte byteArr[8];
    int state = radio.receive(byteArr, 8);
  */

  if (state == RADIOLIB_ERR_NONE) {
    // packet was successfully received
    Serial.println(F("success!"));

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

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

    // print the SNR (Signal-to-Noise Ratio)
    // of the last received packet
    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_RX_TIMEOUT) {
    // timeout occurred while waiting for a packet
    Serial.println(F("timeout!"));

  } 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);
  }
}

Thanks!

K4KDR commented 11 months ago

This is amazing! I just bought a couple of the Pico Waveshare SX1262 (433 Mhz) boards and ran into the same Error-2 condition. Can't wait to try the solutions provided here - thanks everyone!!

-Scott, K4KDR