I'm currently working on connecting and reading an MMA8451 with a raspberry pi running C code. I've got the code up and running, and i can't find any reason why the accelerometer should be returning an incorrect whoami (currently sending 0xff). Here is the code i use to initialize the accelerometer:
mma8451 mma8451_initialise(int device int addr) { mma8451 handle; handle.file = -1; handle.address = addr; char buf[15]; printf("Initalising MMA8451 sensor at address %#02x\n", addr); //Open /dev/i2c-x file without a buffer sprintf(buf, "/dev/i2c-%d", device); if ((handle.file = open(buf, O_RDWR)) < 0) { handle.file = -2; return handle; } //Configure slave i2c address via ioctl if(ioctl(handle.file, I2C_SLAVE, addr) < 0) { handle.file = -3; return handle; } //Check if we read correctly from the sensor char whoami = mma8451_read_byte(&handle, 0x0D); printf("whoami read %#02x\n", whoami); //Undefined behavior for the rest of device operation if the device is not returning hex 1A if(whoami != 0x1A) perror("mma451_pi warning: Device correctly intialized but not returning 0x1A at WHO_AM_I request.\n" "Are you sure you are using a MMA8451 accelerometer on this address?"); //Send reset request mma8451_write_byte(&handle, 0x2B, 0x40); printf("Waiting for accelerometer to be reset\n"); while(mma8451_read_byte(&handle, 0x2B) & 0x40); //reset done printf("Done\n"); mma8451_set_range(&handle, 2); mma8451_write_byte(&handle, 0x2B, 0x02); //high resolution mode mma8451_write_byte(&handle, 0x2A, 0x01 | 0x04); //high rate low noise //Deactivate fifo mma8451_write_byte(&handle, 0x09, 0); //turn on orientation configuration mma8451_write_byte(&handle, 0x11, 0x40); printf("MMA8451 at address %#02x configured for real time sampling, in high rate, low noise mode, at high resolution, on a 2G max range\n", addr); return handle; }
This is directly from the mma8451_pi library found here: https://github.com/Ybalrid/mma8451_pi
Any ideas what could be causing it to return an incorrect whoami?
Hi Tim,
If you have an oscilloscope or a logic analyzer, can you please post here a plot illustrating what is going on the bus when you read the WHO_AM_I register?
Also please make sure you are using correct slave address depending on the connection of the SA0 pin.
Best regards,
Tomas