Page 1 of 2

Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 09 Apr 2012, 22:30
by warrocket94
Hi
i'm Davide and i bought the TPA81 three weeks ago. I would to use the sensor with my Lego Mindstorm NXT 2.0 and i would to read the temperature from the sensor. The hardware part is finished:
I use four pins of the TPA 81: I use Vcc +5V and i connect this pin to the green wire (+4,3V supply) of the Lego cable, then i connect the ground pins of the TPA81 to the red wire of the Lego cable. Then i connect the SCL pin of the TPA81 to the yellow wire of the Lego Cable and finally i connect the SDA pins of the TPA81 the blue wire of the lego cable. I use two 1.8k pull up resistor on the SDA and SCL lines. Is it a right connection to use TPA 81 Sensor?
I use NXC Language to programme the Lego Mindstorm and i use Bricx Command Center software but i can't programme the TPA81 because every time there are some File error. I see, in this forum, a topic that concern this argument but i don't understand very well. Is there someone that can write me the programme to read the temperature from TPA81 using NXC Language.

Thank you very much in advance.

Davide

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 01:51
by mattallen37
Due to the NXT HW setup, your pullups should be 82k instead of 1.8k. I personally have been able to use pullups in the range of 47k to 100k, and even 10k with varying degrees of success. However, 1.8k is far too strong. Other than that, your HW setup sounds fine.

Can you post your NXC code for us to see?

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 11:59
by warrocket94
yes this is the code that i try but it's wrong!
//#define S1 1
//#define I2C_ADDR_DEFAULT 0xD0
//#define I2C_REG_CMD 0x11

//char ReadI2CRegister ( byte port,
// byte i2caddr,
// byte reg,
// byte & out
// ) [inline]

byte out;
char result;
byte CMD_Read[] = {0xD1, 0x11};
task main()
{
SetSensorType(S2, IN_TYPE_CUSTOM);
SetSensorMode(S2, IN_MODE_RAW);
while (I2CCheckStatus(S2) == STAT_COMM_PENDING);
do
{
result = ReadI2CRegister(0, 0xD0, 0x11, out);
//I2CWrite(S1, 2, CMD_Read); // Send the message
TextOut(1,LCD_LINE1,NumToStr(out));
TextOut(1,LCD_LINE2,NumToStr(result));
Wait(1000);
}
while(1);
}


the lcd lego display show me two values: "0" is the first value that the lcd lego display show me and the second value is "-32".. please help me to read the temperature!! :D

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 14:28
by warrocket94
Yeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeahhhhhhhhhhhhhhh!!!!!!!!!!!!!!!!!!!!!!!!!!!!! i read the temperature on the lcd lego display from the TPA81 using NXC language :D i use this code:
// READ TPA81
#define TPA81_PORT IN_1
#define TPA81_ADDR 0xD0

void MyI2CRead(byte port, byte addr, byte reg, byte cnt, byte& outbuf[])
{
byte cmdbuf[]; // register number holder
int loop;
byte nByteReady = 0;

ArrayBuild(cmdbuf, addr, reg);

loop = STAT_COMM_PENDING; //Wait until bus is free
while (loop == STAT_COMM_PENDING ){ // ''
loop = I2CStatus(port, nByteReady); // ''
} // ''

I2CBytes(port, cmdbuf, cnt, outbuf) //Read the registers
}

task main()
{
SetSensorLowspeed(TPA81_PORT);

byte inbuf[10];

while( 1 )
{
MyI2CRead(TPA81_PORT, TPA81_ADDR, 0, 10, inbuf);
NumOut( 0, LCD_LINE1, inbuf[5], DRAW_OPT_CLEAR_WHOLE_SCREEN );

}
}


I find this code into a topic in this forum and i try to use it. Then i download the code on my Lego Mindstorm NXT and on the lcd lego display is appeared some values ("17-20-21-22") and when i put a candle near the sensor those values change in 120-130-128-123 :D:D i'm very happy :D

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 15:12
by mattallen37
Cool, I'm glad you got it working!

BTW, here is the latest version of my I2C library:
MyI2C lib.nxc
(3.55 KiB) Downloaded 365 times
This version should be thread-safe.

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 15:51
by warrocket94
Yes :) but the TPA81 has got 8 pixel to read the temperature. With this programme i read only one value of temperature but can i read the temperature of all eight pixel? How i can do it?

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 16:06
by mattallen37
Take a look at this page; it has a chart of all the I2C registers. The way you have your program setup, you are reading all 10 of them. According to your program, all the pixel data gets stored in the inbuf[] array, in elements 2 through 9. You are only displaying element 5 (pixel 4).

Try this:

Code: Select all

// READ TPA81
#define TPA81_PORT IN_1
#define TPA81_ADDR 0xD0

safecall void MyI2CRead(byte port, byte addr, byte reg, byte cnt, byte& outbuf[])
{
  byte cmdbuf[];                                                      // The buffer of data that will be sent
  byte nByteReady;                                                    // required for getting the status

  ArrayBuild(cmdbuf, addr, reg);                                      // Build the output buffer

  while (I2CStatus(port, nByteReady) ==  STAT_COMM_PENDING){Yield();} // Wait until bus is free

  I2CBytes(port, cmdbuf, cnt, outbuf)    //Read the registers

  while (I2CStatus(port, nByteReady) ==  STAT_COMM_PENDING){Yield();} // Wait until bus is free
}

task main()
{
  SetSensorLowspeed(TPA81_PORT);

  byte inbuf[10];

  while(true)
  {
    MyI2CRead(TPA81_PORT, TPA81_ADDR, 0, 10, inbuf);
    ClearScreen();
    for (int i = 0; i < 8; i ++){
      NumOut(0, LCD_LINE1 - (i*8), inbuf[i+2]);
    }
    Wait(50);
  }
}
BTW, it's helpful if you use code tags when posting code on forums:

Code: Select all

[code]
[/code]

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 16:24
by warrocket94
yes this is great :D thank you very much but the sensitivity of tpa81 is low.... i put a candle in front of the sensor but only one value of the pixel increase...for example if i put a candle in front of the tpa81 the values that i see on the lcd lego display are: 25,21,31,65,103,21,23,26
why this?

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 17:26
by mattallen37
I have no experience with the sensor. All my knowledge of it came from that page I linked to.

It would be my guess though, that only two of the sensors are being effected (the 65 and the 103). Probably all the sensors (pixels) span a horizon. If you were to move the flame compared to the sensor, you would likely see the higher readings from some of the other sensors (pixels).

Re: Read the temperature from the TPA81 Sensor with Lego NXT 2.0

Posted: 10 Apr 2012, 17:50
by warrocket94
ok thank you very much... i have a last question to ask!? Can i open a topic in this forum to explain how built a TPA 81 sensor and how can you programme it with NXC. Because on the web there aren't examples in NXC