LoRa messenger on an Arduino Uno
Two Unos, a Ra-02 LoRa module each and no wire between them: one sends a numbered message every two seconds, the other prints it, and the sender shows its time on the air. The whole build, an Arduino Uno and 2 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Advanced Runs in your browser. Free, and no account needed.
The parts retail for about $62. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// LoRa messenger, the sending end.
//
// A Ra-02 (SX1278) on SPI, driven by LoRa.h with its default pins: NSS on 10,
// RESET on 9 and DIO0 on 2. Every two seconds it sends a numbered message at
// 433 MHz and prints how long the packet took on the air. At the default
// spreading factor of 7 that is a few tens of milliseconds; change it with
// LoRa.setSpreadingFactor() on both boards and watch the time grow.
#include <SPI.h>
#include <LoRa.h>
int counter = 0;
void setup()
{
Serial.begin(9600);
Serial.println("LoRa Sender");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1) {
}
}
}
void loop()
{
Serial.print("Sending packet: ");
Serial.println(counter);
unsigned long start = millis();
LoRa.beginPacket();
LoRa.print("hello ");
LoRa.print(counter);
LoRa.endPacket();
Serial.print(" on the air for ");
Serial.print(millis() - start);
Serial.println(" ms");
counter++;
delay(2000);
}
// LoRa messenger, the receiving end.
//
// The same module and pins as the sender, on the same frequency. parsePacket()
// puts the radio in single receive mode and returns the length of a packet
// once one has come in; then the bytes are read one at a time. The signal
// strength is a fixed -45 dBm in the simulator, which has no distance.
#include <SPI.h>
#include <LoRa.h>
void setup()
{
Serial.begin(9600);
Serial.println("LoRa Receiver");
if (!LoRa.begin(433E6)) {
Serial.println("Starting LoRa failed!");
while (1) {
}
}
}
void loop()
{
int packetSize = LoRa.parsePacket();
if (packetSize) {
Serial.print("Received packet '");
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}
Serial.print("' with RSSI ");
Serial.println(LoRa.packetRssi());
}
}
Parts list
4 parts, plus the jumper wires. They retail for about $62; on Mokxi they're free.
How it is wired
16 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Ground: Arduino Uno R3 (1) pin GND; LoRa module, SX1278 (Ra-02) (1) pin GND
- Arduino Uno R3 (1) pin 13; LoRa module, SX1278 (Ra-02) (1) pin SCK
- Arduino Uno R3 (1) pin 12; LoRa module, SX1278 (Ra-02) (1) pin MISO
- Arduino Uno R3 (1) pin 11; LoRa module, SX1278 (Ra-02) (1) pin MOSI
- Arduino Uno R3 (1) pin 10; LoRa module, SX1278 (Ra-02) (1) pin NSS
- Arduino Uno R3 (1) pin 9; LoRa module, SX1278 (Ra-02) (1) pin RST
- Arduino Uno R3 (1) pin 2; LoRa module, SX1278 (Ra-02) (1) pin DIO0
- Arduino Uno R3 (1) pin 3V3; LoRa module, SX1278 (Ra-02) (1) pin 3.3V
- Ground: Arduino Uno R3 (2) pin GND; LoRa module, SX1278 (Ra-02) (2) pin GND
- Arduino Uno R3 (2) pin 13; LoRa module, SX1278 (Ra-02) (2) pin SCK
- Arduino Uno R3 (2) pin 12; LoRa module, SX1278 (Ra-02) (2) pin MISO
- Arduino Uno R3 (2) pin 11; LoRa module, SX1278 (Ra-02) (2) pin MOSI
- Arduino Uno R3 (2) pin 10; LoRa module, SX1278 (Ra-02) (2) pin NSS
- Arduino Uno R3 (2) pin 9; LoRa module, SX1278 (Ra-02) (2) pin RST
- Arduino Uno R3 (2) pin 2; LoRa module, SX1278 (Ra-02) (2) pin DIO0
- Arduino Uno R3 (2) pin 3V3; LoRa module, SX1278 (Ra-02) (2) pin 3.3V
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.