I am a student and want to make a project where the computer mouse pointer can be controlled via the MPU6050 sensors. So far I have calculated the below things.

pitch and roll with accelerometer data:

def read_word(adr):
    high = bus.read_byte_data(address, adr)
    low = bus.read_byte_data(address, adr+1)
    val = (high << 8) + low
    return val

def read_word_2c(adr):
    val = read_word(adr)
    if (val >= 0x8000):
        return -((65535 - val) + 1)
        return val
accel_xout = read_word_2c(0x3b)
accel_yout = read_word_2c(0x3d)
accel_zout = read_word_2c(0x3f)
accel_xout_scaled = accel_xout / 16384.0
accel_yout_scaled = accel_yout / 16384.0
accel_zout_scaled = accel_zout / 16384.0

Accpitch=round((math.atan2(accel_yout_scaled,accel_zout_scaled)+math.pi) * 180 / math.pi)
Accroll=round((math.atan2(accel_xout_scaled, accel_zout_scaled)+math.pi) * 180 / math.pi)


My question is: Is this the correct way of mapping the output result(degree of orientation in a 3d plane) to the mouse pointer coordinates(x,y)? Is there any other optimal way to do this? Can anyone please help here or give some hints. Thank you in advance.


1 Answer 1


I think the easiest practical way to achieve this is to get an Arduino based on a 32u4 chip (Leonardo / Esplora / Zero), attach the MPU to it and implement a mouse using the Mouse functions.

You can then connect the Arduino the the Pi USB port, and it will be recognized as a mouse.

  • Thank you for your comment. Unfortunately, I have to use raspberry 4 due to some constraints. The final values(x, y) which I am getting is getting mapped with the mouse coordinates. The screen resolution of the pi screen is 720*480 but the values are always somewhere around the (300, 200) range. Therefore it is not working.
    – Frank
    Nov 9, 2021 at 7:41
  • @Frank Then normalize the values you are getting to 0..1 range, and multiply by 720 and 480, respectively. Nov 9, 2021 at 8:34
  • I think I found the way but the readings are quite noisy. As my screen resolution is 720*480 and the range of degree is 0-360. I scale up accordingly. I have updated my code above. Note: I did not use the Gyroscope data as it is not giving my correct details. I just mapped Accpitch and Accroll for mapping to mouse coordinates
    – Frank
    Nov 9, 2021 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.