Picar-x 2 Ultrasonics Returning Negative Number

Hi. I am getting negative numbers using two ultrasonic sensors. My code is below. The single sensor supplied lets the Pcar-x hit a wall at an angle. Using two sensors would avoid that issue.
But I get negative numbers with two verses one. IS there enough power supplied by the Hat for two? Or is my code wrong?

distance1: -1 - distance2: 127
distance1: -1 - distance2: 89
distance1: -1 - distance2: -2
distance1: -1 - distance2: -2
distance1: -1 - distance2: 89
distance1: -1 - distance2: -2
distance1: -1 - distance2: -2
distance1: -1 - distance2: 90
distance1: -1 - distance2: 93
distance1: -1 - distance2: -2
distance1: -1 - distance2: 85
distance1: -1 - distance2: 93
distance1: -1 - distance2: 83

from picarx import Picarx
import time
from robot_hat import Pin, Ultrasonic

POWER = 0 # set forward speed here but set to zero for testing
SafeDistance = 50 # > 40 safe I set this to 50 for testing
DangerDistance = 20 # > 20 && < 40 turn around,
# < 20 backward
tring = ‘D0’
echo = ‘D1’

def main():
try:
#px = Picarx()
rh = Ultrasonic(Pin(tring),Pin(echo)) # tring - yello - , echo - white
px = Picarx(ultrasonic_pins=[‘D2’,‘D3’]) # tring, echo
while True:
distance1 = rh
distance1 = (rh.read())
distance2 = round(px.ultrasonic.read())
print("distance1: ",distance1, " - " " distance2: ",distance2)
time.sleep(0.5)

finally:
    px.forward(0)

if name == “main”:
main()

Please make sure that the pins connected to your sensor module are the same as the pins written in the code.
If the module cable connection pins do not match the code pins, it will print -1.
If the sensor recognizes an obstacle that is too far away, it will print -2.
If the sensor recognizes an obstacle too far away, it will print -2. If it prints -1, it may be caused by your wiring not matching the code pins. Please confirm.

You were correct. Wrong pins. Thank you! :grinning:

1 Like