Search code examples
c#.netframeworksi2cnetduino

i2c read data from sensor with Netduino


I start learning Netduino short time ago. At now, I want to use it with MS5803 30BAR sensor. This Components communicate with I2C Protocol. I learned this protocol a little bit but not enough.

I wrote introducing of code. When I came main code, I did not do anything. My code is below.

Can anybody help about this matter? I will be so pleased :)

public class Program
{
    public static void Main()
    {
        // Configuration of MS5803 30BA
        I2CDevice i2c = new I2CDevice(new I2CDevice.Configuration(0x76>>1, 400));

        byte[] read = new byte[1];

        I2CDevice.I2CTransaction[] i2cTx = new I2CDevice.I2CTransaction[1];
        i2cTx[0] = I2CDevice.CreateReadTransaction(read);


        // ???
    }
}

Solution

  • It looks like you're missing the I2C.Execute call. Without knowing anything about the device you're communicating with this will at least start the transmission.

    Try to add this line after you create the read transaction.

    i2c.Execute(i2cTX[0],500);

            byte[] returnByte = new byte[3];
    
            var readX = new I2CDevice.I2CTransaction[] {I2CDevice.CreateReadTransaction(returnByte) };
            int executed = 0;
            I2CDevice i2c = new I2CDevice(new I2CDevice.Configuration(0x76, 400));            
            executed = i2c.Execute(readX, 400);
                if (executed == 0)
                {
                    //Debug.Print("Read FAIL!");
                                    }
                else
                {
                    //Debug.Print("Read SUCCESS!");
                }
                //throw new Exception("I2C transaction failed");
    
             //you will need to do some bit shifting with the readX array to get your values.
    
        }
    

    Here an excellent document on netMF i2c: https://www.ghielectronics.com/docs/12/i2c

    The device data sheet: http://www.amsys-sensor.eu/sheets/amsys.en.ms5803_30ba.pdf