Skip to content

Instantly share code, notes, and snippets.

@jeerovan
Created March 5, 2020 03:01
Show Gist options
  • Save jeerovan/9391d0bc753c788431b3a0dba9796e2e to your computer and use it in GitHub Desktop.
Save jeerovan/9391d0bc753c788431b3a0dba9796e2e to your computer and use it in GitHub Desktop.
Distance/Range Sensor TOF10120 Python code for Raspberry Pi
#!/usr/bin/python
import time
from smbus2 import SMBus
from micropython import const
bus = SMBus(1)
time.sleep(1)
addr = const(0x52)
prev_distance = 200
def range_mm():
bus.write_byte(addr,0)
time.sleep(0.02)
value = bus.read_byte(addr) << 8 | bus.read_byte(addr)
time.sleep(0.05)
return value
try:
while True:
distance = range_mm()
print(distance)
except KeyboardInterrupt:
bus.close()
print("Stopped")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment