Tutorial: STMicroelectronics VL6180X Time-of-Flight LIDAR Sensor

For many of my applications I need to measure a distance. I have used ultrasonic sensors, but there view angle (beam) is not able to detect smaller objects, it very much depends on the object surface and angle, it is slow and not very precise. I have used infrared sensors, but here again it depends on the infrared reflection of the object in range, it depends the amount of reflected light is not really telling much about the distance, and yet IR reflection is subject of material and object targeted.

But there is yet another sensor type to consider: ToF! ToF (or Time-of-Flight) sensors have a built-in LIDAR: The sensor is sending out light pulses and measures how much time it takes for the light to come back. Similar to ultrasonic sensors (see “Tutorial: Ultrasonic Ranging with the Freedom Board“), but instead of ultrasonic it uses an infrared laser light. Or think about a radar system using an infrared laser light.

Vl6180x Breakout Board with tinyK20 Microcontroller-board

Vl6180x Breakout Board with tinyK20 (NXP Kinetis K20) Microcontroller-board

 

VL6180X

STMicroelectronics calls its technology ‘FlightSense’. It uses a SPAD (Single Photon Avalanche Diode) array and a VCSEL (Vertical Cavity Surface-Emitting Laser), combined with an ALS (Ambient Light Sensor) on the VL6180x. The ToF technology starts to be used in more and more applications: from gesture recognition, human presence detection in public transportation up to detect and measure object distances (e.g. how far a mobile phone is from the ear to adopt the audio volume).

The STMicroelectronics VL6180X sensor is a very small one. Its LGA package makes it a bit difficult to solder it, at least with my SMD soldering skills (my students are by far better!). That’s why breakout modules from Pololu , Sparkfun or Adafruit are a good starting point. Additionally the VL6180X device is operating at 2.8V: the breakout modules include the necessary voltage generator and level shifter to use the module in 3.3V or 5V systems. The sensor uses IR light at 850 nm and the VL6180X comes with an extra ALS (Ambient Light Sensor):

VL6180X Close View

VL6180X Close View

 

The sensor is specified for ranging up to 10 cm with a resolution of 1 mm, but I have found it works fine with the default settings and good conditions up to about 20 cm. There range scaling factor which can be used:

  • 1: ranging up to 20 cm, 1 mm resolution
  • 2: ranging up to 40 cm, 2 mm resolution
  • 3: ranging up to 60 cm, 3 mm resolution

 The range scaling is not mentioned in the data sheet, but mentioned in UM1876.

Wiring

Below a picture of the Pololu breakout module:

 Remove the I2C pull-ups if using multiple sensor boards on the same bus, or if the I2C bus already has pull-ups attached.

Vl6180x Breakout Board Top Side

Vl6180x Breakout Board Top Side

The board has the following pins (left-to-right in the picture below):

  • GPIO1: programmable interrupt output
  • GPIO0/CE: driving this pin low puts the sensor into standby, otherwise it can be used as programmable interrupt output of the sensor. The CE function is needed if multiple sensors are on the same I2C bus (more about this later)
  • SCL: I2C clock
  • SDA: I2C data
  • GND: Ground
  • VIN: supply voltage (2.7-5V)
  • VDD: 2.8V regulator output

VL6180X Breakout Board Bottom Side

VL6180X Breakout Board Bottom Side

Wiring

Using a single sensor, only SCL, SDA, GND and VIN are needed. Below is how I have wired it up with a tinyK20 microcontroller board:

VL6180X Wiring

VL6180X Wiring

I2C Interface

To read and write the device registers over I²C, I’m using the following routines with the GenericI2C (GI2C1) driver:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

uint8_t VL_WriteReg8(uint8_t i2cDeviceAddress, uint16_t reg, uint8_t val) {

  uint8_t r[2];

 

  r[0] = reg>>8;

  r[1] = reg&0xff;

  return GI2C1_WriteAddress(i2cDeviceAddress, &r[0], sizeof(r), &val, sizeof(val));

}

 

uint8_t VL_WriteReg16(uint8_t i2cDeviceAddress, uint16_t reg, uint16_t val) {

  uint8_t r[2], v[2];

 

  r[0] = reg>>8;

  r[1] = reg&0xff;

  v[0] = val>>8;

  v[1] = val&0xff;

  return GI2C1_WriteAddress(i2cDeviceAddress, &r[0], sizeof(r), &v[0], sizeof(v));

}

 

