Taha Arif
Taha Arif

Reputation: 31

Adafruit MMA8451 accelerometer giving my Raspberry Pi osError [Errno 121] Remote I/O error

I am running python3.5 on a Raspberry Pi 3 B+. With a display.

My project is getting a simple accelerometer to print its values. I am following the following tutorial: I even added dtoverlay=spi1-3cs to the end of /boot/config.txt to activate another hardware SPI port as I am using my Pi with a display.

I am trying to run the following code and am having the following errors.

I have run i2cdetect -y -1. It showed a device with address of 1d.

I have checked my connection and everything, not sure what I am doing wrong.

    # Simple demo of reading the MMA8451 orientation every second.
# Author: Tony DiCola
import time

import board
import busio
import adafruit_mma8451
# Initialize I2C bus.
i2c = busio.I2C(board.SCL, board.SDA)

# Initialize MMA8451 module.
sensor = adafruit_mma8451.MMA8451(i2c)
# Optionally change the address if it's not the default:
##sensor = adafruit_mma8451.MMA8451(i2c, address=0x1d)

# Main loop to print the acceleration and orientation every second.
while True:
    x, y, z = sensor.acceleration
    print('Acceleration: x={0:0.3f}m/s^2 y={1:0.3f}m/s^2 z={2:0.3f}m/s^2'.format(x, y, z))
    orientation = sensor.orientation

    print('Orientation: ', end='')
    if orientation == adafruit_mma8451.PL_PUF:
        print('Portrait, up, front')
    elif orientation == adafruit_mma8451.PL_PUB:
        print('Portrait, up, back')
    elif orientation == adafruit_mma8451.PL_PDF:
        print('Portrait, down, front')
    elif orientation == adafruit_mma8451.PL_PDB:
        print('Portrait, down, back')
    elif orientation == adafruit_mma8451.PL_LRF:
        print('Landscape, right, front')
    elif orientation == adafruit_mma8451.PL_LRB:
        print('Landscape, right, back')
    elif orientation == adafruit_mma8451.PL_LLF:
        print('Landscape, left, front')
    elif orientation == adafruit_mma8451.PL_LLB:
        print('Landscape, left, back')
    time.sleep(1.0)

The Errors are as follows:

Traceback (most recent call last):
  File "simpletest.py", line 15, in <module>
    sensor = adafruit_mma8451.MMA8451(i2c)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_mma8451.py", line 103, in __init__
    while self._read_u8(_MMA8451_REG_CTRL_REG2) & 0x40 > 0:
  File "/usr/local/lib/python3.5/dist-packages/adafruit_mma8451.py", line 134, in _read_u8
    self._read_into(address, self._BUFFER, count=1)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_mma8451.py", line 130, in _read_into
    in_end=count, stop=False)
  File "/usr/local/lib/python3.5/dist-packages/adafruit_bus_device/i2c_device.py", line 149, in write_then_readinto
    in_start=in_start, in_end=in_end, stop=stop)
  File "/home/pi/.local/lib/python3.5/site-packages/busio.py", line 68, in writeto_then_readfrom
    in_start=in_start, in_end=in_end, stop=stop)
  File "/home/pi/.local/lib/python3.5/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 63, in writeto_then_readfrom
    readin = self._i2c_bus.read_i2c_block_data(address, buffer_out[out_start:out_end][0], in_end-in_start)
  File "/home/pi/.local/lib/python3.5/site-packages/Adafruit_PureIO/smbus.py", line 215, in read_i2c_block_data
    ioctl(self._device.fileno(), I2C_RDWR, request)
OSError: [Errno 121] Remote I/O error

Upvotes: 3

Views: 992

Answers (2)

Suketu Naik
Suketu Naik

Reputation: 1

Here are the steps I took to solve this:

=====MMA8451 troubleshooting Log: MMA8451 I/O error===================

  1. check all the pins many times
  2. add 1 s delay after i2c call line
  3. run "i2cdetect" command to see if the device is found.
  4. change the rpi config file to slow the i2c clock. Uncomment the following lines. baudrate can be set even lower to 1000 if needed.
#dtparam=i2c_arm=on
#dtparam=i2c_arm_baudrate=10000
  1. change the default 0x1d to 0x1c by connecting pin A to GND and providing the address in the code
  2. connect 4.7kohm pull-up resistors between SDA and 3.3 V and SCL and 3.3V
  3. try 5V from RPI board to the sensor
  4. try connecting 3.3 V and 5V power to Vin pin on sensor
  5. try to put adafruit mma8451 call in the loop and wait till the values come out
# Initialize the bus

i2c = busio.I2C(board.SCL, board.SDA)
time.sleep(1)#wait for 1 second

# Create sensor objects, communicating over the board's default I2C bus
# The following code put sensors in a loop until the data is being read properly by adafruit's library

    while True:
        try:
            # Initialize the Sensors
            mpu = MPU6050(i2c)
            mma = MMA8451(i2c)
            break
        except Exception as e:
            print(e)
            time.sleep(1)

Upvotes: 0

CDJB
CDJB

Reputation: 14506

I had the same problem with my Pi & accelerometer - device showing when using i2cdetect, but unable to get data with either Adafruit's code or i2cget. i2cdump showed garbage/corrupted data. The solution was that the ground wire between the Pi and the accelerometer was faulty. Once this was fixed, the device responded correctly.

Upvotes: 1

Related Questions