Hi
I have used the BME280 pressure and temperature sensor on my Jetson TX1 board.
I connect the SDA and SCL pins to pull up resistors (4.7K) just like the sensor’s datasheet says.
when I run the command i2cdetect -r -y 1 on bus one, the result of the command:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- 4a -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- 76 --
the 0x76 address is the slave address of the BME280 sensor.
when I try to read the values of the register on the sensor with this code, I get some non-sense data from the sensor. all bits of the data for some registers are 1…
I don’t know why…
this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#ifdef __cplusplus
extern "C" {
#endif
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
#ifdef __cplusplus
}
#endif
struct BME280_DATA_TEMPLATE{
unsigned char press_msb;
unsigned char press_lsb;
unsigned char press_xlsb;
unsigned char temp_msb;
unsigned char temp_lb;
unsigned char temp_xlsb;
unsigned char hum_msb;
unsigned char hum_lsb;
};
int main(){
// init data template struct:
struct BME280_DATA_TEMPLATE template;
template.press_msb = 0xF7;
template.press_lsb = 0xF8;
template.press_xlsb = 0xF9;
template.temp_msb = 0xFA;
template.temp_lsb = 0xFB;
template.temp_xlsb = 0xFC;
template.hum_msb = 0xFD;
template.hum_lsb = 0xFE;
int file;
int adapter_nr = 1;
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
file = open(filename, O_RDWR);
if (file < 0){
printf("error about opening i2c-0 \n");
exit(1);
}
printf("not error about openning \n");
int addr = 0x76;
if(ioctl(file, I2C_SLAVE, addr) < 0){
printf("error abour openning address \n");
exit(1);
}
__s32 res;
char buf[10];
for (int c = 0; c <= 8; c++)
{
__u8 reg = template.press_msb + c;
res = i2c_smbus_read_word_data(file, reg);
if (res < 0){
printf("reg is: %c \t res is : %i \n", reg, res);
printf("read error \n");
exit(1);
}
else{
printf("res is: %i ",res);
printf("read success \n");
// exit(1);
}
}
return 0;
}
the output of this code is:
not error about openning
res is: 128 read success
res is: 0 read success
res is: 32768 read success
res is: 128 read success
res is: 0 read success
res is: 32768 read success
res is: 128 read success
res is: 32768 read success
res is: 128 read success
is that a valid output of the sensor?
what can I do?