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

I2C on Netduino 1 not working

netduino i2c

  • Please log in to reply
23 replies to this topic

#1 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 08 February 2013 - 07:18 AM

Hi,

 

I need to communicate with I2C sensor on some project. I do not know why, but after adding any code that executes I2C transaction, program breaks on exception.

 

using System;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;namespace NetduinoSandbox{    public class Program    {        public static void Main()        {            I2CDevice.Configuration config = new I2CDevice.Configuration(1, 1);            I2CDevice device = new I2CDevice(config);            I2CDevice.I2CTransaction[] array = new I2CDevice.I2CTransaction[1];            byte[] data = new byte[2];            array[0] = I2CDevice.CreateWriteTransaction(data);            device.Execute(array,1000);        }    }}

 

 



#2 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 February 2013 - 07:27 AM

What exception? I think you are using incorrect configuration parameters, the clock rate must be >= 10 [kHz]. Also, slave address 1 seems a little bit suspicious, although it should not cause any exception.



#3 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 08 February 2013 - 07:59 AM

that doesent look right:

new I2CDevice.Configuration(1, 1);

 

wich device it is? name

 

and what about pullups on i2c lines?



#4 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 08 February 2013 - 08:07 AM

What exception? I think you are using incorrect configuration parameters, the clock rate must be >= 10 [kHz]. Also, slave address 1 seems a little bit suspicious, although it should not cause any exception.

Hi,

 

Thanks,

You are right about configuration parameters. I have increased clock rate to 15 kHz. Now it does not cause exception. But, unfortunately, I am still not able to communicate with the sensor, maybe wrong address.

Also, which pin is SDA, and which SCL? I have wired SDA to A4, SCL to A5.

 

If you could find me correct address, its this sensor .

 

@NooM: I am not using pull-up resistors, which value would be best?



#5 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 08 February 2013 - 08:17 AM

iam using 4.7 kilo-ohms, they work well.

 

hm, havent found a good datasheet for that :(

so no idea what the i2c address is.



#6 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 08 February 2013 - 08:58 AM

If I remember it correctly, LEGO NXT uses software I2C ("bit-banged"), which does not comply to the I2C protocol specification, so I think you'll not be able to communicate with sensor using Netduino hardware I2C module. You'd probably need to have a look at LEGO NXT firmware source code or information published by various hackers that were able to communicate with LEGO sensors using Arduino, mbed etc., the best would be to capture the communication between the brick and sensor with a logic analyzer.



#7 Stanislav Husár

Stanislav Husár

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts

Posted 08 February 2013 - 09:36 AM

@Noom: Thanks, 4K7 is working well.

 

@CW2: Maybe NXT Brick, but I am not using NXT Brick. ;)

 

@All: So, it seems to be working fine. Using following code, when you connect HiTechnic Color Sensor V2 to I2C bus, you will see color number in output window of Visual Studio.

                I2CDevice.Configuration config = new I2CDevice.Configuration(1, 12);                I2CDevice device = new I2CDevice(config);                I2CDevice.I2CTransaction[] trans = new I2CDevice.I2CTransaction[2];                byte[] mosi = new byte[1];                byte[] miso = new byte[1];                mosi[0] = 0x42;                trans[0] = I2CDevice.CreateWriteTransaction(mosi);                trans[1] = I2CDevice.CreateReadTransaction(miso);                while (true)                {                    device.Execute(trans, Timeout.Infinite);                    Debug.Print(miso[0].ToString());                }


#8 Gutworks

Gutworks

    Advanced Member

  • Members
  • PipPipPip
  • 363 posts
  • LocationOttawa, Ontario

Posted 08 February 2013 - 04:44 PM

Very interesting....

 

I think I just found a new afternoon project :)

 

Thanks Stanislav!



#9 kersten

kersten

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationGermany

Posted 12 March 2013 - 11:39 PM

Hallo,

I have some days are a "Netduino 2". One of my first applications should connect to the I2C bus of an LCD display. I have tried a variety of source code, but all the same error.  Even if no connection to the I2C hardware is present, is the same error. What am I doing wrong? Here's my simple code: I2CDevice.Configuration config = new I2CDevice.Configuration (0xDE, 1); I2CDevice device = new I2CDevice (config); I2CDevice.I2CTransaction [] array = new I2CDevice.I2CTransaction [1]; byte [] buffer = new byte [1]; buffer [0] = 0x0C; array [0] = I2CDevice.CreateWriteTransaction (buffer); device.Execute (array, 1000); 

>> here comes the error:

'System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll. " "device.m_xAction": The value of the "m_xAction" can not be retrieved because no information about the containing class is available.  



#10 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 13 March 2013 - 07:55 AM

I2C bus of an LCD display...System.ArgumentException' occurred in Microsoft.SPOT.Hardware.dll

  The slave address 0xDE in I2CDevice.Configuration constructor is out of range, .NET MF does not include the R/W bit in the address, so the max value is 0x7F. It would help to know what LCD you are using, but I guess the address should be 0x6F.   Welcome to the community!



#11 kersten

kersten

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationGermany

Posted 13 March 2013 - 09:13 PM

