header source
my icon
esplo.net
ぷるぷるした直方体
Cover Image for Running I2C on Pro Micro (2) - Connecting with I2C

Running I2C on Pro Micro (2) - Connecting with I2C

about15mins to read

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 technically I²C (I-squared-C), it is commonly referred to as I2C for convenience.

Communication is possible using just two lines: the clock signal (SCL) and the data signal (SDA), connecting each IC. It seems to be a suitable standard for systems that require scalability. There is also a compatible higher version called I3C.


Source: https://www.rohm.co.jp/electronics-basics/micon/mi_what7

The IC that operates the connected devices is called the master, and the connected devices are called slaves, with 1:many processing being common.

For details on the standard: 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 circuit design.


Source: https://en.wikipedia.org/wiki/I²C

Among the I2C standards, some differ significantly. Slower communication speeds have looser constraints, but they gradually become stricter, and the Ultra-fast mode becomes unidirectional communication.

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

Connection Method

Communication is possible by simply connecting SCL and SDA. Daisy chaining, i.e., extending a cable from one IC to another, is also possible. It is important 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). Resistors are placed to meet this, but the calculation is quite difficult. Too large is not good, too small is not good either, and it also depends on the cable and devices. The maximum capacitance is 400pF, which seems to correspond to about 3-4m of (general) cable (reference). It's complicated, so I used a calculation site. Convenient.

I2C Bus Pull-up Resistor Calculation keisan.casio.jp

When calculating as a test, it turns out that estimating a large 350pF is quite close, and 1kΩ fits perfectly. If you are assuming a long cable, it seems good to place 1kΩ. Power consumption increases, so if it is short, a larger resistor is fine (calculation needed).


https://keisan.casio.jp/exec/user/1649986426

Communication Method

The master specifies the address for transmission and reception. Therefore, it is necessary to know the address of the slave device in advance. A program that scans the addresses of connected devices can also be written, so it seems good to process it nicely at the initialization timing. We will look at it 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 restricted or increased by various methods. This will also be described later.

I2C Module Used This Time

There are various I2C-compatible modules in the world, but this time we will use an IO Expander that increases pins.

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

This time, we will use the MCP23017 (190 yen at Akizuki), which adds 16 bits (16 pins). However, only 3 bits of the address can be set, and the upper 4 bits are fixed (0x20-0x27). Therefore, when connected normally, it is limited to 8 pieces.

The MCP23017 can use internal pull-up resistors on the I/O pins. It's convenient because you don't have to prepare them yourself, but since they are weak at 100kΩ, it seems common to add your own.

Parts to Prepare

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

This time, we will perform I2C communication with a 1kΩ pull-up resistor to confirm that the MCP23017 operates.

The parts used in the first session are assumed to be already available. We will outline the necessary parts just in case.

  • MCP23017 x1

  • 1kΩ resistor x2

  • Breadboard (BB-801 etc.) x1

  • If you want to separate the breadboard. If it fits in one, that's fine too.

Items Used in the First Session

Wiring

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


Source: https://cdn.sparkfun.com/datasheets/Dev/Arduino/Boards/ProMicro16MHzv1.pdf

Looking at the MCP23017 datasheet, decide which pin to insert what. Be careful not to mistake the positions of SCL and SDA. Other than that, connect the test switch to GPB0 (pin 1) of the MCP23017, connect VCC and GND, and connect #RST to VCC to complete the standard work. This time, set all addresses to low and set it to 0x20.


Achievement: Connected one I2C device

The details of the datasheet will be seen in the next article.

Program Creation (I2C Scanner Edition)

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

First, let's simply check if the connection is made. There is a convenient program called I2C Scanner for such occasions. Copy and paste it to execute.

Arduino Playground - I2cScanner playground.arduino.cc

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

I2C Scanner
Scanning...
I2C device found at address 0x20 !
done

If the device is not recognized properly, suspect whether #RESET and address x3 are properly wired.

Also, the I2C Scanner will be used frequently for debugging in the future, so it is good to keep a note of it.

Summary

It became a bit complicated, but it's very convenient to be able to communicate with various devices just by connecting two lines, SDA and SCL.

Next time, let's receive input from the connected device.

Share