Continuous Distance Measurement and OLED Update on BeagleBone Black with VL53L0X

Hello guys, based on my project parking assistance system using a BeagleBone Black running Embedded Linux, VL53L0X time of flight distance sensor to detect the distance to nearby objects and a 0.96-inch OLED display to show the distance and warning messages. how do I continuously measure the distance using the VL53L0X sensor and update the OLED display with the readings? I have added debug prints, checked for memory issues, ensured proper handling of the OLED display but am getting the error prompt Error: ValueError: I/O operation on closed file here's my code
import board
import busio
import adafruit_vl53l0x
from Adafruit_SSD1306 import SSD1306_128_64
import time

# Initialize I2C bus and devices
i2c = busio.I2C(board.SCL, board.SDA)
vl53 = adafruit_vl53l0x.VL53L0X(i2c)
disp = SSD1306_128_64(rst=None)
disp.begin()
disp.clear()
disp.display()

while True:
distance = vl53.range
print(f"Distance: {distance} mm")

disp.clear()
disp.text(f"Distance: {distance} mm", 0, 0, 1)
disp.display()

time.sleep(1)
import board
import busio
import adafruit_vl53l0x
from Adafruit_SSD1306 import SSD1306_128_64
import time

# Initialize I2C bus and devices
i2c = busio.I2C(board.SCL, board.SDA)
vl53 = adafruit_vl53l0x.VL53L0X(i2c)
disp = SSD1306_128_64(rst=None)
disp.begin()
disp.clear()
disp.display()

while True:
distance = vl53.range
print(f"Distance: {distance} mm")

disp.clear()
disp.text(f"Distance: {distance} mm", 0, 0, 1)
disp.display()

time.sleep(1)
Solution:
Hello,Your setup is okay, but the ValueError: I/O operation on closed file error seems to points to an issue with how the OLED display or I2C bus is being managed. Make sure the OLED is properly initialized and stays active. You might also want to wrap your loop in a try-except block to catch errors and prevent the display from closing unexpectedly. re-check your wiring as well. Here's a quick fix: ```python try: while True:...
Jump to solution
3 Replies
Solution
Enthernet Code
Enthernet Code4mo ago
Hello,Your setup is okay, but the ValueError: I/O operation on closed file error seems to points to an issue with how the OLED display or I2C bus is being managed. Make sure the OLED is properly initialized and stays active. You might also want to wrap your loop in a try-except block to catch errors and prevent the display from closing unexpectedly. re-check your wiring as well. Here's a quick fix:
try:
while True:
# Your code to read distance and update the display
except ValueError as e:
print(f"An error occurred: {e}")
try:
while True:
# Your code to read distance and update the display
except ValueError as e:
print(f"An error occurred: {e}")
@Boss lady
Boss lady
Boss lady4mo ago
Thanks very much, this was helpful to me and helped me advanced my project
Want results from more Discord servers?
Add your server