uint8_t VL_ReadReg8(uint8_t i2cDeviceAddress, uint16_t reg, uint8_t *valP) {

  uint8_t tmp[2];

 

  tmp[0] = reg>>8;

  tmp[1] = reg&0xff;

  return GI2C1_ReadAddress(i2cDeviceAddress, &tmp[0], sizeof(tmp), valP, 1);

}

 

uint8_t VL_ReadReg16(uint8_t i2cDeviceAddress, uint16_t reg, uint16_t *valP) {

  uint8_t tmp[2];

 

  tmp[0] = reg>>8;

  tmp[1] = reg&0xff;

  return GI2C1_ReadAddress(i2cDeviceAddress, &tmp[0], sizeof(tmp), (uint8_t*)valP, 2);

}

Sensor Initialization

The sensors need a special initialization sequence, explained in AN4545 and DT0037:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

/* Initialize sensor with settings from ST application note AN4545, section 9 -

   "Mandatory : private registers" */

uint8_t VL_InitDevice(uint8_t i2cDeviceAddress) {

  uint8_t res;

 

  res = VL_WriteReg8(i2cDeviceAddress, 0x207, 0x01);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x208, 0x01);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x096, 0x00);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x097, 0xFD);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0E3, 0x00);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0E4, 0x04);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0E5, 0x02);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0E6, 0x01);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0E7, 0x03);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0F5, 0x02);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0D9, 0x05);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0DB, 0xCE);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0DC, 0x03);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0DD, 0xF8);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x09F, 0x00);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0A3, 0x3C);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0B7, 0x00);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0BB, 0x3C);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0B2, 0x09);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0CA, 0x09);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x198, 0x01);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x1B0, 0x17);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x1AD, 0x00);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x0FF, 0x05);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x100, 0x05);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x199, 0x05);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x1A6, 0x1B);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x1AC, 0x3E);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x1A7, 0x1F);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, 0x030, 0x00);

  if (res!=ERR_OK) {

    VL_OnError(VL_ON_ERROR_INIT_DEVICE);

    return res;

  }

  return ERR_OK;

}

Ranging

The sensor offers multiple modes (continuous and single shot). The easiest one is the single shot ranging. The function below stores the measured value the pointer passed to the function:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

uint8_t VL_ReadRangeSingle(uint8_t i2cDeviceAddress, int16_t *rangeP) {

  uint8_t res;

  uint8_t val;

 

  VL_WriteReg8(i2cDeviceAddress, SYSRANGE__START, 0x01);

  res = readRangeContinuous(i2cDeviceAddress, &val);

  if (res!=ERR_OK) {

    *rangeP = -1;

    return res; /* error */

  }

  if (val>=0 && val<=255) {

    *rangeP = val; /* store value */

    return ERR_OK;

  }

  *rangeP = -2; /* error */

  return ERR_FAILED;

}

 

Because it takes some time until the result is ready, the function below waits until the result is ready:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

static uint8_t readRangeContinuous(uint8_t i2cDeviceAddress, uint8_t *valP) {

  uint8_t range;

  uint8_t res, val;

  uint16_t timeoutMs = 100;

 

  *valP = 0; /* init */

  do { /* breaks */

   res = VL_ReadReg8(i2cDeviceAddress, RESULT__INTERRUPT_STATUS_GPIO, &val);

   if (res!=ERR_OK) {

     break;

   }

   if ((val&0x4)!=0) {

     break; /* 4: New Sample Ready threshold event */

   }

   if (timeoutMs==0) { /* timeout */

     break;

   }

   WAIT1_WaitOSms(1);

   timeoutMs--;

  } while(1);

  if (timeoutMs==0) {

    return ERR_NOTAVAIL; /* timeout */

  }

 

  res = VL_ReadReg8(i2cDeviceAddress, RESULT__RANGE_VAL, &range); /* read range in millimeters */

  if (res!=ERR_OK) {

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, SYSTEM__INTERRUPT_CLEAR, 0x01); /* clear interrupt flag */

  if (res!=ERR_OK) {

    return res;

  }

  *valP = range;

  return ERR_OK;

}

Reading Ambient Light Level

The same principle is used to read the ambient light level:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

static uint8_t readAmbientContinuous(uint8_t i2cDeviceAddress, uint16_t *valP) {

  uint16_t ambient;

  uint8_t res, val;

  uint16_t timeoutMs = 100;

 

  *valP = 0; /* init */

  do {

   res = VL_ReadReg8(i2cDeviceAddress, RESULT__INTERRUPT_STATUS_GPIO, &val);

   if (res!=ERR_OK) {

     break;

   }

   if ((val&0x20)!=0) {

     break; /* new value available */

   }

   if (timeoutMs==0) { /* timeout */

     break;

   }

   WAIT1_WaitOSms(1);

   timeoutMs--;

  } while(1);

  if (timeoutMs==0) {

    return ERR_NOTAVAIL; /* timeout */

  }

 

  res = VL_ReadReg16(i2cDeviceAddress, RESULT__ALS_VAL, &ambient); /* read ambient value */

  if (res!=ERR_OK) {

    return res;

  }

  res = VL_WriteReg8(i2cDeviceAddress, SYSTEM__INTERRUPT_CLEAR, 0x02); /* clear interrupt flag */

  if (res!=ERR_OK) {

    return res;

  }

 

  *valP = ambient;

  return ERR_OK;

}

 

uint8_t VL_ReadAmbientSingle(uint8_t i2cDeviceAddress, uint16_t *ambientP) {

  VL_WriteReg8(i2cDeviceAddress, SYSALS__START, 0x01);

  return readAmbientContinuous(i2cDeviceAddress, ambientP);

}

Using Multiple Sensors

On the MINT Robot (see “MINTomat: World’s Most Complicated Bubble Gum Automata?“) we are using multiple sensors:

Robot with Sensors on each side

Robot with Sensors on each side

3D Printed Cover with Board

3D Printed Cover with Board

 

Because the sensors by default use the same I²C address, the sensors have to enabled one after each other while assigning different I²C device addresses. For this, the CE pin is used to enable one sensor after each other. A GPIO pin (input/output, default after reset as input pin) for each module is used:

ToF CE Pin Configuration

ToF CE Pin Configuration

The code below demonstrates how to do this inside a FreeRTOS task:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

#define VL_NOF_DEVICES  4

#define VL6180X_DEFAULT_I2C_ADDRESS 0x29 /* default address 0101001b (shifted on the bus it is 0101001x */

 

typedef struct {

  int16_t mm; /* distance in mm, negative values are error values */

  uint8_t i2cAddr; /* I2C device address */

} DIST_ToF_DeviceDesc;

 

static DIST_ToF_DeviceDesc ToFDevice[VL_NOF_DEVICES]; /* ToF sensor distance in millimeters */

static uint8_t InitToF(void) {

  uint8_t res;

  int i;

 

  /* initialize data structure */

  for(i=0;i<VL_NOF_DEVICES;i++) {

    ToFDevice[i].mm = 0;

    ToFDevice[i].i2cAddr = VL6180X_DEFAULT_I2C_ADDRESS+1+i; /* make sure they are *not* at the default address! */

  }

  /* disable all devices (CE pin LOW): we will bring them up later one by one.... */

  for(i=0;i<VL_NOF_DEVICES;i++) {

    (void)VL_ChipEnable(i, FALSE); /* disable device */

  }

  for(i=0;i<VL_NOF_DEVICES;i++) {

    (void)VL_ChipEnable(i, TRUE); /* enable device */

    vTaskDelay(pdMS_TO_TICKS(100)); /* give some time to get it enabled */

    res = VL_SetI2CDeviceAddress(i, ToFDevice[i].i2cAddr); /* set hardware I2C address */

    if (res!=ERR_OK) {

      CLS1_SendStr("ERROR: Failed set i2C address of TOF device: ", SHELL_GetStdio()->stdErr);

      CLS1_SendNum8u(i, SHELL_GetStdio()->stdErr);

      CLS1_SendStr("\r\n", SHELL_GetStdio()->stdErr);

      return res;

    }

  }

  /* at this time all devices are enabled (CE pin HIGH) and have unique I2C addresses */

  for(i=0;i<VL_NOF_DEVICES;i++) {

    res = VL_InitAndConfigureDevice(ToFDevice[i].i2cAddr);

    if (res!=ERR_OK) {

      CLS1_SendStr("ERROR: Failed init of TOF device: ", SHELL_GetStdio()->stdErr);

      CLS1_SendNum8u(i, SHELL_GetStdio()->stdErr);

      CLS1_SendStr("\r\n", SHELL_GetStdio()->stdErr);

      return res;

    }

  }

  return ERR_OK;

}

 

static void TofTask(void *param) {

  uint8_t res;

  int errCntr = 0;

  int i;

  bool initDevices = TRUE;

 

  (void)param;

  vTaskDelay(pdMS_TO_TICKS(100)); /* wait to give sensor time to power up */

  /* finished init, run the sensor task */

  for(;;) {

    if (initDevices) {

      do {

        res = InitToF();

        if (res!=ERR_OK) {

          CLS1_SendStr("ToF init failed, retry....!\r\n", SHELL_GetStdio()->stdErr);

          vTaskDelay(pdMS_TO_TICKS(1000));

        }

      } while (res!=ERR_OK);

      CLS1_SendStr("ToF enabled!\r\n", SHELL_GetStdio()->stdOut);

      initDevices = FALSE;

    }

    for(i=0;i<VL_NOF_DEVICES;i++) {

      int16_t range;

 

      range = 0;

      res = VL_ReadRangeSingle(ToFDevice[i].i2cAddr, &range);

      if (res!=ERR_OK) {

        CLS1_SendStr("ToF FAILED!\r\n", SHELL_GetStdio()->stdErr);

        errCntr++;

        GI2C1_Deinit();

        GI2C1_Init();

        initDevices = TRUE; /* re-init devices */

      }

      ToFDevice[i].mm = range;

    } /* for */

    vTaskDelay(pdMS_TO_TICKS(20));

  }

}

Example Application

An example project is available on GitHub. It is a simple bare-metal application which initializes the sensor and then reports the reading on the console/UART:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

#include "Application.h"

#include "VL6180X.h"

#include "LED1.h"

#include "WAIT1.h"

#include "CLS1.h"

 

void APP_Start(void) {

  uint8_t val=0;

  int16_t range;

  uint8_t res;

  uint16_t ambient;

  CLS1_ConstStdIOType *io = CLS1_GetStdio();

 

  VL_Init(); /* initialize sensor driver */

  res = VL_InitAndConfigureDevice(VL6180X_DEFAULT_I2C_ADDRESS);

  if (res!=ERR_OK) {

    CLS1_SendStr("ERROR: Failed init of TOF device: ", io->stdErr);

    CLS1_SendNum8u(VL6180X_DEFAULT_I2C_ADDRESS, io->stdErr);

    CLS1_SendStr("\r\n", io->stdErr);

  }

  for(;;) {

    res = VL_ReadRangeSingle(VL6180X_DEFAULT_I2C_ADDRESS, &range);

    if (res!=ERR_OK) {

      CLS1_SendStr("ERROR Range: ", io->stdErr);

      CLS1_SendNum8u(res, io->stdErr);

    } else {

      CLS1_SendStr("Range: ", io->stdOut);

      CLS1_SendNum8u(range, io->stdOut);

    }

    res = VL_ReadAmbientSingle(VL6180X_DEFAULT_I2C_ADDRESS, &ambient);

    if (res!=ERR_OK) {

      CLS1_SendStr(" ERROR Ambient: ", io->stdErr);

      CLS1_SendNum8u(res, io->stdErr);

    } else {

      CLS1_SendStr(" Ambient: ", io->stdOut);

      CLS1_SendNum16u(ambient, io->stdOut);

    }

    CLS1_SendStr("\r\n", io->stdOut);

    LED1_Neg();

    WAIT1_Waitms(500);

  }

}

 

The output looks like this:

Application output

Application output

Summary

With the STMicroelectronics VL6180X I’m able to measure very accurately distances for robotics and gesture recognition. The VL6180X works up to 60 cm, for longer distances the VL53L0X (up to 2 m) should be considered.

An example project is available on GitHub.

Happy Flying 

LINKS

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值