Lessons and the learning game

LoRa long-range radio

Two Unos and two SX1278 radios: how LoRa reaches kilometers, radio setup, airtime math, duty cycle rules, and an ocean buoy reporting to shore.

The bench

Every lesson opens the same bench: a buoy Uno on the left and a shore station Uno on the right, each with a Ra-02 module (a Semtech SX1278 at 433 MHz). Each board is wired only to its own radio: SPI on pins 11, 12 and 13, NSS on 10, RST on 9 and DIO0 on 2, power from the 3V3 pin. Every LoRa module on the canvas shares the same air, so the boards talk with no wire drawn.

The boss project adds a knob on the buoy's A0 (its wave sensor), an SOS button on pin 3, and on the shore a 16x2 I2C LCD and a red storm lamp on pin 7.

Take the course at /learn/path/lora-long-range on /learn.

The lessons

  1. Spread spectrum. Chirps, spreading factors, processing gain, and why LoRa decodes signals below the noise. The link budget.
  2. SX1278 setup. A driver with no library: reset, RegVersion, LoRa mode, the frequency as Frf = f x 2^19 / 32 MHz, ModemConfig1 and 2, and a packet sent through the FIFO.
  3. Spreading factor and airtime. The datasheet's time-on-air formula for SF7 to SF12 at coding rates 4/5 and 4/8: 41.2 ms for 10 bytes at SF7, 991.2 ms at SF12.
  4. Packets and IDs. Binary frames with a destination, a source, a sequence number and a type; dropping packets for someone else; counting lost packets; the sync word.
  5. Duty cycle and the law. The EU 868 MHz sub-band limits (1 percent in 868.0 to 868.6 MHz, 10 percent in 869.4 to 869.65 MHz) enforced per packet as Toff = T / duty - T, and the US 915 MHz 400 ms dwell limit checked with the formula.
  6. What LoRaWAN adds. Gateways, network servers, the frame layout, session keys and joining, the frame counter, Class A receive windows, downlinks and confirmed uplinks.
  7. Boss: an ocean buoy. Binary reports, a duty cycle budget, a storm mode that reports faster and lights the shore's lamp, and an acknowledged SOS with retries.

What each sketch does

Sketch Board What it does
lrbeacon buoy Sends "buoy ping N" every 2 s and explains SF7.
lrlisten shore Prints every packet with its RSSI and SNR.
lrsetup buoy Sets up the SX1278 with plain SPI and sends through the FIFO.
lrairtime buoy Prints the airtime table and times a real packet.
lrpacket buoy Sends 8 byte binary frames and prints them in hex.
lrsort shore Unpacks frames, drops others' packets, counts lost ones.
lrduty buoy A duty cycle guard, and the US dwell time check.
lrwan buoy A LoRaWAN-shaped Class A node with an RX1 window.
lrwangw shore A gateway and network server stand-in that answers in RX1.
lrbuoy buoy The boss buoy: wave reports, storm mode, SOS.
lrshore shore The boss shore station: LCD, storm lamp, SOS acknowledgment.

What the simulation leaves out

  • Walls and fading. The signal fades with the distance between the two modules on the canvas (50 m on the bench: -54 dBm and 10.0 dB SNR), in open air. Walls, hills, fading and interference are explained, not simulated.
  • SPI speed. A packet is on the air for the datasheet formula's time, but filling the FIFO over the simulated SPI bus takes longer than on a real Uno, so a sketch that times beginPacket() to endPacket() reads about 10 ms more than the formula. The sketches print the formula, which is what a real SX1278 and every duty cycle rule use.
  • LoRaWAN. Lesson 6 runs a model on two plain radios: the frame layout and Class A timing are real, but there is no AES, no join and no network server, and its MIC is a stand-in checksum. To join a real network, use an 868 or 915 MHz board with a stack such as LMIC or RadioLib.
  • Bands. The Ra-02 is a 433 MHz module. The 868 and 915 MHz rules are taught with its packets; check your own country's rules before you transmit on any band.

On a real bench the SX1278 runs on 3.3 V and its pins are not 5 V tolerant: put a level shifter between it and an Uno, or use a 3.3 V board, and never transmit without an antenna.

Build a tiny kernel, another advanced embedded course on the Uno.