Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

how to do simple I2c reading


  • Please log in to reply
10 replies to this topic

#1 henryyao

henryyao

    Member

  • Members
  • PipPip
  • 19 posts

Posted 21 December 2012 - 11:18 PM

Hi,i'm having some problem in reading data from I2C bus. The slave is a temperature sensor with i2c address 0xD0.
Following is my code.I'm having an unhandled exception.
Please let me know what's is problem.

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.NetduinoPlus;

namespace i2c
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            byte[] buffer = new byte[10] ;
            I2CDevice.Configuration config = new I2CDevice.Configuration(0xD0,100);
            I2CDevice dev = new I2CDevice(config);
           
            I2CDevice.I2CWriteTransaction write = I2CDevice.CreateWriteTransaction(new byte[1] { 0 });
            
            I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(buffer);
              
            I2CDevice.I2CTransaction[] actions = new I2CDevice.I2CTransaction[] { write, read };
            dev.Execute(actions, 1000);

            Debug.Print(buffer[0].ToString());

        }

    }
}


#2 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 21 December 2012 - 11:52 PM

What Netduino Version are you using and what firmware is installed? Also what type of sensor is this? Is it a breakout board?

#3 henryyao

henryyao

    Member

  • Members
  • PipPip
  • 19 posts

Posted 22 December 2012 - 03:32 AM

What Netduino Version are you using and what firmware is installed? Also what type of sensor is this? Is it a breakout board?


Hi Dave, I'm using a Netduino plus and firmware 4.2.
It is a Tpa81 sensor, here is the link to this sensor: http://www.robot-ele...m/tpa81tech.htm
And it is not a breakout board, I guess.

#4 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 22 December 2012 - 03:42 AM

Are you using external pullup resistors on the SDA and SCL lines? I think with that sensor you can use two 1.8k resistors. Also the address on the Netduino should be set for 0x68 instead of 0xD0. Netduino needs only the 7 most significant bits for the address.

#5 henryyao

henryyao

    Member

  • Members
  • PipPip
  • 19 posts

Posted 22 December 2012 - 04:31 PM

Are you using external pullup resistors on the SDA and SCL lines? I think with that sensor you can use two 1.8k resistors.

Also the address on the Netduino should be set for 0x68 instead of 0xD0. Netduino needs only the 7 most significant bits for the address.


Yes, I'm using two pull up resister 2.2k. I have tested the 0x68, it does not work either.
I step into the I2CDevice dev = new I2CDevice(config); and it gives me a "No source aviable" page.
:
Locating source for 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'. Checksum: MD5 {a5 f7 23 70 92 46 16 e3 2a 4d b1 44 79 3a ba a}
The file 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs' does not exist.
Looking in script documents for 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'...
Looking in the projects for 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'.
The file was not found in a project.
Looking in directory 'F:\Program Files\C# Express 2010\Common7\IDE\vc7\atlmfc'...
Looking in directory 'F:\Program Files\C# Express 2010\Common7\IDE\vc7\crt'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs.
The debugger could not locate the source file 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'.

#6 henryyao

henryyao

    Member

  • Members
  • PipPip
  • 19 posts

Posted 25 December 2012 - 03:07 AM

Is there anyone here can help me with this?

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 25 December 2012 - 04:05 AM

Hi Henry, Are you sure that the address is 0xD0 and not 0x50? I2C addresses are generally 7-bit (with the top bit reserved for the write/read indicator). Chris

#8 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 25 December 2012 - 12:34 PM

Yes, I'm using two pull up resister 2.2k. I have tested the 0x68, it does not work either.

What does "it does not work either" mean exactly? You should not get the argument exception with 0x68 slave address.

I step into the I2CDevice dev = new I2CDevice(config); and it gives me a "No source avaible" page.

Well, unless you have the source code, you will not be able to step into .NET MF classes - just use 'step over'.

Also, although the documentation does not contain detailed description of the communication protocol, the linked tutorial mentions that repeated start condition is needed when reading from or writing to the slave device; you'd need special I2C methods to achieve that.

#9 henryyao

henryyao

    Member

  • Members
  • PipPip
  • 19 posts

Posted 30 December 2012 - 06:08 PM

Hi Henry, Are you sure that the address is 0xD0 and not 0x50? I2C addresses are generally 7-bit (with the top bit reserved for the write/read indicator). Chris

 

Thanks for that. The address is 0x68 in fact. It's a half of 0xD0. I'm corrected this and I keep getting strange data. It is not what its supposed to be. I guess the problem may be my TPA sensor. 



#10 henryyao

henryyao

    Member

  • Members
  • PipPip
  • 19 posts

Posted 30 December 2012 - 07:11 PM

What does "it does not work either" mean exactly? You should not get the argument exception with 0x68 slave address.


Well, unless you have the source code, you will not be able to step into .NET MF classes - just use 'step over'.

Also, although the documentation does not contain detailed description of the communication protocol, the linked tutorial mentions that repeated start condition is needed when reading from or writing to the slave device; you'd need special I2C methods to achieve that.

Hi CW2,

 

I did get some result from 0x68 address, however the results are strange.

 

I did not use the special I2C method you provided, instead, i changed my code to the following, so I think a startup sequence is triggered at each dev.Execute.

However, the results are still the same strange ones. Now the thing is, do I really have to use the special I2C method or its the problem with my sensor.

            I2CDevice.I2CTransaction[] action1 = new I2CDevice.I2CTransaction[] { write };            I2CDevice.I2CTransaction[] action2 = new I2CDevice.I2CTransaction[] { read };                     dev.Execute(action1, 1000);            dev.Execute(action2, 1000); 


#11 henryyao

henryyao

    Member

  • Members
  • PipPip
  • 19 posts

Posted 04 January 2013 - 08:48 PM

Thanks everyone, I have ordered the new TPA sensor and it turns out the problem was with the old sensor. :(

Waste a lot of time on this...






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.