Hello everyone! I am fairly new to Arduino and we are doing a project with the use of LoRa Ra-02 to send and receive specific characters in order for the device to work.
The thing is when the sender sent a message, the receiver just prints in the serial monitor random characters or sometimes not at all. The expected output is that the sender will send the characters 3, 2, 1 based on the measured distance and then the receiver will receive that and perform its functions such as printing on the OLED display and such. However, we can't do it due to the random characters that are sent to the receiver.
Here is the code for the sender and receiver,
Sender:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>
#include <LoRa.h>
#define SS_PIN 10
#define RST_PIN 9
#define DI0_PIN 2
const int trigPin = 3;
const int echoPin = 4;
long duration;
int distance;
unsigned long previousMillis = 0;
const long interval = 5000;
const int MAX_DISTANCE = 60;
String lastSentCategory = "";
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
while (!Serial);
if (!LoRa.begin(434E6)) {
Serial.println("LoRa initialization failed. Check your connections.");
while (1);
}
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125E3);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Initialize I2C communication
Wire.begin();
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Clear the display
display.clearDisplay();
// Set text size and color
display.setTextSize(2); // Increase text size
display.setTextColor(SSD1306_WHITE);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.034 / 2;
distance = MAX_DISTANCE - distance;
String category = getCategory(distance);
if (category != "Overlimit" && category != lastSentCategory) { // Check if category changed
LoRa.beginPacket();
LoRa.print(category);
LoRa.endPacket();
lastSentCategory = category;
Serial.print("Sent category: ");
Serial.println(category);
}
else if (category == "Overlimit" && category != lastSentCategory) {
lastSentCategory = category;
Serial.print("Sent category: ");
Serial.println(category);
}
// Display distance or "Beyond Range" on OLED
display.clearDisplay();
display.setCursor(0, 0);
display.println("Distance:");
if (distance >= 0 && distance <= MAX_DISTANCE) {
display.setCursor(0, 16);
display.print(distance);
display.println(" cm");
} else {
display.setCursor(0, 16);
display.println("Overlimit");
}
display.display();
}
}
String getCategory(int distance) {
if (distance >= 0 && distance <= 20) {
return "3";
} else if (distance > 20 && distance <= 40) {
return "2";
} else if (distance > 40 && distance <= 60) {
return "1";
} else {
return "Overlimit";
}
}
Receiver:
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SS_PIN 10
#define RST_PIN 9
#define DI0_PIN 2
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
while (!Serial);
if (!LoRa.begin(434E6)) {
Serial.println("LoRa initialization failed. Check your connections.");
while (1);
}
LoRa.setSpreadingFactor(12);
LoRa.setSignalBandwidth(125E3);
Wire.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Warning:");
display.display();
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
Serial.print("Received packet: ");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Warning:");
while (LoRa.available()) {
char received = (char)LoRa.read();
Serial.print(received);
if (received == '1') {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 15);
display.println("RED");
} else if (received == '2') {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 15);
display.println("ORANGE");
} else if (received == '3') {
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 15);
display.println("YELLOW");
}
}
display.display();
Serial.println();
}
}
In rare cases, we've somehow managed to get our setup to send characters correctly using trial and error methods. We've tried everything from pressing reset buttons to swapping sender and receiver roles. The latest success involved stripping down the components to just the Arduino and LoRa module, using basic code, and once it worked, we gradually add the other components until it worked. However, once we remove the power supply and reassembled everything, it sends random characters or none at all.
Our Arduino board is a knockoff with a CH340G chip, and we're using an HC-SR04 ultrasonic sensor.
Here is the sample picture of what we see on the serial monitor of the receiver: