Windows IoT Documentation

Working with I2C Sensors on Windows IoT

The Inter-Integrated Circuit (I2C) bus is a popular serial communication protocol used to connect low-speed peripheral integrated circuits such as sensors, EEPROMs, and real-time clock ICs. Windows IoT provides robust support for interacting with I2C devices.

Understanding I2C in Windows IoT

The I2C controller on your Windows IoT device exposes an I2C bus. This bus can have multiple devices connected to it, each identified by a unique address. Windows IoT uses the Windows.Devices.I2c namespace to provide access to I2C functionality.

Prerequisites

Connecting Your I2C Sensor

Typically, an I2C connection involves the following pins:

Refer to your specific sensor module's datasheet and your Windows IoT device's pinout diagram for correct connections.

Using the I2C API in C#

The core class for I2C communication is I2cDevice. Here's a basic example of how to initialize and read from an I2C device:

using Windows.Devices.I2c;
using System;
using System.Threading.Tasks;

// ...

public async Task ReadFromSensorAsync(int deviceAddress)
{
    // Initialize the I2C controller
    var i2cController = await I2cController.GetDefaultAsync();

    // Create an I2cDevice object with the specified address and settings
    // Mode.Standard is common, but consult your sensor's datasheet for specific clock speeds.
    var i2cDevice = i2cController.GetDevice(new I2cConnectionSettings(deviceAddress) {
        BusSpeed = I2cBusSpeed.StandardMode // Or FastMode, HighSpeedMode
    });

    // Example: Reading a single byte from a register (adjust for your sensor)
    byte[] readBuffer = new byte[1];
    lock (i2cDevice) // It's good practice to lock when accessing I2C devices
    {
        // Write the register address you want to read from
        // (This is often the first step before reading data)
        // Example: assuming register 0x00 is where data starts.
        i2cDevice.Write(new byte[] { 0x00 });

        // Read the data
        i2cDevice.Read(readBuffer);
    }

    // Process the readBuffer data
    Console.WriteLine($"Read value: {readBuffer[0]}");
}
            

Common I2C Operations

Important: Always consult the datasheet of your specific I2C sensor for its register map, command sequences, and required communication protocols. I2C device addresses are typically 7-bit.

Troubleshooting

Tip: Use tools like an I2C scanner program to detect connected devices and their addresses on the bus. This can be invaluable for initial setup and debugging.

Further Resources