Running I2C on Pro Micro (2) - Connecting with I2C
Tags: i2c, arduino, keyboard
Published: 2024-05-25 07:09:27

---
title: Running I2C on Pro Micro (2) - Connecting with I2C
published: true
description: It seems that I2C is often used to connect multiple ICs and perform processing in custom keyboards and sensor modules. In this series, we will use the I2C port on the Pro Micro to operate an IO expander. In this second article of the series, we will look at the basics of I2C and its use on the Pro Micro.
tags: I2C, arduino, keyboard
series: Running I2C on Pro Micro
---

In this second article of the series, we will look at the basics of I2C and its use on the Pro Micro.

## Basics of I2C

Although it is actually I²C (I-squared-C), it is commonly referred to as I2C for convenience.

Using two lines, the clock signal (SCL) and the data signal (SDA), communication can be established by simply connecting each IC. It seems to be a good standard for systems that require scalability. There is also a [compatible higher version called I3C](https://eetimes.itmedia.co.jp/ee/articles/1411/17/news058.html).

<figure><img src="https://res.cloudinary.com/purucloud/image/upload/v1713003829/wp_assets/n77c6ff740c6b_1711340522518-O7SvM1kGng.png" alt=""><figcaption>Citation: https://www.rohm.co.jp/electronics-basics/micon/mi_what7</figcaption></figure>

The IC that operates the connected devices is called the master, and the connected devices are called slaves. It is common to process in a 1:many configuration.

For more details on the standard: [https://www.nxp.com/docs/ja/user-guide/UM10204.pdf](https://www.nxp.com/docs/ja/user-guide/UM10204.pdf)

### Communication Speed and Standards

The communication speed depends on which I2C standard the connected devices support and the design of the circuit.

<figure><img src="https://res.cloudinary.com/purucloud/image/upload/v1712903930/wp_assets/image.png" alt=""><figcaption>Citation: https://en.wikipedia.org/wiki/I%C2%B2C</figcaption></figure>

Among the I2C standards, there are significant differences depending on the standard. The slower the communication speed, the more relaxed the constraints, but they become stricter gradually, and in the case of Ultra-fast mode, it becomes unidirectional communication.

The Pro Micro (ATmega32U4) seems to support Fast mode at 400kbit/s and 1Mbit/s (citation needed). For custom keyboard applications, 400kbit/s is likely sufficient, so we will consider using the less restrictive Fast mode.

### Connection Method

Communication can be established by simply connecting SCL and SDA. It is also possible to [daisy chain](https://ja.wikipedia.org/wiki/%E3%83%87%E3%82%A4%E3%82%B8%E3%83%BC%E3%83%81%E3%82%A7%E3%83%BC%E3%83%B3), i.e., extending the cable from one IC to another. The important thing here is to **insert a pull-up resistor of appropriate size**.

In Fast mode, the rise time (time to change from low to high) is a maximum of `300ns` ([reference](https://www.nxp.com/docs/ja/user-guide/UM10204.pdf)). The resistor is placed to meet this requirement, but the calculation is quite difficult. In short, it should not be too large or too small. It also depends on the cable and devices. The maximum capacitance is `400pF`, which corresponds to about 3-4 meters of (typical) cable ([reference](https://vabc.hatenadiary.jp/entry/2022/09/04/173253)). It is difficult, so I used a calculation site. Convenient.

[I2C Bus Pull-up Resistor Calculation keisan.casio.jp](https://keisan.casio.jp/exec/user/1649986426)

When calculating as a test, it turns out that 1kΩ is just right for a large estimate of 350pF. If you are assuming long cables, it is good to place 1kΩ. If the cable is short, a larger resistor is fine (requires calculation).

<figure><img src="https://res.cloudinary.com/purucloud/image/upload/v1712903924/wp_assets/image-1.png" alt=""><figcaption>https://keisan.casio.jp/exec/user/1649986426</figcaption></figure>

### Communication Method

The master specifies the address for sending and receiving. Therefore, it is necessary to know the address of the slave device in advance. You can also write a program to scan the addresses of connected devices, so it is good to handle it properly during initialization. We will look at this in detail when creating the program.

According to the standard, 7 bits are used for the address. Therefore, theoretically, it supports 2^7=128 devices. However, it is limited or increased by various methods. This will be discussed later.

## I2C Module Used This Time

There are various I2C-compatible modules in the world, but this time we will use an **IO expander** to increase the number of pins.

An IO expander is an IC for expanding IO pins. The Pro Micro has about 18 general-purpose pins, but it is useful when you want more.

This time we will use the [**MCP23017**](https://akizukidenshi.com/goodsaffix/mcp23017_mcp23s17.pdf) ([190 yen at Akizuki](https://akizukidenshi.com/catalog/g/g109486/)), which can increase by 16 bits (16 pins). However, only 3 bits of the address can be set, and the upper 4 bits are fixed (0x20-0x27). Therefore, it is limited to 8 devices when connected normally.

The MCP23017 can use internal pull-up resistors for the I/O pins. It is convenient because you do not have to prepare them yourself, but since they are weak at 100kΩ, it is often necessary to add your own.

## Parts to Prepare

Let's actually try to operate it on a breadboard. First, let's connect one and see if it works. To confirm operation, we will connect a switch to an appropriate pin and see if it responds when pressed.

This time, we will use I2C communication with a 1kΩ pull-up resistor to confirm the operation of the MCP23017.

The parts used in the first session are assumed to be already available. We will briefly overview the necessary parts.

- MCP23017 x1
- 1kΩ resistors x2
- Breadboard ([BB-801](https://akizukidenshi.com/catalog/g/g105294/) etc.) x1
  - If you want to separate the breadboard. If it fits on one, that is fine too.

### Items Used in the First Session

- Breadboard ([BB-801](https://akizukidenshi.com/catalog/g/g105294/) etc.) x1
- [Pro Micro + Pin Header](https://shop.yushakobo.jp/products/3905) x1
- Reset switch x1
- [Jumper wires](https://akizukidenshi.com/catalog/g/g105159/) x many
- Cable to connect Pro Micro to PC

## Wiring

Check the pin assignment of the Pro Micro, which you will see many times, and check the positions of SCL/SDA.

<figure><img src="https://res.cloudinary.com/purucloud/image/upload/v1712903964/wp_assets/Screenshot_2024-03-26_at_15.44.44.png" alt=""><figcaption>Citation: https://cdn.sparkfun.com/datasheets/Dev/Arduino/Boards/ProMicro16MHzv1.pdf</figcaption></figure>

While looking at the [MCP23017 datasheet](https://akizukidenshi.com/goodsaffix/mcp23017_mcp23s17.pdf), decide which pin to insert what. Be careful not to mix up the positions of SCL and SDA. Also, connect a test switch to GPB0 (pin 1) of the MCP23017, connect VCC and GND, and connect #RST to VCC. This completes the standard work. This time, set all addresses to low and make it `0x20`.

<figure><img src="https://res.cloudinary.com/purucloud/image/upload/v1713879109/wp_assets/i2c_bb_sw.png" alt=""><figcaption>Actual: Connected one I2C device</figcaption></figure>

We will look at the details of the datasheet in the next article.

## Program Creation (I2C Scanner Edition)

When using I2C, check which devices are connected at the setup stage and process them accordingly.

First, let's simply check if the connection is successful. There is a convenient program called **I2C Scanner** for such occasions. Copy and paste it and try running it.

[Arduino Playground - I2cScanner playground.arduino.cc](https://playground.arduino.cc/Main/I2cScanner/)

If successful, the following display will appear on the Serial Monitor. Change the address wiring and check if the display changes.