SAM D21 SERCOM I²C Slave Example Project

Last modified by Microchip on 2023/11/09 09:09


This page provides a code example that configures SERCOM2 as an I²C slave and, using a RAM register set, enables you to change LED blinking patterns or read the switch state.

This example targets the SAM D21 family of devices. The application is designed to work using the SAM D21 Xplained PRO Evaluation Kit (ATSAMD21-XPRO) which contains the ATSAMD21J18A Arm® Cortex®-M0+ MCU.

SAM D21 Xplained pro evaluation kit

This application will use:

  • SERCOM2 in I²C Slave mode,
  • Pins PA08 (SDA) and PA09 (SCL),
  • Pin PB30 (User LED - LED0), and
  • Pin PA15 (User Switch - SW0).

To run this demo, an I²C host must be present to issue the commands necessary for the communication. In this demo, we use the MCP2221 USB-I2C/UART Breakout Module (ADM00559), which can be conveniently connected to a PC where commands can be issued using the provided I²C terminal application.

mcp2221 breakout jumpered

This code example uses a register-direct C-coding style (i.e., no software framework) and is built using the Arm GCC compiler toolchain which is installed along with the Atmel® Studio 7 IDE.

Visit the following page to learn how to configure the SERCOM I²C peripheral registers for this application:

SERCOM I²C Slave Configuration
Learn more >

Back to Top


Hardware Tools

SAM D21 Xplained Pro
Evaluation Kit

MCP2221 Breakout Module

Software Tools

Atmel Studio
Integrated Development Environment

MCP2221 I2C/SMBus Terminal
Terminal Program

MCP2221 I²C Terminal Application

This PC application simulates an I²C Master device and interacts with our I²C Slave application using the MCP2221 Breakout Module. Click on the collapsible link below to install this tool on Windows®:

Download and extract the MCP2221 Windows Driver Package

Run McphCdcDriverInstallation.exe to install the USB drivers for the board. It is located in the \MCP2221 Windows Driver 2014-10-09\Driver Installation Tool\x86 sub-folder.

Plug in the MCP2221 Breakout Board. It should enumerate as a composite class device (HID + CDC) as shown:

MCP2221 Device manager

Download and Extract the MCP2221 I2C Terminal Installer Package

Run the MCP2221 I²C Terminal Installer application MCP2221Terminal-v2.0.0-windows-installer.exe. The terminal application will be installed in C:\Program Files (x86)\Microchip

Exercise Files

Installation Instructions

Example Project

We recommend extracting the ZIP file to your C:\ folder.

You should see the folder C:\MTT\32arm\samd21\code-examples-gcc\sercom\i2c-slave-demo containing the solution i2c_slave_demo.atsln.

MCP2221 Commands File

In the target folder you should also find an MCP2221 Commands File MCP2221 - CMDs For I2C Slave Demo.csv, which will be used with the I²C Terminal application:

I2C slave demo commands file

Back to Top

Connection Diagram

SAM D21 Xplained Pro contains an Embedded Debugger (EDBG) that can be used to program and debug the ATSAMD21J18A using the Serial Wire Debug (SWD) interface. The EDBG also includes a Virtual Com port interface over UART, a Data Gateway Interface (DGI) over SPI and TWI, and it monitors four of the SAM D21 GPIOs. Atmel Studio 7 is used as a front-end for the EDBG.

The LED0 is driven by this application and is connected to port PB30, while SW0 is connected to PA15 and requires the internal pull-up for that pin enabled to function properly. The I²C pins SDA and SCL are connected to pins PA08 and PA09 respectively.

I2C slave demo connection diagram

​The IC SDA and SCL signal pull-up resistors are mounted on the MCP2221 breakout board.

The MCP2221 board signals may be connected to the SAM D21 Xplained Pro board header pins as shown here using male-female jumper cables:

I2C slave demo connection picture

Back to Top


Attach the SAM D21 Xplained PRO board to your computer using a USB A-to-MicroB cable. Attach the MCP2221 breakout board to your computer using a USB A-to-MiniB cable. Connect the MCP2221 breakout board pins to the Xplained PRO pins as shown above.

If the SAMD21 Xplained PRO board has been successfully enumerated, you should see the board image come up in Atmel Studio as shown:

