Advertisement
Guest User

VL53L5CX address change micropython

a guest
Mar 12th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import pimoroni_i2c
  2. import time
  3. import machine
  4. import breakout_vl53l5cx
  5.  
  6. # Function to change the I2C address
  7. def vl53l5cx_set_i2c_address(i2c, old_address, new_address):
  8.     # Unlock the sensor's I2C address change functionality
  9.     i2c.writeto_mem(old_address, 0x7fff, bytes([0x00]), addrsize=16)
  10.     # Write the new I2C address
  11.     i2c.writeto_mem(old_address, 0x04, bytes([(new_address)]), addrsize=16)
  12.     # Lock the sensor's I2C address change functionality
  13.     i2c.writeto_mem(new_address, 0x7fff, bytes([0x02]), addrsize=16)
  14.     time.sleep(1)
  15.    
  16.     print(f"Changed I2C address from {hex(old_address)} to {hex(new_address)}")
  17.     print("Available I2C devices after address change:", i2c.scan())
  18.  
  19. # Configuration
  20. PIN_CONFIG = {"sda": 6, "scl": 7}
  21. I2C_BAUDRATE = 2_000_000
  22. DEFAULT_ADDRESS = 0x29  # Default 7-bit address (0x52 shifted right by 1)
  23.  
  24. # Sensor configuration
  25. SENSORS = {
  26.     "sensor_1": {
  27.         "lpn_pin": machine.Pin(10, machine.Pin.OUT),
  28.         "new_address": 0x40,
  29.     },
  30.     "sensor_2": {
  31.         "lpn_pin": machine.Pin(11, machine.Pin.OUT),
  32.         "new_address": 0x50,
  33.     },
  34. }
  35.  
  36. # Initialize I2C
  37. i2c = pimoroni_i2c.PimoroniI2C(**PIN_CONFIG, baudrate=I2C_BAUDRATE)
  38.  
  39. # Disable all sensors initially
  40. for config in SENSORS.values():
  41.     config["lpn_pin"].value(0)
  42.  
  43. # Initialize and configure sensors
  44. sensors = {}
  45. for name, config in SENSORS.items():
  46.     # Disable all sensors except the one being configured
  47.     for sensor_name, sensor_config in SENSORS.items():
  48.         sensor_config["lpn_pin"].value(1 if sensor_name == name else 0)
  49.     time.sleep(3)  # Wait for the sensor to stabilize
  50.  
  51.     # Change the I2C address
  52.     vl53l5cx_set_i2c_address(i2c, DEFAULT_ADDRESS, config["new_address"])
  53.  
  54.     # Initialize the sensor
  55.     sensors[name] = breakout_vl53l5cx.VL53L5CX(i2c, config["new_address"])
  56.     sensors[name].set_resolution(breakout_vl53l5cx.RESOLUTION_8X8)
  57.     sensors[name].set_ranging_frequency_hz(30)
  58.     sensors[name].start_ranging()
  59.  
  60.     print(f"{name} initialized with address {hex(config['new_address'])}")
  61.  
  62. # Enable all sensors after configuration
  63. for config in SENSORS.values():
  64.     config["lpn_pin"].value(1)
  65. time.sleep(3)  # Wait for all sensors to stabilize
  66.  
  67. # Main loop to read sensor data
  68. while True:
  69.     for name, sensor in sensors.items():
  70.         if sensor.data_ready():
  71.             data = sensor.get_data()
  72.             avg_distance = data.distance_avg
  73.             avg_reflectance = data.reflectance_avg
  74.             print(f"{name} - Avg Distance: {avg_distance:.2f} mm, Avg Reflectance: {avg_reflectance:.2f}")
  75.     time.sleep(1)
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement