Created
March 5, 2020 03:01
-
-
Save jeerovan/9391d0bc753c788431b3a0dba9796e2e to your computer and use it in GitHub Desktop.
Distance/Range Sensor TOF10120 Python code for Raspberry Pi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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