0

I'm relatively new to this so apologies in advance. I have a raspberry pi4 with a Bete Ehpi pulse width modulatin nozzle and a Adafruit-Vl53l4cd time of flight sensor. Code below is set to control the duty cycle of the spray based on distance measured. It works for a little bit of time then all of a sudden I get the error below and the nozzle becomes fully open, flooding my workspace. When I run my time of flight sensor code alone I have no errors, similarly, when I run pwm nozzle control alone, I get no error, but when combined this issue persists. Any suggestions?

*Error:

"Traceback (most recent call last):
File
"/home/sppi/SelectivePatina/240709_simple test_01.py", line 62, in <module>
run_program()
File
"/home/sppi/SelectivePatina/240709_simple test_01.py", line 36, in run_program
d = sensor.distance/2.54
File "/home/sppi/SelectivePatina/lib/python3.11/site-packages/adafruit_vl53l4cd.py", line 213, in distance
dist = self._read_register(_VL53L4CD_RESULT_DISTANCE, 2)
File "/home/sppi/SelectivePatina/lib/python3.11/site-packages/adafruit_vl53l4cd.py", line 455, in _read_register
i2c.write(struct.pack(">H", address))
File
"/home/sppi/SelectivePatina/lib/python3.11/site-packages/adafruit_bus_device/i2c_device.py", line 100, in write
self.i2c.writeto(self.device_address, buf, start=start, end=end)
File
"/home/sppi/SelectivePatina/lib/python3.11/site-packages/busio.py", line 212, in writeto
return self._i2c.writeto(address, memoryview(buffer)[start:end], stop=True)
File "/home/sppi/SelectivePatina/lib/python3.11/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 60, in writeto
self._i2c_bus.write_bytes(address, buffer[start:end])
File "/home/sppi/SelectivePatina/lib/python3.11/site-packages/Adafruit_PureIO/smbus.py", line 303, in write_bytes
self._device.write(buf)
OSError: [Errno 5] Input/output error"*

BELOW IS CODE:

import RPi.GPIO as GPIO
import time
import board
import busio
import adafruit_vl53l4cd
#Initialize ToF sensor
i2c = busio.I2C(board.SCL, board.SDA)
sensor =adafruit_vl53l4cd.VL53L4CD(i2c)
sensor.start_ranging()
#Initialize GPIO for PWM
PWM_PIN = 18  # Change to your PWM pin
FREQUENCY = 20  # 50 Hz PWM frequency
DUTY_CYCLE_OFF = 0
DUTY_CYCLE_ON = 10  # 20% duty cycle for 9" to 1"
GPIO.setmode(GPIO.BCM)
GPIO.setup(PWM_PIN, GPIO.OUT)
pwm = GPIO.PWM(PWM_PIN, FREQUENCY)
pwm.start(DUTY_CYCLE_OFF)
def stop_program():

raise KeyboardInterrupt#Ctrl+C
def run_program():

try:

    while True:

        while not sensor.data_ready:

            pass

        sensor.clear_interrupt()

        # Measure height

        d = sensor.distance/2.54

        print("Height: {:.2f} inches".format(d))

        #Convert distance to inches (1 inch = 25.4 mm)

        #Control PWM based on distance

        if d > 9:
            pwm.ChangeDutyCycle(DUTY_CYCLE_OFF)
        elif 1 <= d <= 9:
            pwm.ChangeDutyCycle(DUTY_CYCLE_ON)
        else:
            pwm.ChangeDutyCycle(DUTY_CYCLE_OFF)
            

        time.sleep(0.4)  # Adjust as needed for your application

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
    sensor.stop_ranging()
    pwm.ChangeDutyCycle(DUTY_CYCLE_OFF)
    pwm.stop()
run_program()
root.mainloop() 
New contributor
Burcin Nalinci is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • @jsotola all done. Thank you Commented Jul 9 at 23:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.