Ultrasonic = UltraWeirdness

I would have a more descriptive title if I wasn't so confused.

The scene: Uno with a JSN-SR04T ultrasonic sensor and a
REYAX RYLR998 LoRa module. Sensor is 4ft above the water looking down to measure wave height, then send to receiver 400ft away. 10 samples/sec.

Nano Every with identical LoRa module + 128x64 OLED to show waves graphically and numerically. This is the receive side.

I've had all pieces of this thing working individually and when putting all the code together in one place things were going fine. Until they weren't. I suddenly started getting alternate readings from the transmit side, one good then one bad. I have reduced the code to a minimum to try to focus on the problem. First the transmitter.

void loop() {  // Transmit code.

  // First thing we do is sync up to the next 100mS. OK to be accurate here.
  while (micros() - ET < 100000) {
  }
  ET = micros();

  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  long duration, feet, inches, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(30);
  digitalWrite(trigPin, LOW);

  digitalWrite(LED_BUILTIN, HIGH);
  duration = pulseIn(echoPin, HIGH);
  digitalWrite(LED_BUILTIN, LOW);
  //Serial.print(duration + "  ");  //When this line UNcommented, monitor shows random garbage.
  duration = duration / 2;
  if (duration < 1000) duration = 1000;  // Only range data of interest is 2000-6000.
  if (duration > 9999) duration = 9999;
  lora.println("AT+SEND=" + house_address + ",4," + duration);    // Send it (19mS)
  Serial.println("AT+SEND=" + house_address + ",4," + duration);  // Show what we sent to Lora.
}

And this is the data sent to the LoRa module (duplicated and sent to the monitor):
PWxmitATsend

As you can see, the distance (in uS) alternates with every transmission, from around 1960 to 4360. The 4360 is the correct value (from bouncing off a wall about 4 ft away). I can't blame the sensor since the scope trace clearly shows a steady and accurate pulse width and repetition rate. Shown here:

This is what is received by the receiving LoRa (the isolated number on the left is execution time for the main loop):

And this is the receiving code:

void loop() {     // Receive code.
  ET = millis();  //For timing the loop.

  if (lora.available()) {
    incomingString = lora.readString();
    // char dataArray[30];
    // incomingString.toCharArray(dataArray, 30);
    // char* data = strtok(dataArray, ",");
    // data = strtok(NULL, ",");
    // data = strtok(NULL, ",");

    // //if (strcmp(data, "1000") == 0) {
    digitalWrite(piezo, HIGH);
    delay(10);
    digitalWrite(piezo, LOW);
    // //}

    //ET = millis() - ET;
    Serial.print((millis() - ET));  // Elapsed time for loop, mS.
    Serial.print("    ");
    Serial.println(incomingString);
  }
}

The 1960 distance value is wrong. Should be around 4300. And why isn't the 4300 distance not printing at the monitor at all? (assuming it was received). And why the blank line where the 4300 line should be?


The only #include is SoftwareSerial; both the transmitter and receiver.

I hope I have described the problem clearly. If anyone has any idea of what's going on I would appreciate some suggestions.

You set it LOW twice but HIGH only once in the loop?

You seem to receive 2 echos of the same pulse. Increase the time between sending pulses.

I share your question about that. It's what you see in almost every example of a pinger. The pin should start as LOW since it was set that way at bootup. I just copied right along since it was in working examples.

If I'm getting 2 echos from the same pulse I would still get 2 echos from the same pulse even if it was 1 ping per day. Anyway, I've set it up with the perfect conditions... clear path to the wall and nothing either side or above or below. Also, look at the scope trace. That IS the echo, and only 1 echo per transmit pulse.

From the REYAX LoRa AT Command Guide:
Recommend to set “AT + PARAMETER = 9,7,1,12”
So that's what I did. I think that's when things went south. I previously had the parameters set to 7,8,4,12 which had been working for some time before I changed it. I just now changed it back (to 7,8,4,12) and now I'm getting just a single value sent by the transmitter side. As a welcomed side effect it also is receiving 10 messages/sec like I had, and needed. With the bad parameters I was getting just 5, and planned to address that problem once this weirdness went away.

So... all is well on the Indian River Lagoon. For now.

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