Do laser distance sensors work like the handheld ones?

Hi,

My Bosch handheld laser distance measurer can measure distances to surfaces that are at a very oblique angle to the beam. Can the laser distance sensors used in microcontroller projects do the same? I had originally tried using ultrasonic sensors for my Arduino project, but they have very low tolerance for the non-perpendicular surfaces I need to measure.

Also I notice that some DF Robot laser distance sensors are described as being for ‘Outdoor’ use, but no mention is made of their IP rating, only their light tolerance is mentioned. Does “outdoor’ imply some degree of waterproofness?

Thanks!

Geoff.

1 Like

Hi Geoff,

Thanks for posting on the forum, the short answer is yes, they’re functionally the same in that they’re both Time-of-Flight sensors that measure distance based on the time taken for light to reflect off of a particular media, not unlike ultrasonic sensors. EngineerSupply has a few articles on it that you may find interesting:

However, the ratings and specifications on handheld distance measuring tools such as your own are likely significantly better with much less error than a VL53L1X for example, as they’re a production-release tool rather than prototyping/experimenting breakouts.

As you’ve noticed, outdoor use in the context of ToF sensors rather than referring to ingress protection instead is often referring to the ability for the sensor to be used in sunlight or high ambient lighting conditions.

The relatively extreme ambient lighting conditions in sunlight across the spectrum can often make it extremely difficult to get any kind of accurate reading. I suspect this DFRobot version that you’re looking at either has a shroud, or some other form of ambient light reduction or possibly operates at a wavelength that has more falloff from sunlight this deep in the atmosphere so it doesn’t have as much of an effect. Do you have the SKU for the sensor/s that you were looking at?

3 Likes

Hi Geoffrey,

I’ve done experiments with the Piicodev distance sensors before, and found out that the measurement gets quite iffy beyond about ± 30 degrees. The problem is that the sensor is picking the strongest reflection, and sometimes that’s actually one that is further than the thing you’re trying to measure your distance to!

If you know you’re going to be sticking under about 30 degrees, you might be fine with “hobby-grade” ToF sensors, but I imagine there’s some much more advanced signal processing going on in your Bosch.

What sort of project are you doing?

-James

3 Likes

Many thanks Bryce and James for your helpful replies!

This is the SKU of the particular sensor I was thinking of, but DFRobot has many others as well.

It doesnt look waterproof, but I imagine one could put it in a waterproof enclosure, as long as the case had a transparent (eg glass) front for the beam to get through. The Bosch unit, at least, doesnt seem to see glass, for example.

The project is to make an alert system that tells my housemate when the car-parking space directly outside our house becomes free. I have already built one, based on an Arduino Nano 33 IoT, and an outdoor ultrasonic sensor. It connects to the Arduino Cloud and uses IFTTT to send him a Telegram message when the change in distance measured is greater than 1 metre. So, in theory, when a car is present, the distance measured is about 2.5m, and when it moves away, it’s about 4 metres, because sensor measures to the road surface of the parking space. That threshold is set by editing the variable one creates in the front end of the Arduino Cloud Thing set-up. I do it there, and not in the sketch loop itself, where it would cause repeated alerts.

The problems with using the ultrasonic sensor are that: (1) If I place it high enough that its beam is not affected by pedestrians walking on the path between the sensor and car, then the beam is too oblique (and much greater than 30 degrees, Im afraid) with respect to the side of any car; (2) I can place it lower (where the angle is less than 30 deg) but even though in some cases the distance change measured when a pedestrian crosses the beam is much less than 1 metre (ie the surface of the pedestrian encountered is roughly perpendicular to the beam), in other cases, the beam deflects off the pedestrian and ends up reflecting back from very distant objects, thus giving false positives. I imagine a laser distance sensor might do likewise at this lower height?

One idea would be to hack a more sophisticated laser sensor, like the Bosch handheld one, and use it in the set-up instead. Im not sure how feasible that is though.

Thanks again for your replies nd those interesting links!

Geoff

2 Likes

Hi Geoff,

I know you’d like to have it perfect, but could you maybe just include some logic that waits for 10-60 seconds of the “free space” signal before it fires off the notification?

A false positive isn’t too bad if it doesn’t actually get conveyed to the user.

-James

1 Like

Hi James,

No need for perfection, and I would be very satisfied for the unit do what you are suggesting.

Currently, in my project the alert logic sits outside the loop, in the ‘front end’ of the Arduino Cloud set-up. This has the advantage of using a logic (basically, ‘any two consecutively measured distances vary by more than a user-defined threshold’) that does not trigger multiple alerts when the car space becomes free. But one does not have access to the coding of that logic, one is stuck only with that simple option, which of course is also triggered by pedestrians.

