Skip to content

timokoethe/SX1278

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SX1278 MicroPython Library for Raspberry Pi Pico

MicroPython Raspberry%20Pi%20Pico Version License

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.

Features

  • 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

Installation

Copy sx1278.py to the root of your MicroPython device, then import it in your script:

from sx1278 import Lora

Quick Start

Example 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),
)

Sending Data

send() blocks until transmission has finished. The maximum payload length is 255 bytes.

lr.send("Hello World!")

Receiving Data

Register a receive handler, put the module into receive mode, and keep the script running:

def handler(message):
    print(message)
    # Put the module back into receive mode after handling a packet.
    lr.recv()

lr.on_recv(handler)
lr.recv()

while True:
    pass

Examples

Ready-to-run examples for the Raspberry Pi Pico are included in the example directory:

Important Notes

  • Use a frequency that is legal for your region.
  • The default frequency in sx1278.py is 433.1 MHz.
  • You can override the default by passing frequency= when creating Lora, or by changing the module constant in the library.

License

This project is licensed under the MIT License. See LICENSE.

About

MicroPython driver for the SX1278 LoRa module over SPI, built for Raspberry Pi Pico and Pico 2.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages