2 Sample code for the BH1750
11 #include <linux/i2c-dev.h>
12 #include <linux/i2c.h>
13 #include <sys/ioctl.h>
16 #define BH1750FVI_I2C_ADDRESS 0x23 // ADDR > 0.7 VCC)
17 //#define BH1750FVI_I2C_ADDRESS 0x53 // ADDR < 0.3 VCC)
20 #define PowerDown 0x00
23 #define ContinuHigh 0x10
24 #define ContinuLow 0x13
25 #define OneTimeHigh 0x20
26 #define OneTimeLow 0x23
28 int main(int argc, char **argv)
31 char *fileName = "/dev/i2c-0";
39 // Open port for reading and writing
40 if ((fd = open(fileName, O_RDWR)) < 0) {
41 printf("open error\n");
45 // Set the port options and set the address of the device
46 if (ioctl(fd, I2C_SLAVE, BH1750FVI_I2C_ADDRESS) < 0) {
47 printf("ioctl error\n");
52 retCode=i2c_smbus_write_byte(fd, PowerOn);
53 if(DEBUG)printf("Power On retCode=%d\n",retCode);
55 printf("PowerOn error\n");
60 retCode=i2c_smbus_write_byte(fd, ContinuHigh);
61 if(DEBUG)printf("ContinuHigh retCode=%d\n",retCode);
63 printf("ContinuHigh error\n");
68 // Set i<10 for more one check
71 readSize = read (fd, buf, 2);
72 if(DEBUG)printf("read readSize=%d %x %x\n",readSize,buf[0],buf[1]);
74 res = buf[0]*256+buf[1];
75 if(DEBUG)printf("res=%x\n",res);
77 // printf("Lux=%d\n",lux);
78 printf("Brightness (Lux)\t%0.1f\n", ((double)lux));
81 retCode=i2c_smbus_write_byte(fd, PowerDown);