How do I set the I2C (Mpu-6050) slave address in my application?

@Middleware & OS Hello Guys, how do I set the I2C (Mpu-6050) slave address in my application, am getting the error Failed to acquire bus access and/or talk to slave: Input/output error but i have Checked the wiring and confirmed the slave device address. here's my code below
int addr = 0x48; // The I2C address of the slave device
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to acquire bus access and/or talk to slave");
return 1;
}

int addr = 0x48; // The I2C address of the slave device
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to acquire bus access and/or talk to slave");
return 1;
}

Solution:
Thanks everyone!, I didn't realize the address for the MPU-6050 was different. I changed the address to 0x68 and now my code looks like this: ```c int file; if ((file = open("/dev/i2c-1", O_RDWR)) < 0) {...
Jump to solution
5 Replies
Enthernet Code
Enthernet Code5mo ago
Hey @Boss lady , I think the issue might be with the address you're using. The MPU-6050 typically has an I2C address of either 0x68 or 0x69, depending on the AD0 pin's state. Try changing the address to 0x68 and see if it works. Also, double-check the wiring for the AD0 pin to ensure it matches the address you're using.
Marvee Amasi
Marvee Amasi5mo ago
Yeah 👍, you should also ensure that the I2C bus you're trying to access is the correct one. Sometimes, the I2C bus number can differ based on the hardware platform. For example, on a Raspberry Pi, you might need to use /dev/i2c-1 instead of /dev/i2c-0. Can you share the part of your code where you open the I2C bus?
RED HAT
RED HAT5mo ago
@Boss lady Another thing to consider is permissions. Make sure you have the necessary permissions to access the I2C bus. If you're running your application as a non-root user, you might encounter this error. You can try running your application with sudo to see if that resolves the issue. Here's a modified version of your code with the updated address:
Copy code
int addr = 0x68; // The I2C address of the MPU-6050
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to acquire bus access and/or talk to slave");
return 1;
}
Copy code
int addr = 0x68; // The I2C address of the MPU-6050
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to acquire bus access and/or talk to slave");
return 1;
}
Also, ensure you have the I2C device open correctly:
Copy code
int file;
if ((file = open("/dev/i2c-1", O_RDWR)) < 0) {
perror("Failed to open the i2c bus");
return 1;
}
Copy code
int file;
if ((file = open("/dev/i2c-1", O_RDWR)) < 0) {
perror("Failed to open the i2c bus");
return 1;
}
Solution
Boss lady
Boss lady5mo ago
Thanks everyone!, I didn't realize the address for the MPU-6050 was different. I changed the address to 0x68 and now my code looks like this:
int file;
if ((file = open("/dev/i2c-1", O_RDWR)) < 0) {
perror("Failed to open the i2c bus");
return 1;
}

int addr = 0x68; // The I2C address of the MPU-6050
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to acquire bus access and/or talk to slave");
return 1;
}
int file;
if ((file = open("/dev/i2c-1", O_RDWR)) < 0) {
perror("Failed to open the i2c bus");
return 1;
}

int addr = 0x68; // The I2C address of the MPU-6050
if (ioctl(file, I2C_SLAVE, addr) < 0) {
perror("Failed to acquire bus access and/or talk to slave");
return 1;
}
I ran it with sudo and it worked! Thanks again for your help!
Boss lady
Boss lady5mo ago
Thanks for pointing that out, @Marvee Amasi . Here's the part of my code where I open the I2C bus:
int file;
if ((file = open("/dev/i2c-0", O_RDWR)) < 0) {
perror("Failed to open the i2c bus");
return 1;
}
int file;
if ((file = open("/dev/i2c-0", O_RDWR)) < 0) {
perror("Failed to open the i2c bus");
return 1;
}
It seems like I might be using the wrong I2C bus. I have changed it to /dev/i2c-1 . I appreciate the suggestion!
Want results from more Discord servers?
Add your server