Python with MegaPi + Gyro Sensor returning 0


#1

Hi, I am using python to get data from the sensors using MegaPi.
All the other sensors work (ultrasonic sensor and the line following sensor), but when it comes to the gyro sensor, it’s returning as 0.0. It works in mBlock and also when I use Arduino IDE, but for some reason its not working in Python. My code is attached below:

from megapi_python3 import *
import time

def ultrasonicRead(v):
print(f"distance: {round(v, 2)} cm")

def gyroRead(w):
print(w)

def lineRead(x):
print(int(x))

if name == ‘main’:
bot = MegaPi()
bot.start(‘COM6’)
while True:
# bot.ultrasonicSensorRead(8, ultrasonicRead)
# bot.lineFollowerRead(7, lineRead)
bot.gyroRead(6, 3, gyroRead)
time.sleep(1)


#2

@ilyas Do you have the necessary modules installed in your Python environment? (from megapi_python3 import *, import time, etc.?)


#3

Yes. As I stated in the original post, it works with the ultrasonic sensor and the line following sensor. Its not working only for the gyro.


#4

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