reglage-sp.ino

Arduino MKR WAN 1300 · File : reglage-sp.ino · Language : Arduino · Use case : Régler l'antidatage SF pour maximiser la portée

LoRa range is tuned by spreading factor: this sketch transmits the same message from SF7 up to SF12, thirty seconds per step. The receiver must switch SF along with the transmitter — or run one receiver per SF — to measure each step's real distance.

Hardware

An Arduino MKR WAN 1300 (SAMD21 microcontroller, Semtech SX1272 LoRa modem for point-to-point links, no built-in LoRaWAN stack, 868/915 MHz antenna). At least two MKR WAN 1300s, 868 MHz antennas, open terrain or a car trip for the campaign.

Wiring

No wiring; note the time and place of the last received packets to plot the range curve.

How it works

setSpreadingFactor sets robustness (SF12 = 6x the airtime of SF7), setBandwidth(125) respects the EU plan, setCodingRate(5) adds error correction. Each +1 SF step ≈ +20-35 % range.

Running it

Install Arduino SAMD Boards and the MKRWAN library, select the board, upload. You need TWO LoRa boards (two MKR WAN 1300s, or a 1300 and a 1310) to see exchanges; set the same frequency plan in the code.

Variants

Swap roles to measure inbound, or lock SF12 on the transmitter and vary power (setTxPower) to isolate its effect.

reglage-sp.ino

// Balayage des facteurs detalement : portee vs debit sur MKR WAN 1300.
// Emetteur : diffuse le meme message dans chaque SF, 30 s chacun.
#include <MKRWAN.h>

LoRaModem modem;

const int SFS[] = {7, 8, 9, 10, 11, 12};
int index = 0;
unsigned long dernier = 0;

void setup() {
  Serial.begin(115200);
  while (!Serial);
  if (!modem.begin(EU868)) while (true);
  modem.setFrequency(868.3); // canal libre hors trames classiques
  appliquer();
}

void loop() {
  if (millis() - dernier >= 30000) {
    dernier = millis();
    index = (index + 1) % 6;
    appliquer();
  }
  modem.beginPacket();
  modem.print("SF");
  modem.print(SFS[index]);
  modem.print(" ");
  modem.print(millis() / 1000);
  modem.endPacket(false);
  delay(2000);
}

void appliquer() {
  modem.setSpreadingFactor(SFS[index]);
  modem.setBandwidth(125);    // kHz, obligation reglementaire EU
  modem.setCodingRate(5);     // 4/5
  Serial.print(F("SF="));
  Serial.println(SFS[index]); // SF12 ~ 0.3 kbit/s, portee max
}

← Back to the board