├── README.md ├── .gitignore └── VL53L0Xtest.py /README.md: -------------------------------------------------------------------------------- 1 | # VL53L0X 2 | Tools to interface ToF ranging sensor VL53L0X from ST Microelectronics. 3 | 4 | From product page: 5 | ## World smallest Time-of-Flight \(ToF\) ranging sensor 6 | The VL53L0X is a new generation Time-of-Flight \(ToF\) laser-ranging module housed in the smallest package on the market today, providing accurate distance measurement whatever the target reflectances unlike conventional technologies. It can measure absolute distances up to 2m, setting a new benchmark in ranging performance levels, opening the door to various new applications. 7 | 8 | Started from C API provided with STSW-IMG005 9 | 10 | The VL53L0X breakout board \(called satellite\), can be found as a part of NUCLEO-53L0A1 evaluation package or separately \(53L0-SATEL-I1\) is connected to RPI GPIO interface 11 | SDA, SCL, 3.3V and GND 12 | 13 | Provided code is Python for Raspberry Pi. 14 | Uses smbus library. 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /VL53L0Xtest.py: -------------------------------------------------------------------------------- 1 | import smbus 2 | import struct 3 | import time 4 | 5 | def bswap(val): 6 | return struct.unpack('H', val))[0] 7 | def mread_word_data(adr, reg): 8 | return bswap(bus.read_word_data(adr, reg)) 9 | def mwrite_word_data(adr, reg, data): 10 | return bus.write_word_data(adr, reg, bswap(data)) 11 | def makeuint16(lsb, msb): 12 | return ((msb & 0xFF) << 8) | (lsb & 0xFF) 13 | def VL53L0X_decode_vcsel_period(vcsel_period_reg): 14 | # Converts the encoded VCSEL period register value into the real 15 | # period in PLL clocks 16 | vcsel_period_pclks = (vcsel_period_reg + 1) << 1; 17 | return vcsel_period_pclks; 18 | 19 | 20 | 21 | VL53L0X_REG_IDENTIFICATION_MODEL_ID = 0x00c0 22 | VL53L0X_REG_IDENTIFICATION_REVISION_ID = 0x00c2 23 | VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD = 0x0050 24 | VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD = 0x0070 25 | VL53L0X_REG_SYSRANGE_START = 0x000 26 | 27 | VL53L0X_REG_RESULT_INTERRUPT_STATUS = 0x0013 28 | VL53L0X_REG_RESULT_RANGE_STATUS = 0x0014 29 | 30 | 31 | address = 0x29 32 | 33 | bus = smbus.SMBus(1) 34 | 35 | val1 = bus.read_byte_data(address, VL53L0X_REG_IDENTIFICATION_REVISION_ID) 36 | print "Revision ID: " + hex(val1) 37 | val1 = bus.read_byte_data(address, VL53L0X_REG_IDENTIFICATION_MODEL_ID) 38 | print "Device ID: " + hex(val1) 39 | # case VL53L0X_VCSEL_PERIOD_PRE_RANGE: 40 | # Status = VL53L0X_RdByte(Dev, 41 | # VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD, 42 | # &vcsel_period_reg); 43 | val1 = bus.read_byte_data(address, VL53L0X_REG_PRE_RANGE_CONFIG_VCSEL_PERIOD) 44 | print "PRE_RANGE_CONFIG_VCSEL_PERIOD=" + hex(val1) + " decode: " + str(VL53L0X_decode_vcsel_period(val1)) 45 | 46 | 47 | # case VL53L0X_VCSEL_PERIOD_FINAL_RANGE: 48 | # Status = VL53L0X_RdByte(Dev, 49 | # VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD, 50 | # &vcsel_period_reg); 51 | 52 | val1 = bus.read_byte_data(address, VL53L0X_REG_FINAL_RANGE_CONFIG_VCSEL_PERIOD) 53 | print "FINAL_RANGE_CONFIG_VCSEL_PERIOD=" + hex(val1) + " decode: " + str(VL53L0X_decode_vcsel_period(val1)) 54 | 55 | # Status = VL53L0X_WrByte(Dev, VL53L0X_REG_SYSRANGE_START, 0x01); 56 | val1 = bus.write_byte_data(address, VL53L0X_REG_SYSRANGE_START, 0x01) 57 | 58 | # Status = VL53L0X_RdByte(Dev, VL53L0X_REG_RESULT_RANGE_STATUS, 59 | # &SysRangeStatusRegister); 60 | # if (Status == VL53L0X_ERROR_NONE) { 61 | # if (SysRangeStatusRegister & 0x01) 62 | # *pMeasurementDataReady = 1; 63 | # else 64 | # *pMeasurementDataReady = 0; 65 | # } 66 | cnt = 0 67 | while (cnt < 100): # 1 second waiting time max 68 | time.sleep(0.010) 69 | val = bus.read_byte_data(address, VL53L0X_REG_RESULT_RANGE_STATUS) 70 | if (val & 0x01): 71 | break 72 | cnt += 1 73 | 74 | if (val & 0x01): 75 | print "ready" 76 | else: 77 | print "not ready" 78 | 79 | # Status = VL53L0X_ReadMulti(Dev, 0x14, localBuffer, 12); 80 | data = bus.read_i2c_block_data(address, 0x14, 12) 81 | print data 82 | print "ambient count " + str(makeuint16(data[7], data[6])) 83 | print "signal count " + str(makeuint16(data[9], data[8])) 84 | # tmpuint16 = VL53L0X_MAKEUINT16(localBuffer[11], localBuffer[10]); 85 | print "distance " + str(makeuint16(data[11], data[10])) 86 | 87 | DeviceRangeStatusInternal = ((data[0] & 0x78) >> 3) 88 | print DeviceRangeStatusInternal --------------------------------------------------------------------------------