Thank you "CW2" for the helpful info, after the change of address to 0x6F is not an "exception". But the connection still does not work. The following code: " I2CDevice.Configuration config = new I2CDevice.Configuration (0x6F, 100); I2CDevice device = new I2CDevice (config); I2CDevice.I2CTransaction [] array = new I2CDevice.I2CTransaction [1]; Encoding code = Encoding.UTF8; byte [] buffer = code.GetBytes write ("#DL" + "n"); array [0] = I2CDevice.CreateWriteTransaction (write buffer); COUNT_1 device.Execute int = (array, 1000); Debug.Print ("count1" count_1.ToString + ()); array [0] = I2CDevice.CreateWriteTransaction (write buffer); int = COUNT_2 device.Execute (array, 1000); Debug.Print ("count2" count_2.ToString + ()); " The return value of COUNT_1 = 0 but of COUNT_2 = 4 (sometimes even 0). The display should be deleted with this command, but no response .. The connection via I2C bus by a different controller (not Netduino) to the display is working properly. The pullup resistors to VCC or 4.7 k What can I do?

#12 NooM

NooM

    Advanced Member

  • Members
  • PipPipPip
  • 490 posts
  • LocationAustria

Posted 13 March 2013 - 11:18 PM

id look at an existring driver for that display (netduino) so you can see the settings and such.

 

id also use an multi i2c class. (link in my signature)



#13 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 14 March 2013 - 07:40 AM

The connection via I2C bus by a different controller (not Netduino) to the display is working properly.

  Could you please tell us what display is that, or better yet link to its datasheet?



#14 kersten

kersten

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationGermany

Posted 14 March 2013 - 12:47 PM

here is a link to this Display:
http://www.lcd-modul.../edip160-7e.pdf

The connection to the I2C EEPROM works.
The code to the LCD has been modified with the length byte and checksum but that does not:

I2CDevice.Configuration config = new I2CDevice.Configuration(0x6F, 100); //0xDE
I2CDevice device = new I2CDevice(config);
I2CDevice.I2CTransaction[] array = new I2CDevice.I2CTransaction[2];

Encoding code = Encoding.UTF8;
byte[] text = code.GetBytes("#DL" + "n"); // clear the display
byte[] buffer = new byte[text.Length+3];
buffer[0] = (byte)(0x11); // DC1
buffer[1] = Byte.Parse(text.Length.ToString());
int bcc = buffer[0] + buffer[1];
byte i = 0;
while (i < text.Length) { bcc = bcc + (byte)text[i]; i++; }
text.CopyTo(buffer, 2);
buffer[text.Length + 2] = (byte)bcc; // checksum
array[0] = I2CDevice.CreateWriteTransaction(buffer);
byte[] ack = new byte[1];
array[1] = I2CDevice.CreateReadTransaction(ack); // 0x06 should be returned
int count = device.Execute(array, 100);
Debug.Print("count: " + count.ToString());

#15 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 14 March 2013 - 01:20 PM

What is the value of count (result of Execute() call)? If it is non-zero, check the ack[0] value. Otherwise, try executing transactions one-by-one and check the result of Execute() method.



#16 kersten

kersten

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationGermany

Posted 14 March 2013 - 05:47 PM

I found the cause: the display is set to power down mode. The power consumption is lowered to a minimum. The display remains visible. By addressing the I2C address of the display should be awakened. Does it not! I suspect the time between the writing of the address and the data is too low. Without power-down mode, the connection is normal. Thanks you for the help!

#17 kersten

kersten

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationGermany

Posted 18 March 2013 - 02:03 PM

Hi, I again problems with the I2C bus in connection with the display and the serial EEPROM. When code in I2CBus "transferred = _slaveDevice.Execute (writeXAction, transactionTimeout)" Transferred is always 0 and there is also no exception. The multimeter can be seen in SCL and SDL has a frequency of 0.34 kHz and 4.85 V, and this is always constant. The motherboard is defective?

#18 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 March 2013 - 04:06 PM

I again problems with the I2C bus in connection with the display and the serial EEPROM.

  What operation does the program perform? What EEPROM do you use? Some EEPROMs require repeated start condition, there is usually mandatory timeout after write etc.

 

Regarding the multimeter results, it displays average (or RMS) voltage, you'd need a logic analyzer or oscilloscope to inspect the I2C waveforms in detail.



#19 kersten

kersten

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationGermany

Posted 18 March 2013 - 05:09 PM

Hi CW2, The EEPROM is a Microchip 24LC64 (A0,A1,A2,WP = 0) Here is my Testcode: public static void Main() { I2CDevice.Configuration _slaveConfig = new I2CDevice.Configuration(0x50, 100); uint adresse = 0x01; byte[] buffer = new byte[2]; buffer[0] = (byte)(adresse >> 8); buffer[1] = (byte)adresse; int count = I2CBus.GetInstance().Write(_slaveConfig, buffer, 500); byte[] data = new byte[] { 0x42 }; count = I2CBus.GetInstance().Write(_slaveConfig, data, 500); Debug.Print("Count: " + count.ToString()); } The I2C connection to the display is the same. Another board with an ATmega32 working on the bus without problems

#20 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 18 March 2013 - 07:49 PM

For byte write operation there should be only one write transaction with 3 byte buffer (2 byte address followed by the data byte).

 

Your code issues the following I2C commands (two separate write transactions):

 

START ControlByte [color=#0000ff;]ACK[/color] AddressHigh [color=#0000ff;]ACK[/color] AddressLow [color=#0000ff;]ACK[/color] STOP   START ControlByte [color=#0000ff;]ACK[/color] Data [color=#0000ff;]ACK[/color] STOP

 

while the EEPROM expects (figure 6-1 in the datasheet):

 

START ControlByte [color=#0000ff;]ACK[/color] AddressHigh [color=#0000ff;]ACK[/color] AddressLow [color=#0000ff;]ACK[/color] Data [color=#0000ff;]ACK[/color] STOP

 

where ControlByte is SlaveAddress + R/W bit, and [color=#0000ff;]ACK[/color] is slave acknowledge.







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.