DEV Community

puru
puru

Posted on • Edited on

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

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.

Citation: 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. 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

Communication Speed and Standards

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

Citation: https://en.wikipedia.org/wiki/I%C2%B2C

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, 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). 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). It is difficult, 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 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).

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

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 (190 yen at Akizuki), 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 etc.) x1
    • If you want to separate the breadboard. If it fits on one, that is 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 the positions of SCL/SDA.

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

While looking at the MCP23017 datasheet, 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.

Actual: Connected one I2C device

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

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

Top comments (0)