In an earlier version, I had my logic in the sketch’s loop (where it was, basically, ‘send an alert if the distance measured is greater than 3 metres’, but that ended up having two problems: (1) The issue of the beam bouncing off pedestrians; (2) the alert being sent over and over, because of the repetitive nature of the loop. Other people who have tried analogous alerts have also reported that second issue, and not reported any solution to restricting alerts to a single instance.

I cant as yet see a way to set up the logic so that distances are measured against each other, or, perhaps, averaged, and thus achieve what you suggest (and also avoiding repeated alerts). I’m not familiar with setting up such conditionals, so if you can provide any pointers I would be very appreciative.

Thanks once more!

Geoff.

1 Like

Hi Geoff,

No prob! If you post your code here with 3 backticks (above the tab key) on the first and last line, it’ll show up in a nice, scrollable format:

like so

void loop(){
    //Code goes here
    Serial.println("Hello world");

We’ll take a look at your code, and I might be able to suggest some better logic for you.

-James

1 Like

Hey James,

The code is below, and as you will notice there is no actual triggering logic in it. The logic itself is in the Arduino IoT Cloud set-up of the ‘Thing’ and I’ve included some screenshots of that, in case youre not familiar. In this case, Ive set the threshold to 210, which means that IFTTT will send a message if the change in distance measured is 210 cm or more. There is no reason the triggering logic cant be in the sketch itself, but I dont know how to get it to work as required there. Using the Arduino Cloud set-up, the final sketch is a mix of user- and auto-generated code.

Many thanks!!

Geoff

  Sketch generated by the Arduino IoT Cloud Thing "New Nano Distance"
  https://create.arduino.cc/cloud/things/8dd917a9-6826-41d1-8cd8-a19671b26b44 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  int distance;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

const int trigPin = 3, echoPin = 4;
long duration;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(5, OUTPUT);
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  

  
  // Your code here 
  delay(10);
  digitalWrite(trigPin, LOW); delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);//determine duration to calculate distance 
  distance = duration*0.034/2;
delay(100);
        
}

/*
  Since RandomValue is READ_WRITE variable, onRandomValueChange() is
  executed every time a new value is received from IoT Cloud.
*/
/*
  Since Distance is READ_WRITE variable, onDistanceChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onDistanceChange()  {
  // Add your code here to act upon Distance change
}


1 Like

Hi Geoff,

I wish my Arduino experience extended into their cloud!

I think I get how it works, you can configure variables in your dashboard that get declarations and functions automatically placed in the code, then you can add to that with your own code - correct me if I’m wrong.

One thing I’d like to know is how you’ve got IFTTT integrated, as I can’t see anything in the “on-change” function in the code that points to IFTTT. Knowing this will help me mold your code into what you want.

Though even without knowing how that works, I should be able to get you a high-level overview of how I think it should work:

float measureDistance(){
  // Put your measure distance in a function that returns a float - you'll be using it a lot
  digitalWrite(trigPin, LOW); delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);//determine duration to calculate distance 
  return (duration*0.034/2);
}


const int emptySpaceDistance = 1000; //I'm not sure what the real distance is here, but set it to the lowest value your sensor will see with a true empty space
bool spaceIsEmpty = true; //this is the variable you want to watch with your cloud stuff and send texts based on
int count = 0;
void loop() {
  while (spaceIsEmpty){
    //ArduinoCloud.update();
    if (measureDistance <= emptySpaceDistance){ //if measured distance is less than threshold, start counting
      count++;
    } else {
      count = 0; //reset the counter 
    }
    if (count == 10) {
      spaceIsEmpty = false; // flip the status once you reach 10 consecutive seconds of a reading above the threshold
    }
    delay(1000); //wait 1s between checks
  }
  
  while (!spaceIsEmpty){ //same arrangement as above but for the opposite state
    if (measureDistance >= emptySpaceDistance){
      count++;
    } else {
      count = 0;
    }
    if (count == 10) {
      spaceIsEmpty = true;
    }
  }
  delay(1000);
}

It’s up to you to integrate this with the cloud dashboard, as I’m out of ideas there, but let me know if this gives you more questions!
-James

Hey James

Sorry for the long delay, I was away over Easter, and couldnt check some things with the set-up.

Many, many thanks for the code, which I do get the idea of, as a way of preventing multiple alerts. However, afaik, there is no way of interfacing that approach with the Arduino Cloud front end and its IFTTT component. Ive added one screen capture that shows the ‘webhook’ (at very bottom of page) that is triggered when the change in distance measured exceeds the threshold.

That front end section is very inflexible in the logic it can work with, so it would probably be better for me to avoid it altogether, and use other purely sketch-based approaches to send the alert, eg here are two very different but possible ways:

Once I solve that, then I will also be a position to use your code.

If you have any suggestions re a code-based alert, of course I would also be very happy to hear them!

Edit: This seems probably the best way to use the existing IFTTT webhook, meanwhile avoiding the restrictions of the Arduino Cloud front end.

All the best,

Geoff

2 Likes