---
description: >-
  Measurement of distance within the range 1 mm to 1300 mm for cliff detection
  using VL53L4CD.
---

# Cliff Detection with VL53L4CD

## INTRODUCTION

The B3RB can use a TOF distance sensor as a cliff detector and the appropriate software to prevent it from driving off a table, down a stairway or any other "cliff" it may encounter while moving forward. These sensors could also be used pointing in other directions for other proximity detection applications. This tutorial describes how to perform cliff detection on MR-B3RB using TOF VL53L4CD sensor with MR-CANHUBK344 and MR-CANHUBK3-ADAP boards. \
\
The example and configuration shown below is with the MR-CANHUBK344 running Zephyr and Cognipilot but is showing the board outside of the installation on a MR-B3RB. When used with a B3RB, the power is supplied in the normal fashion and not with external connectors shown.

### Operation Explanation

The Time of Flight VL53L4CD sensor measures distance within the range 1 mm to 1300 mm.

The sample application "samples/sensor/vl53l4cd" polls on the sensor every 1 second and reads the current distance measured from the device. Then it prints the distance on the console (in meters).

## ITEMS REQUIRED

{% hint style="info" %}
All items except 3 (Cable), 4 (TOF sensor) and 10 (5V DC Power supply) are included with [MR-B3RB-S](../archive/whats-in-the-box-pre-production-version/witb-mr-b3rb-s).
{% endhint %}

{% hint style="warning" %}
Re: item no. 9, [MR-CANHUBK344](../archive/whats-in-the-box-pre-production-version/witb-mr-canhubk344) kit that comes with [MR-B3RB-S](../archive/whats-in-the-box-pre-production-version/witb-mr-b3rb-s), includes multiple cables with "SYP" connector which could be utilized for supplying power to the MR-CANHUBK344.
{% endhint %}

<table data-full-width="false"><thead><tr><th width="100" align="center">S.NO.</th><th align="center">NAME</th><th width="100" align="center">QTY</th></tr></thead><tbody><tr><td align="center">1</td><td align="center"><a href="https://www.nxp.com/part/MR-CANHUBK344">MR-CANHUBK344</a></td><td align="center">1</td></tr><tr><td align="center">2</td><td align="center"><a href="https://nxp.gitbook.io/mr-canhubk344/software/px4-autopilot/mr-canhubk3-adap">MR-CANHUBK3-ADAP</a></td><td align="center">1</td></tr><tr><td align="center">3</td><td align="center"><a href="https://robu.in/product/adafruit-stemma-qt-qwiic-jst-sh-4-pin-cable-100mm-long/">STEMMA QT / Qwiic JST SH 4-pin Cable – 100mm</a></td><td align="center">1</td></tr><tr><td align="center">4</td><td align="center"><a href="https://www.adafruit.com/product/5396">TOF VL53L4CD</a></td><td align="center">1</td></tr><tr><td align="center">5</td><td align="center"><a href="../../archive/whats-in-the-box-pre-production-version/witb-mr-b3rb-bmf#j-link-edu-mini-debugger">J-Link EDU MINI DEBUGGER</a> (Complete set)</td><td align="center">1</td></tr><tr><td align="center">6</td><td align="center"><a href="https://nxp.gitbook.io/hovergames/rddrone-fmuk66/connectors/debug-interface-dcd-lz/dcd-lz-breakout-board">DCD-LZ Breakout Board</a></td><td align="center">1</td></tr><tr><td align="center">7</td><td align="center"><a href="https://nxp.gitbook.io/mr-b3rb/whats-in-the-box/witb-mr-b3rb-bmf#cable-custom-pdb-to-canhubk3-s.no.-29-cable-usb-ttl-ftdi-ttl-232r-3v3-s.no.-4-cable-servo-extension">CABLE, USB-TTL, FTDI TTL-232R-3V3, 6 PIN CONNECTOR</a></td><td align="center">1</td></tr><tr><td align="center">8</td><td align="center"><a href="https://nxp.gitbook.io/mr-b3rb/whats-in-the-box/witb-mr-b3rb-bmf#dcd-lz-adapt-s.no.-3-7-position-jst-gh-connector-100-mm-s.no.-27-xt60-female-to-male-deans-t-plug-ad">CABLE, FOR DCD-LZ, 7 PIN, JST-GH CONNECTOR, 100 MM</a></td><td align="center">1</td></tr><tr><td align="center">9</td><td align="center"><a href="https://www.aliexpress.com/i/1023818884.html">CABLE, SYP CONNECTOR</a> for supplying power to MR-CANHUBK344</td><td align="center">1</td></tr><tr><td align="center">10</td><td align="center">DC Power Supply (any) for supplying 5V to MR-CANHUBK344</td><td align="center">1</td></tr></tbody></table>

