Using Ultrasonic sensor with Lora Ra-02 ( or SX1276)

A couple of years ago I posted a request for help putting together a wireless ultrasonic sensor to monitor the water level in the well/tanks at our cottage. In the past few years I've put together a few efforts that had moderate success. I designed and tested a wireless/sensor with alarm and emergency shutoff that works pretty well - in the spring and winter - when the foilage isn't too dense. It's designed around the NRF24101 transceiver. I'm going to put the design up later this fall.

The problem is it can't cut through thick foilage: I've just tried out a Ra-02 transceiver. I found an easy setup to send and receive temp/humidity and put it together and took it up to the well. It seems to broadcast pretty well - which was a pleasant surprise.

MY PROBLEM is there is almost no info marrying the Ra-02 with an ultrasonic sensor. I've spent hours googling, without result. There is a couple that do use it (or SX1276) to broadcast to a gateway... but since we have no internet at the cottage, that isn't an option. I just want to send the resulting data from the sensor to a receiver - directly.

I realize that the SX1276 sends packets in a somewhat slower way than the NRF24101 sends data - but that's not an issue. I don't want to track a speeding car - just water level, so there can be up to several minutes between resends.

I'll keep working on it, trying to marry the LoRa library examples with the NewPing library. If anybody has some experience doing this I'd love to know about it. I'll be happy to share my final results.
Thanks
Jeff

Dr_Bundolo:
A couple of years ago I posted a request for help putting together a wireless ultrasonic sensor to monitor the water level in the well/tanks at our cottage.

Better post a link to it then, so we don't have to ask old questions.
Continuing that post would have been better.
Leo..

Should be able to extract tank level from the pressure on the incoming feed line, inside the cottage.
Assuming there is no flow/pumping most of the time.
Leo..

Wawa:
Should be able to extract tank level from the pressure on the incoming feed line, inside the cottage.
Assuming there is no flow/pumping most of the time.
Leo..

That takes the fun out of learning to use LoRa.

Dr_Bundolo:
MY PROBLEM is there is almost no info marrying the Ra-02 with an ultrasonic sensor.

Well, maybe that's because... you don't?

Have your Arduino read the ultrasound sensor.
Have your Arduino transmit the reading over the LoRa.
Have another Arduino listen to incoming LoRa transmissions and subsequently show it on a display or whatever you want to do with it.
Just like you would do any other sensor-data-over-wireless project.

wvmarle:
Well, maybe that's because... you don't?

Have your Arduino read the ultrasound sensor.
Have your Arduino transmit the reading over the LoRa.
Have another Arduino listen to incoming LoRa transmissions and subsequently show it on a display or whatever you want to do with it.
Just like you would do any other sensor-data-over-wireless project.

Similar to what I have in a project that monitors temperature from way out in a vineyard (400m) and a house with USB plugpack and a OLED display and alarm beeper.

It is undergoing evaluation at the moment, but I used

Tx SX1276 and ProMini and DS18B20 sensor, possible PV charger.
Rx SX1276 and ESP32 and OLED display.

ESP32 for further expansion to WiFi display, Cayenne implementation.

A ProMini at each end would work also.

Google;
promini sx1278

and

promini hc-sr04

Should give you enough to see how it would come together.

Tom... :slight_smile:

Dr_Bundolo:
I've just tried out a Ra-02 transceiver. I found an easy setup to send and receive temp/humidity and put it together and took it up to the well. It seems to broadcast pretty well - which was a pleasant surprise.

MY PROBLEM is there is almost no info marrying the Ra-02 with an ultrasonic sensor.

Not sure I see the problem.

You have the LoRa link working sending the numbers from the 'temp/humidity' sensor but you cannot send the numbers from the ultrasonic sensor ?

Hi,
What controller are you using with the Lora unit?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Can you post a copy of your code that you have working with the Lora unit?

Thanks.. Tom.. :slight_smile:

TomGeorge:
It is undergoing evaluation at the moment, but I used

Tx SX1276 and ProMini and DS18B20 sensor, possible PV charger.
Rx SX1276 and ESP32 and OLED display.

ESP32 for further expansion to WiFi display, Cayenne implementation.

Very similar to my setup apart from me using Lora32u4 II modules.
I also use a Heltec Lora 32 with OLED as the gateway. Initially to post results to Cayenne but now I run a RPi with Influx DB & Chronograf.

I know what you mean about undergoing evaluation. This has been going on for a couple of years as I change the code to preserve battery life and then see how long it lasts. Winters for me do impact battery life, a single 18650 battery only lasted 6 months over winter.

Thank you all for your replies. I have managed to get a rough system working: Here is the TX side:

// ask_transmitter.pde
// -- mode: C++ --
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module

#include<LoRa.h>
#include <SPI.h> // Not actually used but needed to compile

#include <NewPing.h>

#define TRIGGER_PIN 7 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 8 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup(){
Serial.begin(9600);
while (!Serial);

Serial.println(“LoRa Receiver”);

if (!LoRa.begin(433E6)) {
Serial.println(“Starting LoRa failed!”);
while (1);

LoRa.setTxPower(20);
}
}

void loop(){

const char ping_cm;

delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.println("Ping: ");
Serial.print(sonar.ping_cm()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println(“cm”);
LoRa.beginPacket(); ///send packet
LoRa.print(sonar.ping_cm());
LoRa.endPacket();
delay(200);
}

the RX side is:

#include<LoRa.h>
#include <SPI.h> // Not actually used but needed to compile
String readString;
char data = 0;
void setup() {
{
Serial.begin(9600);
while (!Serial);

Serial.println(“LoRa Receiver”);

if (!LoRa.begin(433E6)) {
Serial.println(“Starting LoRa failed!”);
while (1);
LoRa.setTxPower(20);
}
}
}

void loop() {
// try to parse packet
int packetSize = LoRa.parsePacket();
if (packetSize) {
// received a packet
Serial.print("distance is ");

// read packet
while (LoRa.available()) {
Serial.print((char)LoRa.read());
}

Serial.print(" cm “);
Serial.println(” ");

(char)LoRa.read()= distance;
}
}

I’m using two RA-02 units and two unos.

What I’d like to do now if finding a way to convert the data that is printed in the line:
Serial.print((char)LoRa.read());
after it’s received to some easy to use value. Something like
(char)LoRa(read)() = distance();
then I can manipulate that received data in if statements (to trigger alarms).
I’ve tried all sorts of variations but can’t come up with the magic equation. Can you guys help?
Jeff

You should really read the instructions on how to post your code correctly on this forum.

An observation on your code. The line LoRa.setTxPower(20); will never be executed.

I have not tried but in theory the line int distance = LoRa.parseInt(); should work.

Its extremly unlikley that you need to use such a high level of power as 20dBm, 100mW, and to do so might well be illegal.

On an equal data rate basis a 434Mhz LoRa module should have a link advantage over a 2.4Ghz NRF24L01 of 40dB or more, so about 100 times range.

Set the power level to minimum, 2dBm, and see how well it works.

If you want more range increase the spreading factor, going from spreading factor 7 to spreading factor 12 will increase range by about 4 times. The packets would then take longer to send, but if the updates are only needed every few minutes thats not a problem.

thank you again for your replies. My apologies for not posting the code correctly. Here are the two sketches - hopefully in the correct format. My problem is the formatting options aren’t visible in the quick reply.

This is my first experience with the Ra-02. I’ve spent the last couple of years working with the NRF24101. After my disapointment with that transceiver to cut through the brush, I immediately went for high power. I’ll dial it back and see if it works. We are in a fairly remote area so I’m not overly concerned with interference - but it doesn’t hurt to be careful and will take less power…

On my NRF24101 attempts, I have a fairly sophisticated system that not only monitors the depth, but will sound an alarm if the water drops beyond a preset level - and a further option to shut the system off.

Once I determine that the LoRa will work, I’ll try and replicate the bells and whistles.

Again thanks for your help, and I welcome your suggestions.
Jeff

TX code

// ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module


#include<LoRa.h>
//#include <SPI.h> // Not actually used but needed to compile

#include <NewPing.h>

#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     8  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.




void setup(){
Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);

// LoRa.setTxPower(20);
  }  
}

void loop(){


  
delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Distance is: ");
Serial.print(sonar.ping_in()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println(" inches");
LoRa.beginPacket(); ///send packet
LoRa.print(sonar.ping_in());
LoRa.endPacket();
delay(200);
}

RX Code

#include<LoRa.h>
#include <SPI.h> // Not actually used but needed to compile


void setup() {
  {
Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

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

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("distance is ");

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());  
     }

 
Serial.print(" cm ");
 Serial.println(" ");


  }
}

...also any suggestions about SetSignalBandwidth?

Dr_Bundolo:
...also any suggestions about SetSignalBandwidth?

Information on bandwidth here;

What is LoRa

Once I determine that the LoRa will work, I'll try and replicate the bells and whistles.

why do you need to rip up a program your happy with and start again? If all you are doing is replacing the transmitter/receiver then keep the original code but just modify/change the part to send & receive the values.

tnx. Excellent tutorial. Explains a lot.

Hi:
Thanks for the help. I’ve inserted the statement:
LoRa.setSpreadingFactor(10);
as per Riva’s suggestion. Instead of getting a two decimal readout like:
Distance is: 66 inches
Distance is: 66 inches
Distance is: 66 inches
Distance is: 66 inches

I get:
distance is:566676756667675666767566676756667675666767566676756667675666767 cm
distance is: 56667675666767566676756667675666767566676756667675666767566676766 cm
distance is: 56667675666767566676756667675666767566676756667675666767566676766 66667 cm

Again, here is the receive program (the Tx program is the same)

#include<LoRa.h>
#include <SPI.h> // Not actually used but needed to compile


void setup() {
  {
Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
   //  LoRa.setTxPower(20);
}
//LoRa.setSpreadingFactor(10);
LoRa.setSignalBandwidth(62.5E3);
//LoRa.crc();
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("distance is ");

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());  
     }

 
Serial.print(" cm ");
 Serial.println(" ");
//int distance = LoRa.parseInt();

  }
}

Again thanks for any help and advice in improving the program
Jeff

Dr_Bundolo:
Thanks for the help. I've inserted the statement:
LoRa.setSpreadingFactor(10);
as per Riva's suggestion.
Not my suggestion. :confused:
I see it in the code but it is commented out. Are both TX & RX sketches the same with regard to Lora setup?

(Sigh)
A new day a new problem. The long integer printout problem has mysteriously disapeared. YAY. I’ve moved on to working on the simple LED alarm I had working with the NRF24L01 model. It doesn’t seem to work when I’ve transfered the snippit of code to the LoRa version.

I’m enclosing the code for both the transmitter and receiver for inspection. Again thanks for the help.

TX code:

  // ask_transmitter.pde
// -*- mode: C++ -*-
// Simple example of how to use RadioHead to transmit messages
// with a simple ASK transmitter in a very simple way.
// Implements a simplex (one-way) transmitter with an TX-C1 module


#include<LoRa.h>
//#include <SPI.h> // Not actually used but needed to compile

#include <NewPing.h>

#define TRIGGER_PIN  7  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     8  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.




void setup(){
Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);

// LoRa.setTxPower(20);
  }
LoRa.setSpreadingFactor(10);
LoRa.setSignalBandwidth(62.5E3);  
//LoRa.crc();
}

void loop(){


  
delay(50);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Distance is: ");
Serial.print(sonar.ping_in()); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println(" inches");
LoRa.beginPacket(); ///send packet
LoRa.print(sonar.ping_in());
LoRa.endPacket();
delay(200);
}

RX code

#include<LoRa.h>
#include <SPI.h> // Not actually used but needed to compile

/*-----( Declare Constants and Pin Numbers )-----*/
int ledPin = 6;

void setup() {
  {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
  while (!Serial);
  Serial.println("LoRa Receiver");
  if (!LoRa.begin(433E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
}
LoRa.setSpreadingFactor(10);
LoRa.setSignalBandwidth(62.5E3);
//LoRa.crc();
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
  Serial.print("distance is ");

    // read packet
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());  
     }
Serial.print(" cm ");
Serial.println(" ");
float targetDistance = LoRa.parseInt();

{
  if (targetDistance <=25 ) 
        {
              digitalWrite(ledPin,LOW);
        }
  else {
         digitalWrite(ledPin,HIGH);
   }
  }
  }
}