Boss lady
Boss lady
DIIDevHeads IoT Integration Server
Created by Boss lady on 7/2/2024 in #middleware-and-os
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
7 replies