Skip to content

Instantly share code, notes, and snippets.

@fagci
Created June 19, 2024 17:40
Show Gist options
  • Save fagci/dc05344d2fac373459d7a3a793f85602 to your computer and use it in GitHub Desktop.
Save fagci/dc05344d2fac373459d7a3a793f85602 to your computer and use it in GitHub Desktop.
LoRa32 V2.1_1.6 sx1278 433 MHz spectrum analyzer
#include <SPI.h>
#include <SSD1306Wire.h>
#include <RadioLib.h>
const uint8_t LORA_MOSI = 27;
const uint8_t LORA_MISO = 19;
const uint8_t LORA_SCLK = 5;
const uint8_t LORA_CS = 18;
const uint8_t LORA_DIO0 = 26;
const uint8_t LORA_DIO1 = 33;
const uint8_t LORA_DIO2 = 32;
const uint8_t LORA_RST = 23;
SSD1306Wire display(0x3C, SDA, SCL);
SX1278 radio = new Module(LORA_CS, LORA_DIO0, LORA_RST, LORA_DIO1);
int ConvertDomain(int aValue, int aMin, int aMax, int bMin, int bMax) {
const int aRange = aMax - aMin;
const int bRange = bMax - bMin;
aValue = std::clamp(aValue, aMin, aMax);
return ((aValue - aMin) * bRange + aRange / 2) / aRange + bMin;
}
void setup() {
SPI.begin(LORA_SCLK, LORA_MISO, LORA_MOSI);
display.init();
display.flipScreenVertically();
radio.beginFSK(433, 32.768, 6.25, 25);
radio.startReceive();
}
void loop() {
display.clear();
for (uint8_t i = 0; i < 128; ++i) {
radio.setFrequency(433.0 + 0.025 * i);
int rssi = radio.getRSSI(true, true);
uint8_t v = ConvertDomain(rssi, -120, -45, 0, 64);
display.drawVerticalLine(i, 64 - v, v);
}
display.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment