SPX1276 pin connections and error codes

Initially asked for confirmation of the actual LORA pins and how to connect to an Arduino.
Got confirmation as to the correct connections to a MEGA. Also confirmed that actual SPI connections are not required as the pins: int pin_cs = 41; int pin_dio0 = 0; int pin_nrst = 33;
int pin_dio1 = 34, are the SPI connections. Issue is with MEGA pin 0 is the RX UART so when a devcie is connected to that pin it overrides the USB programming port.

The other issue is where is the documentation that explains error codes? I am getting error code -2, and error code -16.

1 Like

Hi Geoff,

Can you share what LoRa module you are using? Is there a library that you are using to that is giving you these error codes or does this happen when compiling the code?

1 Like

Its in the topic title: SPX1276.

I am using the recommended RadioLib examples, I am also using the example that is recommended on the Core Electronics page: SX1276­_Transmit.

This sketch loads without errors, but when it is run the serial monitor returns:

[SX1276] Initializing … failed, code -2

It has also returned: … failed, code -16

2 Likes

Hi Geoff!

Welcome back!

Thanks for clarifying, but to be sure, could you confirm if its this specific board?

From the troubleshooting guide it looks like that means that the SPI bus is incorrect or there are connection issues:

Keen to get this one sorted!
Liam

you can choose another pin for pin_dio0 that doesn’t interfere with the UART communication.

Yes it is the SPX1276 as pictured.

This is the board I wanted as that is why I ordered it.

The first enquiry I had was to try to find an actual connection diagram, there are many other sensor and lora tutorials that include an actual physical wiring diagram from the device to the Arduino. I have not been able to find one.

I have studied the RadioLib github at length and cannot find to the incorrect SPI bus connections.

Thanks for responding, will assist in any other way.

It’s the use of MEGA pin 0 that is the issue, not the SPX1276 pin_DI0. The other confusion is that the SPX1276 pins are labelled as: D0, D1, D2 etc, not DI0, DI1, etc.

Another MEGA pin could be used but will the correct signalling be applied from that other pin? There must be some technical reason for initially using MEGA pin 0?

Hi Geoff,

If you could share some images of your current setup and the code you are currently running we can dive deeper into the problem.

If you look at the schematic for this board the pins on the E19-915M30S module are named DI0, DI1, etc. These line up with pins D0, D1, and onwards, so there is no need to worry about this.

And last question, where did you get the pinout you are following to connect the Mega to the LoRa breakout?

Photos and sketch attached:

(Attachment SX127x_Transmit.ino is missing)

(attachments)


The sketch I attached was rejected, sending all attachments again:

(Attachment sxtransmit.txt is missing)

See attached photos, the sketch I am using is SX_Transmit: https://github.com/sparkfunX/LoRa_1W_Breakout/tree/main/Firmware

There is no technical difference between Mega pin 0 and the other digital pins. Some Mega pins do have a technical difference, in whether or not they support PWM and exactly which PWM frequencies can be used. I don’t think PWM is relevant in this case, and anyway pin 0 isn’t a PWM pin. Pin 0 is actually a poor choice but perhaps the original configuration used when this sketch was implemented forced that choice for some reason. If you aren’t using other pins for other purposes then go ahead and test it out with any other pin - it won’t hurt to try.

1 Like

There is a difference between Pin 0 and other MEGA digital pins. It is the RX pin for the UART 0. So if the sketch is using that pin to do something it will matter.

I tried changing to MEGA pin 2, physical wiring change, and altered the line in the sketch:

int pin_di0 = 2; // was 0

No difference to the operation, still returned an error:

[SX1276] Initializing … failed, code -2

Yes - the comment should have been “There is no technical difference between ATMega2560 pin 0 and the other digital pins”. In other words there is no technical reason for choosing this pin.

However, in the Arduino Mega development board the ATMega2560 pin 0 is wired to the ATMega16U2 used for the USB interface, which is why it is a poor choice in this application and why you should try the sketch using any other digital pin. Pin 2 is fine for this choice. If that didn’t solve the problem then either pin 0 was not the cause or there was a good reason for using pin 0, even though there is no technical difference.

The original configuration would likely work just fine with Pin 0 if the sketch did not create a Serial object, which is quite possible with the Mega because it has multiple hardware UARTs. So the other change is to check if Serial is being used and if it is then either remove it or change it to Serial1 and debug through that port instead of the USB connection. If that doesn’t work then I would conclude that Pin 0 is not the problem and you can continue debugging with whatever configuration is most convenient. Leaving the code exactly as provided and using an alternative debugging console would be my preference.

1 Like

Thanks for your reply.

I will investigate further wiring details to get the correct wiring.

1 Like

Hi Geoff,

From the photos you sent you arent using the MEGA’s default SPI pins.

The pins can be found here: SPI - Arduino Reference

The error -2 and -16 are from the CS, SDI, SDO and SDK pins being in the wrong input on the Mega

1 Like

That photo is using only the pins quoted in the sketch. That is before I had confirmation that the SPI pins also need to be connected.

From the SX1276x_Transmit sketch:

// SX1276 requires the following connections:

int pin_cs = 41;

int pin_dio0 = 0;

int pin_nrst = 33;

int pin_dio1 = 34;

// SX1276 radio = new Module(pin_cs, pin_dio0, pin_nrst, pin_dio1);

I have also since found the reference to the error codes:

https://github.com/jgromes/RadioLib/wiki/Troubleshooting-Guide

1 Like

Hi Geoff,

The new module line must be uncommented to use the new SPI pins, the default SPI on the Mega that @Liam120347 sent through will also work.

Liam

Liam,

Thanks for the reminder. I have modified that line in the sketch.

I also swapped the SPX1276 SDO pin to the MEGA pin50 (MISO) and SPX1276 SDI pin to MEGA pin51 (MOSI), and it works correctly.

Geoff.

2 Likes

Further to this forum topic: I have the SPX1276 working using a MEGA board. My issue was that I had the SDO and SDI pins on the SPX swapped around when connected to the SPI connections on the MEGA.

The SDO pin goes to MEGA pin 50, which is labelled as MISO, and the SDI pin goes to MEGA pin 51, which is labelled as MOSI.

Regarding the issue with the sketch suggesting MEGA pin 0, disconnecting this pin before uploading the sketch, and then reconnecting it after loading the sketch also caused the SPX to work. Another option is to just use another MEGA pin other than 0, (I used D2), and included the pin numbers in the sketch on line:

SX1276 radio = new Module(pin_cs, pin_dio0, pin_nrst, pin_dio1);

just replace the pin_cs with the actual MEGA pin number that is suggested in the sketch: 41.

Do the same with the other pins in the brackets; pin_di0 becomes 2.

With the transmitter working the next step is to configure and code the receiver.

1 Like