Reference

Last revision 2025/12/13

This guide provides an in-depth look at the MCP23017 library, detailing its API functions for setting pin modes, writing and reading digital pins, and managing interrupts. It explains how to configure I2C addresses using DIP switches and offers supplementary information on cascade connections and schematic layouts.

Communication Protocol Description

This module communicates via IIC (I2C). The IIC address can be configured from 0x20 to 0x27 using the A0/A1/A2 DIP switches. The relationship between DIP switch settings and IIC address is as follows:

A2 A1 A0 IIC Address
0 0 0 0x20
0 0 1 0x21
0 1 0 0x22
0 1 1 0x23
1 0 0 0x24
1 0 1 0x25
1 1 0 0x26
1 1 1 0x27(Default)

API Description

 /**
   * @brief Set the pin mode to  input, output or pull-up input (internal 100KΩ pull-up resistor)
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) included in ePin_t.
   * @param mode Mode, it can be set to Input, Output, Pull-up Input (internal 100KΩ pull-up resistor)
   * @return Return 0 if the setting is successful, otherwise return non-zero. 
   */
  int pinMode(ePin_t pin, uint8_t mode);

  /**
   * @brief Write digtial pin. The pin needs to be set to output mode before writing.
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) inlcuded in ePin_t.
   * @param level High level 1 or Low level 0
   * @return Return 0 if the writing is successful, otherwise return non-zero. 
   */
  int digitalWrite(ePin_t pin, uint8_t level);

  /**
   * @brief Read digital pin. The pin needs to be set to input mode before reading. 
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) included in ePin_t.
   * @return Return High or Low
   */
  int digitalRead(ePin_t pin);

  /**
   * @brief Set a pin to interrupt mode
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) included in ePin_t.
   * @param mode Interrupt mode: all enumeration values included in eInterruptMode_t.
   * @param cb Interrupt service function, needs to be defined and transferred parameter by users. Prototype: void func(int)
   */
  void pinModeInterrupt(ePin_t pin, eInterruptMode_t mode,  MCP23017_INT_CB cb);

  /**
   * @brief Poll if an interrupt occurs on a port group.
   * @param group Port group, it could be all enumeration values included in eGPIOGroup_t,  GPIO GroupA(eGPIOA), 
   * @n GPIO GroupB(eGPIOB) GroupA+B (eGPIOALL).
   * @n When setting to eGPIOA,poll if an interrupt occurs on the port group A. 
   * @n When setting to eGPIOB, poll if an interrupt occurs on the port group B.
   * @n When setting to eGPIOALL, poll if an interrupt occurs on the port group A+B
   * @n None, poll if an interrupt occurs on the all ports of group A and B by default. 
   */
  void pollInterrupts(eGPIOGroup_t group=eGPIOALL);

  /**
   * @brief Convert pin into string description
   * @param pin Pin number, it could be all enumeration values (eGPA0-eGPB7/ 0-15) inlcuded in ePin_t.
   * @return Return pin description string
   * @n such as "eGPA0" "eGPA1" "eGPA2" "eGPA3" "eGPA4" "eGPA5" "eGPA6" "eGPA7"
   * @n   "eGPB0" "eGPB1" "eGPB2" "eGPB3" "eGPB4" "eGPB5" "eGPB6" "eGPB7"
   * @n   "eGPA" "eGPB"
   */
  String pinDescription(ePin_t pin);

  /**
   * @brief Convert pin into string description 
   * @param pin  Pin number, range 0~15
   * @return Return pin description string
   * @n such as "eGPA0" "eGPA1" "eGPA2" "eGPA3" "eGPA4" "eGPA5" "eGPA6" "eGPA7"
   * @n   "eGPB0" "eGPB1" "eGPB2" "eGPB3" "eGPB4" "eGPB5" "eGPB6" "eGPB7"
   * @n   "eGPA" "eGPB"
   */
  String pinDescription(int pin);

Other Supplementary Information

Cascade Connection

Cascade Connection 1

Cascade Conneciton 2

Was this article helpful?

TOP