title: I2C connection between Windows IOT and Arduino Nano using C#
body: <p>I'm pretty new in the world of Raspberry and Arduino (especially together with Windows IoT). My plan is, to readout different sensors (temp and pressure) with the Arduino and then sending the values to my RP2 where I can use them on a GUI. </p>
  
  <p>So, it's not a problem to readout the sensors. I receive the correct values. Afterwards I'm sending them to the I²C bus (based on the requirements of the Wire.h lib). </p>
  
  <p>For my RP2 I found two similar projects and my code in C# is based on those. </p>
  
  <p>So far, so good, but when I start both devices I don't get any data on my RP. My RP finds the Arduino. </p>
  
  <p>To localize the problem I'm using a Wire.h sample sketch for sending random values to my RP. But there is still the same problem, so I guess there is a problem with my C# code. Also I set a breakpoint at place where my RP should write the values into a array. But it looks like, there are no incoming values. </p>
  
  <p>I attached both codes. Hope somebody can help me. I'm stuck pretty bad... </p>
  
  <p>Thank you very much!
  Thiemo</p>
  
  <p>Arduino code:</p>
  
  <pre><code>#include &lt;Wire.h&gt;
    #define SLAVE_ADDRESS 0x40
  
  
  byte val = 0;
  byte val_2 = 100;
  
  void setup()
  {
    Wire.begin(SLAVE_ADDRESS); // join i2c bus
    Serial.begin(9600);
  }
  
  void loop()
  {
    Wire.beginTransmission(SLAVE_ADDRESS); // transmit to master device
                                // device address is specified in datasheet
    Wire.write(val);             // sends value byte  
    Wire.write (val_2);
   // Serial.write(val);
  //  Serial.write(val_2);
    Wire.endTransmission();     // stop transmitting
  
    val++;        // increment value
    val_2++;
    if(val == 64) // if reached 64th position (max)
    {
      val = 0;    // start over from lowest value
    }
  
    if (val_2 == 164)
    {
      val = 100;  
    }
    delay(500);
  }
  </code></pre>
  
  <p>C# code:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Runtime.InteropServices.WindowsRuntime;
  using Windows.Foundation;
  using Windows.Foundation.Collections;
  using Windows.UI.Xaml;
  using Windows.UI.Xaml.Controls;
  using Windows.UI.Xaml.Controls.Primitives;
  using Windows.UI.Xaml.Data;
  using Windows.UI.Xaml.Input;
  using Windows.UI.Xaml.Media;
  using Windows.UI.Xaml.Navigation;
  // i2c Libs
  using Windows.Devices.Enumeration;
  using Windows.Devices.I2c;
  using System.Diagnostics;
  using System.Threading;
  
  
  // Die Vorlage "Leere Seite" ist unter http://go.microsoft.com/fwlink/?LinkId=402352&amp;clcid=0x409 dokumentiert.
  
  namespace _007_Test7_I2C
  {
      /// &lt;summary&gt;
      /// Eine leere Seite, die eigenständig verwendet werden kann oder auf die innerhalb eines Rahmens navigiert werden kann.
      /// &lt;/summary&gt;
  
      public sealed partial class MainPage : Page
      {
  
          private I2cDevice Device;
          private Timer periodicTimer;
  
  
          public MainPage()
          {
              this.InitializeComponent();
              initcomunica();
          }
  
          private async void initcomunica()
  
          {
  
              var settings = new I2cConnectionSettings(0x40); // Arduino address
              settings.BusSpeed = I2cBusSpeed.StandardMode;
              string aqs = I2cDevice.GetDeviceSelector("I2C1");
              var dis = await DeviceInformation.FindAllAsync(aqs);
              Device = await I2cDevice.FromIdAsync(dis[0].Id, settings);
              periodicTimer = new Timer(this.TimerCallback, null, 0, 1000); // Create a timer
  
          }
  
          private void TimerCallback(object state)
          {
  
              byte[] RegAddrBuf = new byte[] { 0x40 };
              byte[] ReadBuf = new byte[7];
  
              try
              {
                  Device.Read(ReadBuf);
               //   Debug.WriteLine(ReadBuf);
              }
              catch (Exception f)
              {
                  Debug.WriteLine(f.Message);
              }
  
  
              char[] cArray = System.Text.Encoding.UTF8.GetString(ReadBuf, 0, 7).ToCharArray();  // Converte  Byte to Char
              String c = new String(cArray);
              Debug.WriteLine(c);
              // refresh the screen, note Im using a textbock @ UI
  
              var task = this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =&gt;
              { Temp_2.Text = c; });
  
          }
  
      }
  }
  </code></pre>