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.

I don’t remember what I really did but it works.

Now, I found a new problem: temperature is OK but become wrong some hours after (22 degres to 49 degrees !). On a second system, temperature -same environment- is always good.
I was thinking it was a cable problem so sensor has been changed by a new one.
I did a soft reset and saw temperature came back to good value.
So this is my new question: there is a temperature request every 10mn, is it better to do a “reset” before each reading request (reset reading K0 to K4) ?

I did this: when temperature difference value between 2 request exceed 5 degrees I did a K request and retry (it works until now)

Hi @orphee,

There doesn’t seem to be any mention of a maximum delay time in the sensor datasheet, so it’s unexpected that the sensor would be playing up like this.

It’s perhaps worth noting that the code in your initial post seems to be missing a few stop calls - you start I2C, write some things, wait a bit, and then start again without stopping. It’s possible this (or some other part(s) of your program) are causing the sensor to misbehave.

I don’t believe this should be necessary, but it’s at least good to know that it’s working for you :slight_smile:

Hi,

It was to dangerous to have a wrong measures so I prefered to be sure it made a reset in case of big variation (my product can be used for more than 20 days and there is no possibility to make a manual correction during this time). Now I didn’t saw wrong temperature in real situation (6 days long).