acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

SX126x_Settings.ino (5142B)

      1 /*
      2    RadioLib SX126x Settings Example
      3 
      4    This example shows how to change all the properties of LoRa transmission.
      5    RadioLib currently supports the following settings:
      6     - pins (SPI slave select, DIO1, DIO2, BUSY pin)
      7     - carrier frequency
      8     - bandwidth
      9     - spreading factor
     10     - coding rate
     11     - sync word
     12     - output power during transmission
     13     - CRC
     14     - preamble length
     15     - TCXO voltage
     16     - DIO2 RF switch control
     17 
     18    Other modules from SX126x family can also be used.
     19 
     20    For default module settings, see the wiki page
     21    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx126x---lora-modem
     22 
     23    For full API reference, see the GitHub Pages
     24    https://jgromes.github.io/RadioLib/
     25 */
     26 
     27 // include the library
     28 #include <RadioLib.h>
     29 
     30 // SX1262 has the following connections:
     31 // NSS pin:   10
     32 // DIO1 pin:  2
     33 // NRST pin:  3
     34 // BUSY pin:  9
     35 SX1262 radio1 = new Module(10, 2, 3, 9);
     36 
     37 // SX12628 has different connections:
     38 // NSS pin:   8
     39 // DIO1 pin:  4
     40 // NRST pin:  5
     41 // BUSY pin:  6
     42 SX1268 radio2 = new Module(8, 4, 5, 6);
     43 
     44 // or using RadioShield
     45 // https://github.com/jgromes/RadioShield
     46 //SX1261 radio3 = RadioShield.ModuleB;
     47 
     48 // or using CubeCell
     49 //SX1262 radio = new Module(RADIOLIB_BUILTIN_MODULE);
     50 
     51 void setup() {
     52   Serial.begin(9600);
     53 
     54   // initialize SX1268 with default settings
     55   Serial.print(F("[SX1262] Initializing ... "));
     56   int state = radio1.begin();
     57   if (state == RADIOLIB_ERR_NONE) {
     58     Serial.println(F("success!"));
     59   } else {
     60     Serial.print(F("failed, code "));
     61     Serial.println(state);
     62     while (true);
     63   }
     64 
     65   // initialize the second LoRa instance with
     66   // non-default settings
     67   // this LoRa link will have high data rate,
     68   // but lower range
     69   Serial.print(F("[SX1268] Initializing ... "));
     70   // carrier frequency:           915.0 MHz
     71   // bandwidth:                   500.0 kHz
     72   // spreading factor:            6
     73   // coding rate:                 5
     74   // sync word:                   0x34 (public network/LoRaWAN)
     75   // output power:                2 dBm
     76   // preamble length:             20 symbols
     77   state = radio2.begin(915.0, 500.0, 6, 5, 0x34, 20);
     78   if (state == RADIOLIB_ERR_NONE) {
     79     Serial.println(F("success!"));
     80   } else {
     81     Serial.print(F("failed, code "));
     82     Serial.println(state);
     83     while (true);
     84   }
     85 
     86   // you can also change the settings at runtime
     87   // and check if the configuration was changed successfully
     88 
     89   // set carrier frequency to 433.5 MHz
     90   if (radio1.setFrequency(433.5) == RADIOLIB_ERR_INVALID_FREQUENCY) {
     91     Serial.println(F("Selected frequency is invalid for this module!"));
     92     while (true);
     93   }
     94 
     95   // set bandwidth to 250 kHz
     96   if (radio1.setBandwidth(250.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) {
     97     Serial.println(F("Selected bandwidth is invalid for this module!"));
     98     while (true);
     99   }
    100 
    101   // set spreading factor to 10
    102   if (radio1.setSpreadingFactor(10) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
    103     Serial.println(F("Selected spreading factor is invalid for this module!"));
    104     while (true);
    105   }
    106 
    107   // set coding rate to 6
    108   if (radio1.setCodingRate(6) == RADIOLIB_ERR_INVALID_CODING_RATE) {
    109     Serial.println(F("Selected coding rate is invalid for this module!"));
    110     while (true);
    111   }
    112 
    113   // set LoRa sync word to 0xAB
    114   if (radio1.setSyncWord(0xAB) != RADIOLIB_ERR_NONE) {
    115     Serial.println(F("Unable to set sync word!"));
    116     while (true);
    117   }
    118 
    119   // set output power to 10 dBm (accepted range is -17 - 22 dBm)
    120   if (radio1.setOutputPower(10) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
    121     Serial.println(F("Selected output power is invalid for this module!"));
    122     while (true);
    123   }
    124 
    125   // set over current protection limit to 80 mA (accepted range is 45 - 240 mA)
    126   // NOTE: set value to 0 to disable overcurrent protection
    127   if (radio1.setCurrentLimit(80) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) {
    128     Serial.println(F("Selected current limit is invalid for this module!"));
    129     while (true);
    130   }
    131 
    132   // set LoRa preamble length to 15 symbols (accepted range is 0 - 65535)
    133   if (radio1.setPreambleLength(15) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) {
    134     Serial.println(F("Selected preamble length is invalid for this module!"));
    135     while (true);
    136   }
    137 
    138   // disable CRC
    139   if (radio1.setCRC(false) == RADIOLIB_ERR_INVALID_CRC_CONFIGURATION) {
    140     Serial.println(F("Selected CRC is invalid for this module!"));
    141     while (true);
    142   }
    143 
    144   // Some SX126x modules have TCXO (temperature compensated crystal
    145   // oscillator). To configure TCXO reference voltage,
    146   // the following method can be used.
    147   if (radio1.setTCXO(2.4) == RADIOLIB_ERR_INVALID_TCXO_VOLTAGE) {
    148     Serial.println(F("Selected TCXO voltage is invalid for this module!"));
    149     while (true);
    150   }
    151 
    152   // Some SX126x modules use DIO2 as RF switch. To enable
    153   // this feature, the following method can be used.
    154   // NOTE: As long as DIO2 is configured to control RF switch,
    155   //       it can't be used as interrupt pin!
    156   if (radio1.setDio2AsRfSwitch() != RADIOLIB_ERR_NONE) {
    157     Serial.println(F("Failed to set DIO2 as RF switch!"));
    158     while (true);
    159   }
    160 
    161   Serial.println(F("All settings succesfully changed!"));
    162 }
    163 
    164 void loop() {
    165   // nothing here
    166 }