Remarks

Vl53l0x
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Distance.Vl53l0x
                <img src="https://img.shields.io/nuget/v/Meadow.Foundation.Sensors.Distance.Vl53l0x.svg?label=Meadow.Foundation.Sensors.Distance.Vl53l0x" 
                style="width: auto; height: -webkit-fill-available;" /></a> |

Code Example

Vl53l0x sensor;

public override Task Initialize()
{
    Resolver.Log.Info("Initializing hardware...");

    var i2cBus = Device.CreateI2cBus(I2cBusSpeed.FastPlus);
    sensor = new Vl53l0x(Device, i2cBus, (byte)Vl53l0x.Addresses.Default);

    sensor.DistanceUpdated += Sensor_Updated;

    return Task.CompletedTask;
}

public override Task Run()
{
    sensor.StartUpdating(TimeSpan.FromMilliseconds(250));

    return Task.CompletedTask;
}

private void Sensor_Updated(object sender, IChangeResult<Length> result)
{
    if (result.New == null) { return; }

    if (result.New < new Length(0, LU.Millimeters))
    { 
        Resolver.Log.Info("out of range.");
    }
    else 
    {
        Resolver.Log.Info($"{result.New.Millimeters}mm / {result.New.Inches:n3}\"");
    }
}

Sample project(s) available on GitHub

Wiring Example

To wire a Vl53l0x to your Meadow board, connect the following:

Vl53l0x Meadow Pin
GND GND
SCL D08 (SCL)
SDA D07 (SDA)
VCC 3V3

It should look like the following diagram:

Characteristic Locus
Inheritance System.Object ObservableBase<Length> SamplingSensorBase<Length> PollingSensorBase<Length> ByteCommsSensorBase<Length> > Vl53l0x
Implements ISamplingSensor<Length> ISamplingSensor<Length> IDisposable IRangeFinder
Inherited Members ByteCommsSensorBase<Length>.Peripheral ByteCommsSensorBase<Length>.ReadBuffer ByteCommsSensorBase<Length>.WriteBuffer ByteCommsSensorBase<Length>.Init(Int32, Int32) ByteCommsSensorBase<Length>.Dispose(Boolean) ByteCommsSensorBase<Length>.Dispose() PollingSensorBase<Length>.StartUpdating(Nullable<TimeSpan>) PollingSensorBase<Length>.StopUpdating() SamplingSensorBase<Length>.samplingLock SamplingSensorBase<Length>.Updated SamplingSensorBase<Length>.SamplingTokenSource SamplingSensorBase<Length>.Conditions SamplingSensorBase<Length>.IsSampling SamplingSensorBase<Length>.UpdateInterval SamplingSensorBase<Length>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<Length>.Read() ObservableBase<Length>.observers ObservableBase<Length>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<Length>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Length>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Distance
Assembly Vl53l0x.dll

Syntax

public class Vl53l0x : ByteCommsSensorBase<Length>, ISamplingSensor<Length>, ISamplingSensor<Length>, IDisposable, IRangeFinder

Constructors

Vl53l0x(IDigitalOutputController, II2cBus, IPin, Byte)

Creates a new Vl53l0x object

Declaration
public Vl53l0x(IDigitalOutputController device, II2cBus i2cBus, IPin shutdownPin, byte address = null)

Parameters

Type Name Description
IDigitalOutputController device

Meadow device

II2cBus i2cBus

I2C bus

IPin shutdownPin

Shutdown pin

System.Byte address

VL53L0X address

Vl53l0x(IDigitalOutputController, II2cBus, Byte)

Creates a new Vl53l0x object

Declaration
public Vl53l0x(IDigitalOutputController device, II2cBus i2cBus, byte address = null)

Parameters

Type Name Description
IDigitalOutputController device

Meadow device

II2cBus i2cBus

I2C bus

System.Byte address

I2C address

Properties

Distance

The distance to the measured object.

Declaration
public Length? Distance { get; protected set; }

Property Value

Type Description
System.Nullable<Length>

IsShutdown

Is the hardware shutdown / off

Declaration
public bool IsShutdown { get; }

Property Value

Type Description
System.Boolean

MaximumDistance

Maximum valid distance in mm (CurrentDistance returns -1 if above).

Declaration
public Length MaximumDistance { get; }

Property Value

Type Description
Length

MinimumDistance

Minimum valid distance in mm.

Declaration
public Length MinimumDistance { get; }

Property Value

Type Description
Length

Methods

GetSpadInfo()

Get the spad info

Declaration
protected Tuple<int, bool> GetSpadInfo()

Returns

Type Description
Tuple<System.Int32, System.Boolean>

Initialize()

Initializes the VL53L0X

Declaration
protected Task Initialize()

Returns

Type Description
Task

MeasureDistance()

Tell the sensor to take a measurement

Declaration
public void MeasureDistance()

PerformSingleRefCalibration(Byte)

perform a sensor self calibration

Declaration
protected void PerformSingleRefCalibration(byte vhvInitByte)

Parameters

Type Name Description
System.Byte vhvInitByte

The VHV init byte

RaiseEventsAndNotify(IChangeResult<Length>)

Raise distance change event and notify subscribers

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)

Parameters

Type Name Description
IChangeResult<Length> changeResult

ReadSensor()

Returns the current distance/range

Declaration
protected override Task<Length> ReadSensor()

Returns

Type Description
Task<Length>

The distance in the specified Units. Default mm. Returns -1 if the shutdown pin is used and is off

Overrides

Meadow.Foundation.SamplingSensorBase<Length>.ReadSensor()

SetAddress(Byte)

Set a new I2C address

Declaration
public void SetAddress(byte newAddress)

Parameters

Type Name Description
System.Byte newAddress

ShutDown()

Shut down/power down the sensor

Declaration
public void ShutDown()

ShutDown(Boolean)

Set the Shutdown state of the device

Declaration
public Task ShutDown(bool state)

Parameters

Type Name Description
System.Boolean state

true = off/shutdown. false = on

Returns

Type Description
Task

Events

DistanceUpdated

Distance updated event

Declaration
public event EventHandler<IChangeResult<Length>> DistanceUpdated

Event Type

Type Description
EventHandler<IChangeResult<Length>>