## SETUP DIAGRAMS

<figure><img src="https://396338415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FU93yDWZcgjXGgsC1Duqv%2Fuploads%2FNJ2ecnUbjjJcdY2SAbm4%2Fblock_diagram.svg?alt=media&#x26;token=d86313c9-40d6-4a3e-ad77-4206deb9213d" alt=""><figcaption><p>Block Diagram</p></figcaption></figure>

<div><figure><img src="https://396338415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FU93yDWZcgjXGgsC1Duqv%2Fuploads%2FjzdLyNLIDmgrd76hscrp%2Fcanhub3-adap.jpg?alt=media&#x26;token=990cc718-b73e-4d1c-839f-122061b90bea" alt=""><figcaption><p>MR-CANHUBK3-ADAP</p></figcaption></figure> <figure><img src="https://396338415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FU93yDWZcgjXGgsC1Duqv%2Fuploads%2FybyWK5bcd5yOPGabWQbT%2Fcanhubk3%20overview.jpg?alt=media&#x26;token=7a75c7bd-04c3-4595-976a-54e5639b9bbf" alt=""><figcaption><p>MR-CANHUBK344 (Zoomed out)</p></figcaption></figure> <figure><img src="https://396338415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FU93yDWZcgjXGgsC1Duqv%2Fuploads%2FuO5gbnJlvzYAkbMT9mVh%2Fcanhubk344.jpg?alt=media&#x26;token=e7247629-db1f-4bdc-971d-ae5775a642f1" alt=""><figcaption><p>MR-CANHUBK344 (Zoomed in)</p></figcaption></figure> <figure><img src="https://396338415-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FU93yDWZcgjXGgsC1Duqv%2Fuploads%2F8lypWlIAxlJX4pt76fvk%2Fcomplete_setup.jpg?alt=media&#x26;token=46076242-5ff9-4c67-8a41-bc5797624207" alt=""><figcaption><p>Complete Setup</p></figcaption></figure></div>

## HARDWARE SETUP

This hardware setup shows these boards outside of MR-B3RB on a tabletop. This of course can also be done on the vehicle itself. Simply plug in the sensor and the STEMMA QT cable to the CANHUB-ADAP. The power and other connections do not change from a normal MR-B3RB configuration.

{% hint style="info" %}
The JLINK can alternatively connect to the small DCD-LZ board  if preferred.

Power may be supplied using either of the power connectors. In an assembled MR-B3RB this would be using the 5 pin JST-GH power in cable rather than the SYP cable.
{% endhint %}



1. Connect hardware components as shown in the block diagram above. The CANHUB-ADAP includes a I2C connector that follows the ADAFRUIT STEMMAQT standard.
2. Attach the MR-CANHUBK3-ADAP board on top of MR-CANHUBK344.
3. Power on the MR-CANHUBK344 board - connect the SYP connector to the MR-CANHUBK344, and the other end of the cable to your bench power supply or any source that could provide 5V DC.

## SOFTWARE SETUP

### Setup West Workspace

```
cd ~
west init -m "https://github.com/NXPHoverGames/zephyr.git" cliff_detection_project

cd ~/cliff_detection_project
west update
```

### Building and Flashing

{% code lineNumbers="true" %}
```
cd ~/cliff_detection_project/zephyr
git checkout tof_vl53l4cd_with_mux_pr

west build -b mr_canhubk3 samples/sensor/vl53l4cd -p
west flash
```
{% endcode %}

## RESULTS

Get the shell of MR-CANHUBK344.

```
sudo chmod 666 /dev/ttyUSBn
cu -l /dev/ttyUSBn -s 115200
```

The shell should display output as:

```
proximity = 1
distance = 0.019 m
proximity = 1
distance = 0.021 m
proximity = 1
distance = 0.019 m
proximity = 1
distance = 0.018 m
proximity = 1
distance = 0.020 m
proximity = 1
distance = 0.017 m
proximity = 1
distance = 0.018 m
proximity = 1
distance = 0.019 m
proximity = 1
distance = 0.020 m
proximity = 1
distance = 0.018 m
proximity = 1
distance = 0.020 m
proximity = 1
distance = 0.018 m
proximity = 1
distance = 0.020 m
proximity = 1
distance = 0.020 m
proximity = 1
distance = 0.019 m
proximity = 1
distance = 0.016 m
```
