แชร์ผ่าน

Azure Sphere – I2C

This sample demonstrates how to use I2C with Azure Sphere in a high-level application. The sample displays data from an accelerometer connected to an MT3620 development board through I2C (Inter-Integrated Circuit). Once per second the application calls the Applibs I2C APIs to retrieve the accelerometer data. It then calls Log_Debug to display the data.

By default, this sample is configured to use an external accelerometer—the ST LSM6DS3. It is not configured to use the on-board sensors found on some development boards, such as the ST LSM6DS0 on the Avnet Starter Kit. To run the sample using the Avnet MT3620 Starter Kit and the on-board ST LSM6DSO accelerometer, see Use the Avnet MT3620 Starter Kit and its on-board accelerometer.

The sample uses the following Azure Sphere libraries.

Library Purpose
eventloop Invokes handlers for timer events.
i2c Manages Inter-Integrated Circuit (I2C) interfaces.
log Displays messages in the Device Output window during debugging.

Contents

File/folder Description
app_manifest.json Application manifest file, which describes the resources.
CMakeLists.txt CMake configuration file, which Contains the project information and is required for all builds.
CMakePresets.json CMake presets file, which contains the information to configure the CMake project.
launch.vs.json JSON file that tells Visual Studio how to deploy and debug the application.
LICENSE.txt The license for this sample application.
main.c Main C source code file.
README.md This README file.
.vscode Folder containing the JSON files that configure Visual Studio Code for deploying and debugging the application.
HardwareDefinitions Folder containing the hardware definition files for various Azure Sphere boards.

Prerequisites

This sample requires the following hardware:

  • An Azure Sphere development board that supports the Sample Appliance hardware requirements.

    Note: By default, the sample targets the Reference Development Board design, which is implemented by the Seeed Studios MT3620 Development Board. To build the sample for different Azure Sphere hardware, change the value of the TARGET_HARDWARE variable in the CMakeLists.txt file. For detailed instructions, see the Hardware Definitions README file.

  • ST LSM6DS3 accelerometer

  • 2 x 10K ohm resistors

  • A breadboard (recommended because this sample requires wiring from multiple sources to the same pin and the use of pull-up resistors)

  • Jumper wires to connect the boards

Setup

  1. Ensure that your Azure Sphere device is connected to your computer, and your computer is connected to the internet.

  2. Ensure that you have Azure Sphere SDK version 24.03 or above. At the command prompt, run az sphere show-sdk-version to check. Upgrade the Azure Sphere SDK for Windows or Linux as needed.

  3. Ensure that the Azure CLI is installed. At a minimum, the Azure CLI version must be 2.45.0 or later.

  4. Install the Azure Sphere extension.

  5. Enable application development, if you have not already done so, by entering the az sphere device enable-development command in the command prompt.

  6. Clone the Azure Sphere samples repository and find the I2C_LSM6DS3_HighLevelApp sample in the I2C folder or download the zip file from the Microsoft samples browser.

  7. Set up your hardware to display output from the sample. For instructions, see Set up hardware to display output.

  8. Make the following connections between the ST LSM6DS3 and MT3620 dev boards. Make sure that power is disconnected while you wire the boards.

    Note: This sample uses ISU2 on the MT3620 board; however, you can use another ISU by adjusting the wiring, the code, and the application manifest.

    Connection diagram for ST LSM6DS3 and MT3620

Use the Avnet MT3620 Starter Kit and its on-board accelerometer

If you're using the Avnet MT3620 Starter Kit and its on-board ST LSM6DSO accelerometer instead of the ST LSM6DS3 external accelerometer, make the following changes in the sample:

  1. Open the I2C\main.c source code file:

    1. Replace 0x69 with 0x6C in the following code:

      static const uint8_t expectedWhoAmI = 0x69;
      
    2. Replace SAMPLE_LSM6DS3_I2C with SAMPLE_LSM6DSO_I2C in the following code:

      i2cFd = I2CMaster_Open(SAMPLE_LSM6DS3_I2C);
      
  2. Open the app_manifest.json file and replace $SAMPLE_LSM6DS3_I2C with $SAMPLE_LSM6DSO_I2C in the following code:

    "I2cMaster": [ "$SAMPLE_LSM6DS3_I2C" ]
    

Build and run the sample

To build and run this sample, follow the instructions in Build a sample application.

Test the sample

When you run the application, it reads the accelerometer WHO_AM_I register. The returned value (0x69 for the LSM6DS3) is compared with the application's expectedWhoAmI constant to verify that the MT3620 can successfully communicate with the accelerometer. If this fails, verify that the devices are wired correctly, and that the application opened the correct I2C interface. For details on the LSM6DS3 registers, see the ST LSM6DS3 data sheet.

After displaying the initial values, the application configures the accelerometer and then displays the vertical acceleration every second.

To test the accelerometer data:

  1. Keep the device still and observe the accelerometer output in the Output Window. It should indicate a vertical acceleration of approximately +1g. Once the data from the accelerometer CTRL3_C register is displayed, the output should repeat every second.

  2. Turn the accelerometer upside down and observe the updated data in the Output Window. The vertical acceleration should change from approximately +1g to approximately -1g.

Next steps