Main Site Documentation

I2C Interface and a (maybe) dodgey accelerometer


#1

Hi Guys,

First off I should point out I’m totally new to electronics - I’m a C# developer by trade and just wanted to have a mess around. Anyway, I’ve bought a Panda II and this http://www.sparkfun.com/products/9810.

After looking around and playing a bit, I’ve wired the VIN to the 3V3,GND to ground and SLC and SDA to their respective pins (all straight into the board - well via a breadboard). I’ve confirmed there’s a circut by adding an LED on ground which works fine. The problem is I can’t seem to read any data back (or indeed write any). Here’s my code, I was wondering if someone could check my logic and wiring :slight_smile:


public static void Main()
{
            // create I2C object with address 0x18 (as per datasheet)
            I2CDevice.Configuration con = new I2CDevice.Configuration(0x18, 400);
            I2CDevice i2c = new I2CDevice(con);
            // create transactions
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];

            byte[] CTRL_REG1_A = new byte[] { 0x20, 0x27 };
            byte[] CTRL_REG4_A = new byte[] { 0x23, 0x40 };

            // create write buffer (we need one byte)
            xActions[0] = I2CDevice.CreateWriteTransaction(CTRL_REG1_A);
            xActions[1] = I2CDevice.CreateWriteTransaction(CTRL_REG4_A);

            int exeResult = i2c.Execute(xActions, 1000);

            if (exeResult == 0)
            {
                Debug.Print("Unable to init I2C device at 0x18");
                return;
            }
            else
            {
                Debug.Print("I2C device initialized");
            }
}

I’m always getting zero returned from the initial write transaction. Has anyone got any ideas please? Any help would be much appreciated.

Kind Regards,

Chris Evans


#2

Welcome to the community.

Where is the read transaction in your code?


#3

Hi, thanks for the reply - I didn’t include the read transaction as I kept getting 0 returned from the write transaction - is this normal? I have just tried the I2C scanner posted previously but it found nothing unfortunately but here’s the full code:



public static void Main()
        {
            // create I2C object with address 0x18 (as per datasheet)

            byte address = 0x18;

            I2CDevice.Configuration con = new I2CDevice.Configuration(address, 100);
            I2CDevice i2c = new I2CDevice(con);
            // create transactions
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];

            byte[] CTRL_REG1_A = new byte[] { 0x20, 0x27 };
            byte[] CTRL_REG4_A = new byte[] { 0x23, 0x40 };

            // create write buffer (we need one byte)
            xActions[0] = I2CDevice.CreateWriteTransaction(CTRL_REG1_A);
            xActions[1] = I2CDevice.CreateWriteTransaction(CTRL_REG4_A);

            int exeResult = i2c.Execute(xActions, 1000);

            if (exeResult == 0)
            {
                Debug.Print("Unable to init I2C device at 0x18");
                return;
            }
            else
            {
                Debug.Print("I2C device initialized");
            }

            if (exeResult > 0)
            {
                Debug.Print("Trying " + address.ToString());

                try
                {
                    byte[] buffer = new byte[6];

                    buffer[0] = ReadIC2Register(i2c, 0x28);//read OUT_X_L_A (MSB)
                    buffer[1] = ReadIC2Register(i2c, 0x29);//read OUT_X_H_A (LSB)
                    buffer[2] = ReadIC2Register(i2c, 0x2A);//read OUT_Y_L_A (MSB)
                    buffer[3] = ReadIC2Register(i2c, 0x2B);//read OUT_Y_H_A (LSB)
                    buffer[4] = ReadIC2Register(i2c, 0x2C);//read OUT_Z_L_A (MSB)
                    buffer[5] = ReadIC2Register(i2c, 0x2D);//read OUT_Z_H_A (LSB)

                    int Ax = (int)(buffer[0] << 8) + buffer[1];
                    int Ay = (int)(buffer[2] << 8) + buffer[3];
                    int Az = (int)(buffer[4] << 8) + buffer[5];

                    Debug.Print("[" + Ax + "," + Ay + "," + Az + "]");

                    Thread.Sleep(100);
                }
                catch (Exception ex)
                {
                    Debug.Print("Address: " + address.ToString() + " threw an exception");
                }
            }

        }

        public static byte ReadIC2Register(I2CDevice device, byte register)
        {
            byte[] write = new byte[] { register };
            byte[] read = new byte[1];
            I2CDevice.I2CTransaction[] readValues = new I2CDevice.I2CTransaction[] { 
                I2CDevice.CreateWriteTransaction(write),
                I2CDevice.CreateReadTransaction(read)
            };

            int result = device.Execute(readValues, 1000);

            return (byte)read[0];
        }
    }



#4

This part has I2C pulled up to 1.8V. If you are always open collector, then you are probably OK (depending on what Panda sees as a logic 1) but if you have ever driven the I2C lines at 3.3V (during config, possibly), then you might have hurt the tiny board.