Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pimoroni_i2c
- import time
- import machine
- import breakout_vl53l5cx
- # Function to change the I2C address
- def vl53l5cx_set_i2c_address(i2c, old_address, new_address):
- # Unlock the sensor's I2C address change functionality
- i2c.writeto_mem(old_address, 0x7fff, bytes([0x00]), addrsize=16)
- # Write the new I2C address
- i2c.writeto_mem(old_address, 0x04, bytes([(new_address)]), addrsize=16)
- # Lock the sensor's I2C address change functionality
- i2c.writeto_mem(new_address, 0x7fff, bytes([0x02]), addrsize=16)
- time.sleep(1)
- print(f"Changed I2C address from {hex(old_address)} to {hex(new_address)}")
- print("Available I2C devices after address change:", i2c.scan())
- # Configuration
- PIN_CONFIG = {"sda": 6, "scl": 7}
- I2C_BAUDRATE = 2_000_000
- DEFAULT_ADDRESS = 0x29 # Default 7-bit address (0x52 shifted right by 1)
- # Sensor configuration
- SENSORS = {
- "sensor_1": {
- "lpn_pin": machine.Pin(10, machine.Pin.OUT),
- "new_address": 0x40,
- },
- "sensor_2": {
- "lpn_pin": machine.Pin(11, machine.Pin.OUT),
- "new_address": 0x50,
- },
- }
- # Initialize I2C
- i2c = pimoroni_i2c.PimoroniI2C(**PIN_CONFIG, baudrate=I2C_BAUDRATE)
- # Disable all sensors initially
- for config in SENSORS.values():
- config["lpn_pin"].value(0)
- # Initialize and configure sensors
- sensors = {}
- for name, config in SENSORS.items():
- # Disable all sensors except the one being configured
- for sensor_name, sensor_config in SENSORS.items():
- sensor_config["lpn_pin"].value(1 if sensor_name == name else 0)
- time.sleep(3) # Wait for the sensor to stabilize
- # Change the I2C address
- vl53l5cx_set_i2c_address(i2c, DEFAULT_ADDRESS, config["new_address"])
- # Initialize the sensor
- sensors[name] = breakout_vl53l5cx.VL53L5CX(i2c, config["new_address"])
- sensors[name].set_resolution(breakout_vl53l5cx.RESOLUTION_8X8)
- sensors[name].set_ranging_frequency_hz(30)
- sensors[name].start_ranging()
- print(f"{name} initialized with address {hex(config['new_address'])}")
- # Enable all sensors after configuration
- for config in SENSORS.values():
- config["lpn_pin"].value(1)
- time.sleep(3) # Wait for all sensors to stabilize
- # Main loop to read sensor data
- while True:
- for name, sensor in sensors.items():
- if sensor.data_ready():
- data = sensor.get_data()
- avg_distance = data.distance_avg
- avg_reflectance = data.reflectance_avg
- print(f"{name} - Avg Distance: {avg_distance:.2f} mm, Avg Reflectance: {avg_reflectance:.2f}")
- time.sleep(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement