[SOLVED] My distance sensor doesn't work

My distance sensor connects to my GoPiGo3 through Analog Digital 1 port.

Run the program
basic_obstacle_avoid.py
at

/home/pi/Dexter/GoPiGo/Software/Python/Examples/Ultrasonic_Basic_Obstacle_Avoider

And it doesn’t work and I got 0 cm distance report. Actually there is no obstacle in front of my robot.

The message from running window is like:

Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type “copyright”, “credits” or “license()” for more information.

================================ RESTART ================================

Press ENTER to start

Dist: 0 cm
Stopping


Thanks,
Jason

Hello @junwan2000,
You have a GoPiGo3 which is fundamentally different from a GoPiGo2 in its hardware architecture. You cannot run any of the examples under the GoPiGo folder. You have to look under the GoPiGo3 folder.

Also are you talking about the ultrasonic sensor or the distance sensor?

I am using the distance sensor, the flat one with white color.

Where is the example code for a distance sensor on GoPiGo3?

Hello @junwan2000
All code for GoPiGo3 is under /home/pi/Dexter/GoPiGo3

The distance sensor example can be found here:

Thanks!

I still have error:

my_distance_sensor = gpg.init_distance_sensor()
AttributeError: ‘EasyGoPiGo3’ object has no attribute ‘init_distance_sensor’

Hi @junwan2000,

I think you have an older version of the GoPiGo3 installed.


In your terminal, type the following command: sudo curl https://raw.githubusercontent.com/DexterInd/Raspbian_For_Robots/master/upd_script/fetch_gopigo3.sh | bash

This will update your GoPiGo3 repository to the latest version.
By the way, after updating the GoPiGo3 repository, you might also want to update the firmware of the GoPiGo3 through the DI Update app that’s found on the Desktop.


After running the update, please run the example @cleoqc showed you and you should see your script working.

Thank you!

Hi Robert,

I still got error after followed the steps:

  1. Updated my GoPiGo3 repository to the latest version
  2. Updated the firmware of the GoPiGo3 through the DI Update app

My code is as:

import time
import easygopigo3 as easy

gpg = easy.EasyGoPiGo3()

my_distance_sensor = gpg.init_distance_sensor()

while True:
    # Directly print the values of the sensor.
    print("Distance Sensor Reading (mm): " + str(my_distance_sensor.read_mm()))

The new error is:

Distance Sensor init: [Errno 121] Remote I/O error

Traceback (most recent call last):
  File "/home/pi/Dexter/GoPiGo3/Software/Python/Examples/Distance_sensor_wj/distance_sensor_wj1.py", line 6, in <module>
    my_distance_sensor = gpg.init_distance_sensor()
  File "build/bdist.linux-armv7l/egg/easygopigo3.py", line 839, in init_distance_sensor
    return DistanceSensor(port, self)
  File "build/bdist.linux-armv7l/egg/easygopigo3.py", line 2615, in __init__
    distance_sensor.DistanceSensor.__init__(self)
  File "build/bdist.linux-armv7l/egg/di_sensors/distance_sensor.py", line 24, in __init__
    self.VL53L0X = VL53L0X.VL53L0X(bus = bus)
  File "build/bdist.linux-armv7l/egg/di_sensors/VL53L0X.py", line 121, in __init__
    self.reset(self.ADDRESS)
  File "build/bdist.linux-armv7l/egg/di_sensors/VL53L0X.py", line 142, in reset
    value = self.i2c_bus.read_reg_8u(IDENTIFICATION_MODEL_ID)
  File "build/bdist.linux-armv7l/egg/di_sensors/dexter_i2c.py", line 196, in read_reg_8u
    val = self.transfer([reg], 1)
  File "build/bdist.linux-armv7l/egg/di_sensors/dexter_i2c.py", line 106, in transfer
    return self.i2c_bus.read_i2c_block_data(self.address, outArr[0], inBytes)
IOError: [Errno 121] Remote I/O error

Thanks,
Jason

out of curiosity @junwan2000, which port is your distance sensor connected to?

My distance sensor connects to port Analog/Digital 1 .

Thanks,
Jason

Hi @junwan2000,

Please notice that the Distance Sensor has to be connected to an I2C port.
See more in our documentation - link.

Please try connecting it to the right port and then tell us if this solves your issue.

Thank you!

Hi Robert,

My code works with the Distance Sensor after switch the cable to I2C!

Thank you so much for your help!

Jason

1 Like

Hi @junwan2000,

That’s great to hear.

Thank you!