Raspberry pi + python ultra sonic sensor


I’m trying to control the Ultimate 2.0 robot via Raspberry Pi and Python. At first I tested the ports 1-4 with the encoder motor everything worked fine.

megapi import *

def onRead(v):
print(“distance:”+str(v)+" cm");

if name == ‘main’:
bot = MegaPi()
while 1:

Now after running the example code for the ultrasonic sensor. I can hear the sensor working for some time. Then it stops and the following message appears:
“device reports readiness to read but returned no data (device disconnected or multiple access on port?)”


Hi, may be you can take a look at this link: https://www.raspberrypi.org/forums/viewtopic.php?t=228315.
By the way, this is kind of DIY program which belong to advanced personal applicatin and beyong our support ability.


This code works for me with ultimate 2 and RPI

from megapi import *
distance = 123

def onRead(v):
global distance
print(“distance:”+str(v)+" cm")
distance = v

bot = MegaPi()



print (“start test”)

while 1:

print (distance)
idist = int(distance)
if idist < 12:
    print("time to turn")

UltrasonicRead function will not allow return or global variable

Thank you, for your replies. I had to manually change the settings for UART in /boot/cmdline.txt.


you mean to remove the console entry (like console=serial0,115200).

Enjoy your new capabilities.


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.