Lora Sx1278 and HC-SR04 interfacing ESP32

Hello, I have Lora Sx1278, HC-SR04 ultrasonic sensor, and ESP32 and I want to transmit the distance (in meters, decimal form) from the sensor of the transmitter to the receiver. The transmitter and receiver have both ESP32 and Lora sx1278. Can someone help me provide the code for this? Please..

Sorry, we’re not a free code-writing service, but we’re more than happy to help. Please post an annotated schematic showing exactly how you plan to wire your project, along with your preliminary code, and explain what isn’t working. Start simple and tackle the project in small sections.

I also recommend getting a copy of the Arduino Cookbook where you might find that most of your project has already been covered in there.

I'm planning to make a flood level monitoring IOT project using Lora sx1278, HC-SR04, and ESP32. Here is my code and it's not functioning. I didn't get the distance on the serial monitor from the sensor. I'm sorry i am not knowledgeable enough about this. Please help. I only made this code based on the simple programming of Lora that I've watched on youtube. I want to learn and master Lora programming. Please help..

Transmitter side


// Lora library
#include <SPI.h>
#include <LoRa.h> 

// Lora pins
#define ss 5
#define rst 14
#define dio 15

// Ultrasonic sensor pins
#define echoPin 21
#define trigPin 22

// Ultrasonic sensor variables
long duration;
float distance_cm;
float distance_m;
float totalDistance = 4.00; // Set total distance up to the bottom (meters)
float level_m;
 
void setup() {
  Serial.begin(115200);
  
  LoRa.setPins(ss, rst, dio);

  while (!Serial);  
  Serial.println("LoRa Sender");
  if (!LoRa.begin(433E6)) { // or 915E6, the MHz speed of your module
    Serial.println("Starting LoRa failed!");
    while (1);
  }

  pinMode(trigPin, OUTPUT); 
  pinMode(echoPin, INPUT); 
  delay(2000); 
}

void loop() {
  ultrasonic();
  LoRa.beginPacket();  
  LoRa.print(level_m);
  LoRa.endPacket();
  delay(50);
}

void ultrasonic()
{
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    duration = pulseIn(echoPin, HIGH);
    distance_cm = duration * 0.034 / 2; //formula to calculate the distance for ultrasonic sensor
    distance_m = distance_cm / 100;
    Serial.print("Distance: ");
    Serial.print(distance_cm);
    Serial.print(" cm");
    Serial.print("    ");
    Serial.print("Distance: ");
    Serial.print(distance_m);
    Serial.print(" m");

    // Determing level
    level_m = totalDistance - distance_m; 
    Serial.print("    ");
    Serial.print("Level: ");
    Serial.print(level_m);
    Serial.println(" m");
    delay(500);
}


Receiver side


// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "  "
#define BLYNK_TEMPLATE_NAME "  "
#define BLYNK_AUTH_TOKEN "  "

// Comment this out to disable prints and save space
//#define BLYNK_PRINT Serial
 
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
 
char auth[] = BLYNK_AUTH_TOKEN;
 
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = " ";
char pass[] = " ";
 
BlynkTimer timer;

#include <SPI.h>
#include <LoRa.h> 

#define ss 5
#define rst 14
#define dio 15

String inString = "";    // string to hold input
float level_m = 0;
 
void setup() {
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  
  LoRa.setPins(ss, rst, dio);

  while (!Serial);
  Serial.println("LoRa Receiver");
  if (!LoRa.begin(433E6)) { // or 915E6
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) { 
    // read packet    
    while (LoRa.available())
    {
      int inChar = LoRa.read();
      inString += (char)inChar;
      level_m = inString.toInt();       
    }
    inString = "";     
    LoRa.packetRssi();    
  }

  Blynk.run();
  Blynk.virtualWrite(V0, level_m);
  Serial.print("Level: ");
  Serial.print(level_m);
  Serial.println(" m");
}




I moved your topic to an appropriate forum category @01dan.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

concentrate on getting the ultrasonic sensor working then worry about the LoRa
does any information from the sensor appear on the serial monitor?

this worked on a ESP32 with a AJ-SR04M ultrasonic sensor

// --- Hardware Mapping ---
#define trig 12  //Pin 33 trigger Tx
#define echo 13  //Pin 32 echo Rx

void trigPuls();

float pulse;     //echo time duration
float dist_cm;   //distance in cm

void setup() 
{
  pinMode(trig, OUTPUT);
  pinMode(echo, INPUT);
  
  digitalWrite(trig, LOW);
  Serial.begin(115200);
}

void loop() 
{
   trigPulse();
   pulse = pulseIn(echo, HIGH, 200000);
   dist_cm = pulse/58.82;
   // 340m/s
   // 34000cm/s  
   /*
        100000 us - 17000 cm/s
             x us - 1cm
           1E6
      x = -----
          17E3
   */  
   Serial.println(String(dist_cm) + " cm");
   delay(1000);
}

void trigPulse()
{
  digitalWrite(trig, HIGH);  //Trigger Pulse HIGH
  delayMicroseconds(10);     //for 10 micro seconds
  digitalWrite(trig, LOW);   //Trigger Pulse LOW
}

some time back I implemented a river monitoring system using The Things UNO with a BMP280 temperature/pressure sensor, a SR04M ultrasonic transducer and a DS18B20 DallasTemperature sensor uploading data over LoRaWAN to the myDevices/cayenne desktop

I was reading the sensors and uploading data every 10 minutes

you can see the river is tidal – rise and fall of about 10cm

which specific LoRa module do you have?

Noted, thank you!

1 Like

The HC-SR04 is working on ESP32. However, when I include the Lora to transmit the distance/level to another Esp32, I get constant 3 meters appearing on the serial monitor. I used the sx1278 Lora module. How to fix this?

how have you connected (and powered) the modules to the ESP32 - upload a schematic?
have you tested the LoRa module using example programs, e.g.
File>Examples>LoRa>LoRaSender and LoRaReceiver?

remember the ESP32 uses 3.3V logic - directly connecting 5V logic devices to it can damage the device

for monitoring flood levels the waterproof version of the ultrasonic sensor is useful, e.g.

The sx1278 Lora module has an operating voltage of 3.3V which I connected it to the 3.3V pin of the Esp32. I tried first to use a potentiometer on Lora and Esp32 and it worked. The values changes as I rotate the knob. However, when I use the HC-SR04 sensor, I can't get the values from the sensor. I think the problem is in my code.

what is displayed on the transmitter and receiver serial monitors?

what if you comment out the LoRa code in the transmitter, e.g.

void loop() {
  ultrasonic();
  //LoRa.beginPacket();  
  //LoRa.print(level_m);
  //LoRa.endPacket();
  delay(50);
}

does the ultrasonic sensor then work?

how do you power the ultrasonic senor?

Yes, the ultrasonic sensor is working without the Lora code. I powered the ultrasonic sensor on the Vin of Esp32. The problem maybe is the Lora code. Can you help me correct the code?

if commenting out the LoRa code in the transmitter results in the ultrasonic sensor working it could be the LoRa radio transmission is Interfering with the sensor circuits
move the sensor away from the LoRa module?

You could also try reducing the TX power to the minimum of 2dBm.

As I recall that library defaults the TX power to 17dBm, which is probably illegal in a lot of places. 10dBm is a common limit.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.