3.1. Using the Distance Sensor

3.1.1. Basic Example

Before going to the more advanced example program of using the Distance Sensor, we’re going to give an example of the easiest way to read from the sensor.

The following code snippet reads values off of the Distance Sensor and prints them iteratively in the console. As you’ll see, this is far easier than the following examples, which are more complex to use, but have a more granular control over the device.

In this example program, connect the Distance Sensor to an I2C port on whichever platform (GoPiGo3, GrovePi or BrickPi3) and then run the following script.

# import the modules
from di_sensors.easy_distance_sensor import EasyDistanceSensor
from time import sleep

# instantiate the distance object
my_sensor = EasyDistanceSensor()

# and read the sensor iteratively
while True:
  read_distance = my_sensor.read()
  print("distance from object: {} cm".format(read_distance))

  sleep(0.1)

The source file for this example program can be found here on github

3.1.2. Continuous-mode

Again, just like in the previous example program, connect the Distance Sensor to an I2C port on whichever platform before running the following script.

The advantage of this script over the ones in the following and previous sections is that the time taken for reading the distance can be fine-tuned by the user - for instance, it can be made to run as fast as possible (to see how fast it can read see the API of DistanceSensor) or it can be made to go very slow. Each fine-tune has its benefits and disadvantages, so the user has to experiment with the sensor and determine what setting suits him best.

import time
from di_sensors.distance_sensor import DistanceSensor

print("Example program for reading a Dexter Industries Distance Sensor on an I2C port.")

# establish communication with the DistanceSensor
ds = DistanceSensor()

# set the sensor in fast-polling-mode
ds.start_continuous()

while True:
    # read the distance in millimeters
    read_distance = ds.read_range_continuous()
    print("distance from object: {} mm".format(read_distance))

The source code for this example program can be found here on github.

3.1.3. Single-mode

In this third example, we have the same physical arrangement as in the second one, the only difference being in how we communicate with the sensor. This time, we take single-shot readings, which for the user is simpler than having to tune the distance sensor first and then read off of it. The only disadvantage is that there’s no fine-control over how fast the sensor is making the readings.

import time
from di_sensors.distance_sensor import DistanceSensor

print("Example program for reading a Dexter Industries Distance Sensor on an I2C port.")

ds = DistanceSensor()

while True:
    # read the distance as a single-shot sample
    read_distance = ds.read_range_single()
    print("distance from object: {} mm".format(read_distance))

The source code for this example program can be found here on github.

3.1.4. Console Output

All 3 example scripts described in this chapter should have a console output similar to what we have next.

distance from object: 419 mm
distance from object: 454 mm
distance from object: 452 mm
distance from object: 490 mm
distance from object: 501 mm
distance from object: 8190 mm
distance from object: 1650 mm
distance from object: 1678 mm
distance from object: 1638 mm
distance from object: 1600 mm