Source: https://mokxi.com/libraries/radiolib
Updated: 2026-11-02

Libraries / Communication
Arduino library · Communication

# RadioLib: an SX1278 LoRa radio with status codes for every call

RadioLib, by Jan Gromes, drives dozens of radio chips through one style of API: SX127x and SX126x LoRa radios, CC1101, RF69, nRF24 and more. Every call returns an int status, RADIOLIB_ERR_NONE (0) on success and a negative code naming the problem otherwise, which makes failures far easier to diagnose than a bare true or false.

Open the example in the editor See the calls

Built in on Mokxi: #include <RadioLib.h> and it builds, with nothing to install. Free, and no account needed.

## What RadioLib does

You describe the wiring with a Module: chip select, the interrupt pin (DIO0 on an SX127x), reset, and a second interrupt pin (DIO1). The radio object takes that Module, and begin() with no arguments picks RadioLib’s defaults for the chip, which on an SX1278 are 434 MHz, 125 kHz bandwidth, spreading factor 9 and coding rate 4/7.

transmit() and receive() block until the packet has gone or arrived. For a receiver that does other work, call startReceive() once, register a function with setPacketReceivedAction(), set a flag in it, and call readData() from loop() when the flag is up.

Library Manager name | RadioLib

Author | Jan Gromes

Include | #include <RadioLib.h>

Boards | every board Mokxi compiles for but ATtiny85

In the simulator | Runs, with a gap the note below names.

Parts it drives | LoRa module, SX1278 (Ra-02)

## Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <RadioLib.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "RadioLib" by Jan Gromes from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add RadioLib to lib_deps in PlatformIO. Mokxi follows the calls of release 7.1.2, so a sketch written here builds there too.

What to know in the simulator: Only the SX127x radios in LoRa mode: SX126x, SX128x, CC1101 and RadioLib’s other modules are not declared, and beginFSK is not here.

ESP32-C3 · Arduino Uno · Arduino Nano · Arduino Mega 2560 · Arduino Leonardo · SparkFun Pro Micro · Arduino Micro · ESP32-C6 · Raspberry Pi Pico · Raspberry Pi Pico W · Seeed XIAO SAMD21 · STM32 · STM32 Nucleo-F411RE · STM32 Blue Pill · micro:bit V2 · Arduino Uno R4 · ESP32 DevKit · ESP32-S3-DevKitC-1 · ESP8266 NodeMCU · Arduino Pro Mini 3.3 V · Seeed XIAO ESP32-C3 · ESP32-C3 SuperMini · Seeed XIAO ESP32-C6 · ESP32-DevKitC V4 · LOLIN32 Lite · Wemos D1 R32 · Cheap Yellow Display · TTGO T-Display · Seeed XIAO ESP32-S3 · Arduino Nano ESP32 · Arduino Nano RP2040 Connect · Seeed XIAO RP2040 · Waveshare RP2040-Zero · Arduino Uno R4 WiFi

## The calls you will use

In the order a sketch meets them.

### SX1278 radio = new Module(10, 2, 9, 3)

Creates an SX1278 on NSS 10, DIO0 2, RESET 9 and DIO1 3. SX1276 and RFM95 are the same class family with their own names.

### int state = radio.begin()

Starts the radio with RadioLib’s defaults, or with frequency, bandwidth and spreading factor given as arguments. Compare the result with RADIOLIB_ERR_NONE.

### radio.transmit("Hello World!")

Sends a string or a byte array and waits until it is out. Returns RADIOLIB_ERR_PACKET_TOO_LONG for more than 255 bytes.

### radio.receive(str)

Waits for a packet and fills a String or buffer. Returns RADIOLIB_ERR_RX_TIMEOUT when nothing came in time.

### radio.setPacketReceivedAction(setFlag)

Calls your function from the DIO0 interrupt when a packet arrives. Mark it ICACHE_RAM_ATTR on ESP boards and only set a flag inside it.

### radio.startReceive()

Puts the radio in continuous receive mode for interrupt-driven sketches.

### radio.readData(str)

Reads the packet that arrived, after the flag says one did.

### radio.getRSSI()

The last packet’s signal strength in dBm. getSNR() gives the signal to noise ratio.

### radio.setSpreadingFactor(10)

Changes the spreading factor (6 to 12) after begin(). Like the other modem settings, it must match on both radios.

## Example: Transmit with an SX1278

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino · Arduino Uno R3

### Wiring

- Arduino Uno R3 13 to LoRa module, SX1278 (Ra-02) SCK
- Arduino Uno R3 11 to LoRa module, SX1278 (Ra-02) MOSI
- Arduino Uno R3 12 to LoRa module, SX1278 (Ra-02) MISO
- Arduino Uno R3 10 to LoRa module, SX1278 (Ra-02) NSS
- Arduino Uno R3 9 to LoRa module, SX1278 (Ra-02) RST
- Arduino Uno R3 2 to LoRa module, SX1278 (Ra-02) DIO0
- Arduino Uno R3 3 to LoRa module, SX1278 (Ra-02) DIO1
- Arduino Uno R3 3V3 to LoRa module, SX1278 (Ra-02) 3.3V
- Arduino Uno R3 GND to LoRa module, SX1278 (Ra-02) GND

Open it in the editor, wired and ready to run

## Common errors and how to fix them

### failed, code -2

RADIOLIB_ERR_CHIP_NOT_FOUND: no SX127x answered on SPI. Check NSS and RESET in the Module match the wiring, the SPI pins, and the 3.3 V supply. A wrong class (SX1262 code on an SX1278 module) gives the same code.

### receive() returns -6 every time

That is a timeout: nothing arrived with matching settings. Make frequency, bandwidth, spreading factor, coding rate and sync word the same on both radios, and attach both antennas.

### 'SX1262' was not declared in this scope in Mokxi

Mokxi simulates RadioLib’s SX127x LoRa classes only, so other radios are not declared and beginFSK() is not there. Run SX1276, SX1278 or RFM95 sketches, or try the sx1278 part with the LoRa library.

## Keep going

### Learn

- SPI Explained: Clock, Data and Chip Select

### Parts

- LoRa module, SX1278 (Ra-02)

### More communication libraries

- OneWire
- TinyGPS
- TinyGPSPlus
- MFRC522
- SoftwareSerial
- AltSoftSerial
- i2cdetect
- Keyboard

Every built-in library · Open the editor

## Try RadioLib without installing anything

Open the example, change the sketch, and keep your version in a free account.

Start building free

Open the editor
