Home        Store        Learn        Blog

Problem reading TSYS01 sensor

Hi all !

I’m trying to read this sensor with no succes. I need to use software I2C (to many risk with hardware one).
Here is my code:

#define TSY01_Adress 0xEE
#define TSY01_Reset 0x1E
#define TSY01_K0 0xAA
#define TSY01_K1 0xA8
#define TSY01_K2 0xA6
#define TSY01_K3 0xA4
#define TSY01_K4 0xA2
#define TSY01_ADC_Read 0x00
#define TSY01_ADC_Temp_Conv 0x48
#define TSY01_PROM_Read 0xA0

unsigned int TSYS01_K0;
unsigned int TSYS01_K1;
unsigned int TSYS01_K2;
unsigned int TSYS01_K3;
unsigned int TSYS01_K4;

unsigned int ReadK(char Adress)
{
unsigned int Result=0;

Soft_I2C_Start();
Soft_I2C_Write( TSY01_Adress  );
Soft_I2C_Write( TSY01_K0 );
//reading datas
Soft_I2C_Start();
Soft_I2C_Write( TSY01_Adress +1);
Result=(Soft_I2C_Read(1))<<8;
Result=Result+Soft_I2C_Read(0);
Soft_I2C_Stop();
delay_ms(10);

}

//------------------------------------------------------------------------------

void TSYS01_Init()
{
Soft_I2C_Start();
Soft_I2C_Write( TSY01_Adress );
Soft_I2C_Write( TSY01_Reset ); // 0b11110
Soft_I2C_Stop();
delay_ms(10);
// calibration reading
TSYS01_K0=ReadK(TSY01_K0);
TSYS01_K1=ReadK(TSY01_K1);
TSYS01_K2=ReadK(TSY01_K2);
TSYS01_K3=ReadK(TSY01_K3);
TSYS01_K4=ReadK(TSY01_K4);

}

//------------------------------------------------------------------------------

float TSYS01_ReadTemperature()
{
float Result=0;
unsigned ADC=0;

Soft_I2C_Start();
Soft_I2C_Write( TSY01_Adress );
Soft_I2C_Write( TSY01_ADC_Temp_Conv );
delay_ms(10); // (>8.2ms)
Soft_I2C_Write( TSY01_Adress);
Soft_I2C_Write( TSY01_ADC_Read );
Soft_I2C_Start();
Soft_I2C_Write( TSY01_Adress +1);

ADC=(Soft_I2C_Read(1))<<8;
ADC=(ADC+(Soft_I2C_Read(1)))<<8;
ADC=ADC+(Soft_I2C_Read(0));
ADC=ADC+(Soft_I2C_Read(0));
Soft_I2C_Stop();

Result = (-2.0)*TSYS01_K4*pow(10, -21)*pow(ADC, 4) +
            4.0 *TSYS01_K3*pow(10, -16)*pow(ADC, 3) +
            (-2.0)*TSYS01_K2*pow(10, -11)*pow(ADC, 2) +
            1.0*TSYS01_K1*pow(10, -6)*ADC +
            (-1.5)*TSYS01_K0*pow(10,-2);
            
return Result;

}

Hi @orphee,

It’s a bit difficult to respond to this meaningfully without knowing which hardware you’re using, and what isn’t working at the moment (is your code compiling? does it run? are you getting errors? are the readings incorrect?)

I’m not sure what the logic is here. What risks are you concerned about?

It would likely be beneficial to refer to the Blue Robotics TSYS01 arduino library. You’ll also want to make sure you have appropriate pull-up resistors on your SDA and SCL lines (which might already be integrated into your board, or level converter if you’re using one).

Not sure if this would even compile with “Address” spelled incorrectly?

Compilers check for consistency, not correct English, so since the same spelling is used throughout that shouldn’t cause any compilation issues. Now that you mention it though, I’ve just noticed that the ReadK function takes in char Adress and then doesn’t actually use it (but does use a hardcoded TSY01_K0 which is likely where that variable is supposed to go).

In the Blue Robotics Arduino library the reading process gets all the calibration values in a loop, into an array, with just one delay beforehand instead of one per value. That’s a more robust way of obtaining the relevant data, and avoids having to hardcode a read call and corresponding output variable for each calibration value.