Using the temperature & humidity sensor with H/W I2C

We tested to read si702x sensor with H/W I2C.

xu3_si702x_2.jpg

Open /etc/modprobe.d/blacklist.conf file and add drivername using following syntax:

blacklist ioboard_bh1780
blacklist ioboard_bmp180
blacklist ioboard_keyled

Reboot

sudo reboot

Compile & Run

gcc -o ex_si702x ex_si702x.c
odroid@odroid:~$ sudo ./ex_si702x
Temperature : 28.64 °C
Humidity : 43.62 %
ex_si702x.c
#include <stdio.h>                                                              
#include <fcntl.h>                                                              
#include <linux/i2c-dev.h>                                                      
 
#define CMD_MEASURE_TEMPERATURE_HOLD    0XE3                                    
#define CMD_MEASURE_HUMIDITY_HOLD       0xE5                                    
#define SI702X_ADDRESS                  0x40                                    
 
int main(void)                                                                  
{                                                                               
        unsigned char rbuf[2];                                                  
        unsigned char wbuf[1];                                                  
        double temperature = 0;                                                 
        double humidity = 0;                                                    
        int status = 0;                                                         
 
        // Net name I2C_1 Physical address : 0x12C70000
        int fd = open("/dev/i2c-4", O_RDWR);                                    
        if (fd < 0) {                                                           
                printf("ERROR: open failed\n");                                 
                return -1;                                                      
        }                                                                       
 
        status = ioctl(fd, I2C_SLAVE, SI702X_ADDRESS);                          
        if (status < 0) {                                                       
                printf("ERROR: ioctl error\n");                                 
                close(fd);                                                      
                return -1;                                                      
        }                                                                       
 
        wbuf[0] = CMD_MEASURE_TEMPERATURE_HOLD;                                 
        write(fd, wbuf, 1);                                                     
        if (read(fd, rbuf, 2) != 2)                                             
                printf("Failed to read from the i2c bus.\n");                   
 
        temperature = rbuf[1] | rbuf[0] << 8;                                   
        printf("Temperature : %.2lf *C\n", (temperature*175.72/65536) - 46.85) ;
 
        wbuf[0] = CMD_MEASURE_HUMIDITY_HOLD;                                    
        write(fd, wbuf, 1);                                                     
        if (read(fd, rbuf, 2) != 2)                                             
                printf("Failed to read from the i2c bus.\n");                   
 
        humidity = rbuf[1] | rbuf[0] << 8;                                      
        printf("Humidity : %.2lf %%\n", (humidity*125.0/65536) - 6);            
 
        close(fd);                                                              
}