Notes About Working with Various Arduino & Netduino Microcontroller Boards

Friday, December 7, 2012

Sending Data from a Netduino Plus 2 to an Arduino Leonardo Using I2C

The example Arduino sketches for the Wire library include examples of communication between two Arduinos using I2C. I wanted to try the same thing between a Netduino (Plus 2) and an Arduino (Leonardo).

Based on a posting the Netduino forums, it seems that the Netduino can only act as a master, not a slave. The Arduino can function as either a master or a slave.  In the example below, the Netduino Plus 2 serves as the I2C master sending data to the Arduino Leonardo acting as slave.

The I2C address for the slave is 0x04, though values up to 127 are allowed as addresses. No address is needed for the master. 

The code for the Arduino Leonard is the slave_receiver program found in the sample sketches for the Wire library included with the Arduino IDE.

This example requires the updated firmware for the Netduino Plus 2 to fix problems with I2C. This is a fairly easy update and only requires a few minutes.

Connect the 5V, GND, SCL, and SDA  pins of the Netduino to the Leonardo. Pull-up resistors are needed on the SCL and SDA lines. (In my original post, I said that pull-up resistors were not needed, but that was incorrect - see the comments.)

Here is the sample C# code for the Netduino, which sends a repeating cycle of bytes from 0 - 255.  Open the Arduino's serial console to see the values received from the Netduino Plus 2. 

using System.Threading;
using Microsoft.SPOT.Hardware;

namespace I2C
{
    public class Program
    {
        public static void Main()
        {
            I2CDevice slave = null;
            
            byte addr = 0x04;
            byte data = 0x00; 
            slave = new I2CDevice(new I2CDevice.Configuration(addr, 100));
            while (true)
            {
                byte[] i2cData = { data++ };
                I2CDevice.I2CTransaction[] sendData = new I2CDevice.I2CTransaction[] {
                    I2CDevice.CreateWriteTransaction(i2cData)
                };
                int bytesRead = slave.Execute(sendData, 100);
                Thread.Sleep(500);
            }
        }
    }
}

5 comments:

  1. Hello Brad,

    Are you sure there are no pull-ups needed ?

    thanks
    Henk

    ReplyDelete
    Replies
    1. Hi Henk -

      When I indicated that no pull-up resistors were needed on SCL and SDA, I may not have taken the Netduino Plus 2's internal pull-ups into account. I wrote what worked for me, but I should do some more checking into the state of the built-in pull-ups.

      Delete
  2. Hello Brad,

    Thanks for your reply,
    do you think the internal pull-ups from the ND2+ or the arduino are used ?
    I used the ND2+ in a different project, and it seems not to function with these,
    build in pull-ups.
    Also very nice article.

    thanks

    ReplyDelete
    Replies
    1. Hi Henk -

      I haven't had a chance to get back to this in the last few days, but in doing some poking around on the Web, I found a discussion that indicated that the Netduino Plus 2's built-in pull-up resistors are on by default for the SCL and SDA pins. These resistors, however, are not very strong, so they do not work with all devices. I definitely have used components that needed external pull-up resistors with I2C connections to the Netduino (and the Arduino). I'll try to find that again and post the link.

      Delete
    2. Hi Henk -

      I have done a number of tests using both an Arduino Leonardo and an Arduino Uno R3 as the slave device. Without pull-up resistors on the SDA and SCL lines to the Netduino Plus 2, I found that the code often did not work (but occasionally it did). I must have just been very lucky when I tried this back in December. With pull-up resistors (10k Ohm), the communication worked consistently.

      Interestingly enough, once the communication between the devices is established, removing the pull-up resistors has no effect. (I think this is why I thought the pull-ups weren't really needed.) If the connection between the devices is interrupted by disconnecting the SCL or SDA line and there are no pull-up resistors, the connection does not resume reliably. With the pull-up resistors in place, though, communication is reliably established again after the connections are restored.

      I still need to look into the internal pull-ups on the Netduino Plus 2, but I think it's best to say that pull-up resistors are needed on the Netduino's DA and SCL lines. Sorry if my original post had you tearing your hair out.

      Delete