Lightweight MicroPython driver for the Ra-01 LoRa module based on the SX1278 chipset, with a setup focused on the Raspberry Pi Pico (RP2040) and Raspberry Pi Pico 2 (RP2350).
The library communicates over SPI and provides a simple API for sending and receiving LoRa packets.
- MicroPython-first API with minimal setup
- Works with Raspberry Pi Pico and Pico 2
- Blocking packet transmission with
send() - Interrupt-driven packet reception with
on_recv() - Configurable frequency, bandwidth, spreading factor, coding rate, and CRC
Copy sx1278.py to the root of your MicroPython device, then import it in your script:
from sx1278 import LoraExample wiring used by the snippets below:
| SX1278 / Ra-01 | Raspberry Pi Pico | Used in code |
|---|---|---|
| MISO | GP0 | MISO = 0 |
| DIO0 | GP10 | RX = 10 |
| SCK | GP2 | SCK = 2 |
| MOSI | GP3 | MOSI = 3 |
| RST | GP16 | RST = 16 |
| NSS | GP1 | CS = 1 |
| GND | GND | power |
| 3V3 | 3V3(OUT) | power |
Set up SPI first:
from machine import Pin, SPI
from sx1278 import Lora
SCK = 2
MOSI = 3
MISO = 0
CS = 1
RX = 10
RST = 16
spi = SPI(
0,
baudrate=10_000_000,
sck=Pin(SCK, Pin.OUT, Pin.PULL_DOWN),
mosi=Pin(MOSI, Pin.OUT, Pin.PULL_UP),
miso=Pin(MISO, Pin.IN, Pin.PULL_UP),
)
spi.init()Then create the LoRa instance:
lr = Lora(
spi,
cs=Pin(CS, Pin.OUT),
rx=Pin(RX, Pin.IN),
rs=Pin(RST, Pin.OUT),
)All radio settings have defaults. See the API reference for the full list of constructor options.
send() blocks until transmission has finished or the TX timeout is reached. The default timeout is 5000 ms and can be changed with tx_timeout_ms= when creating Lora. The maximum payload length is 255 bytes.
lr.send("Hello World!")Register a receive handler, put the module into receive mode, and keep the script running:
def handler(message):
print(message.decode())
lr.on_recv(handler)
lr.recv()
while True:
passThe handler is called with the payload as a bytes object, so print(message) prints b'Hello World!' rather than Hello World!.
Decode it when you expect text.
Ready-to-run examples for the Raspberry Pi Pico are included in the example directory:
docs/api.md documents the full public API: method signatures, defaults, value ranges, limitations, and which radio mode each method requires.
- Use a frequency that is legal for your region.
- The default frequency in
sx1278.pyis433.1MHz. - You can override the default by passing
frequency=when creatingLora, or by changing the module constant in the library. - The default transmit power is
17dBm. Values above17are clamped to17by the default PA_BOOST output path.
This project is licensed under the MIT License. See LICENSE.