How can I interface an I2C device with BeagleBone Black running Embedded Linux

Hello guys, how can I interface an I2C device with BeagleBone Black running Embedded Linux for my home automation system?. I have connected the I2C device to the appropriate pins on the BeagleBone Black, enabled the I2C interface on the BeagleBone Black, wrote a C program to communicate with the I2C device using the I2C bus. But keep encountering Failed to open the I2C bus. this is my code
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>

int main() {
int i2cFile;

// Open the I2C bus
i2cFile = open("/dev/i2c-1", O_RDWR);
if (i2cFile < 0) {
printf("Failed to open the I2C bus.\n");
return -1;
}

// Set the slave address
int slaveAddress = 0x12;
if (ioctl(i2cFile, I2C_SLAVE, slaveAddress) < 0) {
printf("Failed to set slave address.\n");
return -1;
}

// Perform I2C communication
// ...

// Close the I2C bus
close(i2cFile);

return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>

int main() {
int i2cFile;

// Open the I2C bus
i2cFile = open("/dev/i2c-1", O_RDWR);
if (i2cFile < 0) {
printf("Failed to open the I2C bus.\n");
return -1;
}

// Set the slave address
int slaveAddress = 0x12;
if (ioctl(i2cFile, I2C_SLAVE, slaveAddress) < 0) {
printf("Failed to set slave address.\n");
return -1;
}

// Perform I2C communication
// ...

// Close the I2C bus
close(i2cFile);

return 0;
}
@Middleware & OS
Solution:
@Boss lady if the devices exist, then the next thing I would check are the permissions. Having just taken a quick look on mine... ``` debian@BeagleBone:~$ ls -l /dev/i2c-* crw-rw---- 1 root gpio 89, 0 May 17 12:23 /dev/i2c-0 crw-rw---- 1 root gpio 89, 1 May 17 12:23 /dev/i2c-1...
Jump to solution
5 Replies
Ming
Ming6mo ago
@Boss lady Do you have the i2c devices showing up in /dev yet? If not, the code will never run.
Boss lady
Boss lady6mo ago
Can I check it using ls /dev/i2c
Ming
Ming6mo ago
Yes - though use ls /dev/i2c*
Boss lady
Boss lady6mo ago
Yes the i2c devices are present
Solution
Ming
Ming6mo ago
@Boss lady if the devices exist, then the next thing I would check are the permissions. Having just taken a quick look on mine...
debian@BeagleBone:~$ ls -l /dev/i2c-*
crw-rw---- 1 root gpio 89, 0 May 17 12:23 /dev/i2c-0
crw-rw---- 1 root gpio 89, 1 May 17 12:23 /dev/i2c-1
crw-rw---- 1 root gpio 89, 2 May 17 12:23 /dev/i2c-2
debian@BeagleBone:~$ ls -l /dev/i2c-*
crw-rw---- 1 root gpio 89, 0 May 17 12:23 /dev/i2c-0
crw-rw---- 1 root gpio 89, 1 May 17 12:23 /dev/i2c-1
crw-rw---- 1 root gpio 89, 2 May 17 12:23 /dev/i2c-2
Then that shows me they are only readable by the root user and anyone belonging to the gpio group. First thing to try is therefore running you program with sudo . If that works, then it is likely to be a permission issue. If it works, then the simplest solution would be to add yourself to the gpio group.
Want results from more Discord servers?
Add your server