I2C communication

Hi,

I bought some DS1631Z thermo sensors, but I have big troble to communicate through I2C (using one per netduino),
I think I try every example I found through google, but nothing work for me :frowning:

If you know about any working code, please let me know, or
if it’s not very difficult, my config is all A pins grounded for someone who know I2C and can write code example fo me.

Thanks so much, after third day of trying, I think it’s impossible…

@ cyberh0me - Hi, thanks for quick reply, I try this example too, but this ends with unexpected error and program exit without more info.

You may need pullup resistors (2.2k) on the SDA/SCL lines (if you are using a Netduino). I am pretty sure the Netduino does not have them.

@ munderhill - Hello, thank, I have it.
All three A pins grounded, ground grounded, vcc I try with 3,3 and 5V too, no change and SD and SC are connected to 3,3/5V through 2,2/4,7K resistor.

Post your code

@ munderhill - at last, I try ithis code:


using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication2
{
    public class Program
    {
        public static void Main()
        {
            //create I2C object
            //note that the netmf i2cdevice configuration requires a 7-bit address! It set the 8th R/W bit automatically.
            I2CDevice.Configuration con = new I2CDevice.Configuration(0xAA, 400);
            I2CDevice MyI2C = new I2CDevice(con);

            // Create transactions
            // We need 2 in this example, we are reading from the device
            // First transaction is writing the "read command"
            // Second transaction is reading the data
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];

            // create write buffer (we need one byte)
            byte[] RegisterNum = new byte[1] { 2 };
            xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);
            // create read buffer to read the register
            byte[] RegisterValue = new byte[1];
            xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);

            // Now we access the I2C bus using a timeout of one second 
            // if the execute command returns zero, the transaction failed (this 
            // is a good check to make sure that you are communicating with the device correctly
            // and don't have a wiring issue or other problem with the I2C device)
            if (MyI2C.Execute(xActions, 1000) == 0)
            {
                Debug.Print("Failed to perform I2C transaction");
            }
            else
            {
                Debug.Print("Register value: " + RegisterValue[0].ToString());
            }
        }
    }
}


before:



                   Dim config As I2CDevice.Configuration = New I2CDevice.Configuration(1, 100)
                   Dim i2c As I2CDevice = New I2CDevice(config)


                   Dim write As Byte = ""
            Dim read As byte  = new byte[1]

                   ' create I2C write and read transaction
                   Dim i2cTx As I2CDevice.I2CTransaction = New I2CDevice.I2CTransaction(1)
                   i2cTx(0) = I2CDevice.CreateWriteTransaction(write)
                   i2cTx(1) = I2CDevice.CreateReadTransaction(read)


                   ' execution
                   i2c.Execute(i2cTx, I2C_TIMEOUT)


and before I found some libraries, but nothing with good result.

If I am reading the datasheet correctly, I think the address starts at 0x48 and goes through 0x4F, so with all the address lines tied to ground the would be 0x48.

@ munderhill - app still exit, but now i pause it at end and see, there was a debug message: “Failed to perform I2C transaction”

I think it’s because i’m not sending proper “code”

If i’m right, I must write “0x51” and then read “0xAA”? and if yes, then I don’t know where I must enter values, can you help me?

(sorry for maybe basic question, but i never meet i2c before)

Thanks

Try this.


           var xActions = new I2CDevice.I2CTransaction[1];

	// START Conversions
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x51 });
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

	// READ Temp
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xAA });
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

            var data = new byte[2];
            xActions[0] = I2CDevice.CreateReadTransaction(data);
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

	// STOP Conversions
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x22 });
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

@ munderhill - I’m happy for your code, it looks like what I need, but I still have no answer from sensor

i’m using this code, but if I break code at last debug print, data[0] and data[1] is still 0x00:


using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace NetduinoApplication3
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            I2CDevice.Configuration con = new I2CDevice.Configuration(0x48, 400);
            I2CDevice I2C = new I2CDevice(con);

            //I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
            
            var xActions = new I2CDevice.I2CTransaction[1];

            // START Conversions
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x51 });
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

            // READ Temp
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0xAA });
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

            var data = new byte[2];
            xActions[0] = I2CDevice.CreateReadTransaction(data);
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

            // STOP Conversions
            xActions[0] = I2CDevice.CreateWriteTransaction(new byte[] { 0x22 });
            I2C.Execute(xActions, 1000);
            Thread.Sleep(5);

            Debug.Print(data[0].ToString());
            Debug.Print(data[1].ToString()); // breakpoint here
            
            //byte[] RegisterNum = new byte[1] { 2 };
            //xActions[0] = I2CDevice.CreateWriteTransaction(RegisterNum);

            //byte[] RegisterValue = new byte[1];
            //xActions[1] = I2CDevice.CreateReadTransaction(RegisterValue);

            //if (I2C.Execute(xActions, 1000) == 0)
            //{
            //    Debug.Print("Failed to perform I2C transaction");
            //}
            //else
            //{
            //    Debug.Print("Register value: " + RegisterValue[0].ToString());
            //}
            
            Thread.Sleep(10000000); 
        }

    }
}


@ munderhill - Dear Munderhill, now sensor works perfectly,
last trobles are my mistake, I have switched SC pin with SD.

Thank you very very very very very much for your help and time, you’re great :slight_smile:

Peter

Glad to help!