Trouble with TPA81 reading results

I have a TPA81 and I can read a pixel a time and get the correct results.
But if I read all values in one time to an array, when I read the result array it looses values.
I can’t read all the values. Is there an explanation?

The code:


byte[] result = new byte[9];
SoftwareI2CBus.I2CDevice tpa81 = i2c.CreateI2CDevice(
                (ushort)(0xD0 >> 1) ,
                0 );

                int read;
                int writen;
                
                tpa81.WriteRead(new byte[] { 0x01 }, 0, 1, result, 0, 9, out writen, out read);
                Debug.Print("AMBIENT: " + result[0]);

                for (int i =1; i <9; i++)
                    Debug.Print("PIXEL" + i + ": " + result[i]);


Example of output (it should have Ambient and 8 pixels):


/*
Output:
Reading from TPA81 successfull, time: 1
Pixel 1: 16
Pixel 2: 15
Pixel 3: 22
Pixel 4: 16
Pixel 5: 21
Pixel 6: 17
Reading from TPA81 successfull, time: 1
Pixel 1: 17
Pixel 4: 15
Pixel 5: 21
Pixel 6: 17
Pixel 7: 21
Reading from TPA81 successfull, time: 1
Ambiente:17
Pixel 1: 14
Pixel 2: 16
Pixel 3: 14
Pixel 6: 17
Reading from TPA81 successfull, time: 1
Ambiente:17
Pixel 0: 17
Pixel 3: 12
Pixel 5: 24
Pixel 6: 20
Pixel 7: 17
Pixel 8: 17
*/


@ codewizpt - Welcome to the community!

The code you posted and the output do not match, can I assume that you only tweaked the string literals?

Have you checked the data in the debugger by inspecting the data in the array? I have seen that Debug.Print sometimes does not successfully return data to the debugger and looking at your output that seems like what is happening.

interesting sensor by the way!

1 Like

@ taylorza thanks for the help.
Indeed it was the debugger that didn’t show the results. The array has all the pixels filled.
I added a

 in the for loop and it debugged correctly!

@ codewizpt - It is a pleasure, I am glad I could help.