Hello,
Most Arduino users just copy&paste an example from internet and then just edit few things. Thanks to that they do not understand basics.
Your i2c address have to be bitshifted;
#define I2C_SHT40_ADDRESS 0x44<<1
//or
int i2caddress = 0x44<<1;
// 0x44h in binary 0010 1100b
// 0x88h in binary 0101 1000b
// 0x44h << 1 = 0x88h
If you do not do it like that then you need to do it in every read/write call.
You also can not use I2C methods like below
void SHT40::sampleLow(){
i2c.start();
ThisThread::sleep_for(10ms);
if(i2c.write(0xE0)==1){ //Set: Read low precision (0xE0)
i2c.read(i2caddress,SHT_DLC, 6); // request 6 bytes from slave device #8
}
i2c.stop();
}
Overloaded method I2C::write() has two variants, so you can not combinate it like you did.
- first one is for single byte transactions and Start/Stop have to be handled by you.
- second one is for complet transaction and everything is managed by that method.
So just simple command should look like this with first method
i2c.start();
i2c.write(address); // i2c address bitshifted to left by 1
i2c.write(0xE0);
i2c.stop();
But much more better is with second one
void SHT40::sampleLow(){
if(i2c.write(i2caddress,0xE0,1)==0){ //Set: Read low precision (0xE0)
i2c.read(i2caddress,SHT_DLC, 6); // request 6 bytes from slave device #8
}
}
About the scaner function.
Like above the address have to be bitshifted.
void scanI2C() {
for(int address=1;address<127;address++) {
int ack = i2c.write(address<<1, "0x94", 1); // address should be bitshifted
if (ack == 0) {
printf("\tFound at %3d - in 7bit %3x\r\n", address, address <<1);
}
if(ack != 0){
printf("I2C Error: %d\n", ack);
}
ThisThread::sleep_for(5ms);
}
}
BR, Jan