Why do some ports for sensors not work?

Hi,

When I connect sensor to the brickpi, just PORT4 can get data from sensor.

I use infrared sensor, it always gets 65532

May I know what is the problem with that?

Which version of BrickPi are you using? You can use this post for reference.

Can you post your code?

Which infrared sensor are you trying to use?

from BrickPi import *

BrickPiSetup()
BrickPi.SensorType[PORT_2] = TYPE_SENSOR_EV3_INFRARED_M0
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_EV3_US_M0
BrickPiSetupSensors()

while True:
result = BrickPiUpdateValues() # Ask BrickPi to update values for sensors/motors
if not result :
ultra = BrickPi.Sensor[PORT_4]
infra = BrickPi.Sensor[PORT_2]
print(“ultra:”+str(ultra))
print(" infra:"+str(infra))
time.sleep(0.01)

I am using BrickPi 2 and the sensor is EV-3 Infrared and ultrasonic. They can work well separately. But when I read data from them together, just one work.

I found the problem is the order I set up the BrickPi.Sensor.
The first port I set up can work. But the latter cannot.

When I reboot the pi again. The first time I run the script it can read data from 2 sensors. But just the first time. The other time, it cannot.

At the beginning of the program try setting the sensor type to TYPE_SENSOR_RAW (and make the setting take effect using BrickPiSetupSensors()), and then set the sensor type to the correct type (e.g. TYPE_SENSOR_EV3_INFRARED_M0).

I am sorry but that does not work.
BrickPiSetup()
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_RAW
BrickPi.SensorType[PORT_1] = TYPE_SENSOR_RAW
BrickPiSetupSensors()
BrickPi.SensorType[PORT_4] = TYPE_SENSOR_EV3_US_M0
BrickPi.SensorType[PORT_1] = TYPE_SENSOR_EV3_INFRARED_M0
BrickPiSetupSensors()

One of the value is incorrect. And the value just from 65532 to 0.

Sometimes it can get the correct output. Maybe 1 of 10 times

But I need it to be reliable.

Try adding delays to allow the settings to take effect. When configuring for an EV3 sensor, it can take several seconds to be ready.

I add time.sleep(10) after BrickPiSetupSensors(), but it also does not work.

Hello @panjinb to be clear: are you using the BrickPi+ ? I see “BrickPi2” above.

You will increase the chances of a good reading with the BrickPi+ if you increase your sleep times all around. So after setupSensors and also in between readings.