SAMD21 Xplained pro enumeration success

The board is identified by the last four digits of its serial number (see the sticker on the bottom of the board). In this example, the last four digits are 1344.

Open the Solution

Select File > Open > Project/Solution…

Open the Project from the file drop down

Navigate to the solution folder and select the i2c_slave_demo.atsln solution file:

Navigate to the solution folder

Configure the Debugger

Next, you need to configure the debugger in Atmel Studio to discover and connect to the target EDBG IC on your Xplained Pro board.

First, navigate to Project > Properties as shown:

Open the project properties

Next, under the project's 'Tool' settings, select your EDBG target from the pull-down. Select 'SWD' as the interface:

Select EDBG from the pulldown and then select SWD for the interface

Save the tool setting by clicking on the Save All button:

Save the tool settings

Rebuild/Program the Target

Finally, click on the Start Without Debugging icon in Atmel Studio which re-builds the HEX file from the project source code, downloads/programs the HEX file onto the target MCU, and releases the target MCU Reset pin, allowing the program to execute.

Click on Start with Debugging icon from the ribbon

If prompted, upgrade the EDBG firmware on the board:

Upgrade the EDBG firmware by clicking upgrade

You need to click on Start Without Debugging again after an EDBG firmware upgrade in order to rebuild/program the target.

After the programming is complete, you should see the Amber LED LED0 on the SAM D21 Xplained PRO board toggle at a 1 second rate:

The Amber LED toggles at a rate of 1 second

Open MCP2221 I²C/SMBus Terminal Application

Open the MCP2221 I²C/SMBus Terminal Application. If the MCP2221 board is recognized, you should see the 'Select Device' field populated with the component ID on the far upper-right corner of the window:

MCP2221 terminal select device view

If there is no device ID on that field, please refer to the MCP2221 terminal installation instructions to install the board.

Import Commands

Now, select File > Import Commands. Navigate to the firmware solution folder and select the file MCP2221 - CMDs For I2C Slave Demo.csv:

Navigate to the firmware solution folder

You should now have the following commands listed under the Command list:

mcp2221 terminal commands list

Issue Read Regs Command to the I²C Slave - Step 1

After the Terminal is configured, issue the first command on the list by pressing the button Send on the right side. This command accesses the I²C device address 0x20, which is the Slave address selected for this example, and write 0x00 to the register offset byte, initializing the register pointer for the memory map into 0x00. This is the first step required before issuing the Read Regs command:

mcp2221 terminal first step for read command

The Received/Send Data sub-window on the bottom should show the following result:

mcp2221 terminal first step for read command response good

If you get the following error message, it is likely that your I²C connections between the MCP2221 Breakout Board and XplainedPRO board are incorrect. Go back to the "Connection Diagram" section and review your connection.

mcp2221 terminal first step for read command response error

Issue Read Regs Command to the I²C Slave - Step 2

After successfully issuing the command in step 6, run the command Read Regs, which is the next command below. This reads two bytes from the I²C Slave, the result should be the following:

mcp2221 terminal second step for read command response good

The result shows that the first byte equals 0x01, which is the current LED blink pattern configuration. The second byte is 0x00, meaning the switch on the board is not pressed. If you press (and hold) the switch, the second byte should be 0x01 and this is the expected output on the terminal:

mcp2221 terminal second step for read command response detail

Always run the first command (step 6) before reading the two bytes (step 7).

Issue LED Pattern Behavior Commands

In order to control the LED0 blink pattern behavior, you can choose between three options:

  • To disable the LED0 blink pattern and have it solid OFF, run the following command:

mcp2221 terminal led blink off command

  • To enable LED0 toggling at a 1 second period, run the following command:

mcp2221 terminal led toggle 1s command

  • To enable LED0 toggling at a 100 ms period, run the following command:

mcp2221 terminal led toggle 100ms command

Always be sure that each command issued has a success output on the Received/Sent Data window as shown:

mcp2221 terminal led toggle command success window

Back to Top


This example project demonstrated how to initialize the SAM D21 SERCOM l2C peripheral in Slave mode, as well as how to handle the interrupts generated by the different sources.

Back to Top