Cover image
旋转 - 编码器以上MCP23017
Private

旋转 - 编码器以上MCP23017

Try Now
2021-09-11

库和示例代码可以通过MCP23017 I2C GPIO扩展器控制多个旋转编码器。

3 years

Works with Finder

12

Github Watches

18

Github Forks

116

Github Stars

Using Rotary Encoders over MCP23017(s)

Description

This is a library and example code with which one can controll multiple rotary encoders over the MCP23017 I2C GPIO expander.

The basic idea is that rotary encoders are either polled or read using interrupts.

When polling:

  • read the state of all GPIO pins
  • feed this as input to the rotary encoder library

When using interrupts:

  • activate CHANGE interrupts on all inputs
  • setup the MCP23017 to OR the INTA (interrupt on GPIO Bank A) and INTB (GPIO Bank B) together on both pins
    • thus uses only one interrupt pin is used
  • when an interrupt occurs, sets a boolean flag
    • main code will check for flag, if it set will read registers and update encoders

Used libraries

Hardware

Library usage

Include the RotaryEncOverMCP.h header in your project.

This exposes a RotaryEncOverMCP which can be constructed by using a reference to an Adafruit_MCP23017 object, its two pin numbers for encoder output A and B. Can also optionally receive an ID and a callback function which will be called when there is a clock-wise or counter-clock-wise rotation of the encoder. The callback is then called with a boolean flag (clockwise rotation or not) and its previously given ID.

class RotaryEncOverMCP {
	RotaryEncOverMCP(Adafruit_MCP23017* mcp, byte pinA, byte pinB, rotaryActionFunc actionFunc = nullptr, int id = 0);

	/* Initialize object in the MCP */
	void init();

 	/* On an interrupt, can be called with the value of the GPIOAB register (or INTCAP) */
	void feedInput(uint16_t gpioAB);

	/* Poll the encoder. Will cause an I2C transfer. */
	void poll();

	Adafruit_MCP23017* getMCP();
};

Modifications

The following modifications to the libraries were made

  • Adafruit MCP23017: Added the uint16_t readINTCAPAB(); function for reading the interrupt capture registers (state of the GPIO pins at the moment the interrupt occured)
  • Rotary: Added unsigned char process(unsigned char pin1State, unsigned char pin2State); so that it internally doesn't have to do the digitalRead itself but can be controlled from the outside

Circuits

Schematics can be found in the "Images" folder.

Using 1 encoder + 1 MCP23017 :

Using 8 encoders + 1 MCP23017:

Since MCP23017 has a 3-bit modifyable address, there can be 2^3 = 8 devices on one I2C bus (= 128 GPIO ports). With each device having 16 inputs and each encoder needing 2 inputs, this yields a maximum of 8 * 16 / 2 = 64 rotary encoders. One can use a software I2C bus and 2 free pins to further increase this number.

The example sketches are already made for such a case by specifying a list of Adafruit_MCP23017 objects. When using interrupt-based reading, you must OR all interrupt lines together to one (e.g. by using a diode-OR or a CMOS OR gate).

Credits

Written by Maximilian Gerhardt. Based on the Arduino stackexchange question by Andrew Lazarus: https://arduino.stackexchange.com/questions/52909/reading-several-rotary-encoders

License

Since this project contains the Rotary project wich under GPLv3, this project's code is under GPLv3, too.

相关推荐

  • Joshua Armstrong
  • Confidential guide on numerology and astrology, based of GG33 Public information

  • Emmet Halm
  • Converts Figma frames into front-end code for various mobile frameworks.

  • Elijah Ng Shi Yi
  • Advanced software engineer GPT that excels through nailing the basics.

  • https://maiplestudio.com
  • Find Exhibitors, Speakers and more

  • Alexandru Strujac
  • Efficient thumbnail creator for YouTube videos

  • https://zenepic.net
  • Embark on a thrilling diplomatic quest across a galaxy on the brink of war. Navigate complex politics and alien cultures to forge peace and avert catastrophe in this immersive interstellar adventure.

  • https://reddgr.com
  • Delivers concise Python code and interprets non-English comments

  • 林乔安妮
  • A fashion stylist GPT offering outfit suggestions for various scenarios.

  • 田中 楓太
  • A virtual science instructor for engaging and informative lessons.

  • pontusab
  • 光标与风浪冲浪社区,查找规则和MCP

  • av
  • 毫不费力地使用一个命令运行LLM后端,API,前端和服务。

  • 1Panel-dev
  • 🔥1Panel提供了直观的Web接口和MCP服务器,用于在Linux服务器上管理网站,文件,容器,数据库和LLMS。

  • GeyserMC
  • 与Minecraft客户端/服务器通信的库。

  • Mintplex-Labs
  • 带有内置抹布,AI代理,无代理构建器,MCP兼容性等的多合一桌面和Docker AI应用程序。

  • awslabs
  • AWS MCP服务器 - 将AWS最佳实践直接带入您的开发工作流程的专门MCP服务器

  • WangRongsheng
  • 🧑‍🚀 llm 资料总结(数据处理、模型训练、模型部署、 o1 模型、mcp 、小语言模型、视觉语言模型)|摘要世界上最好的LLM资源。

  • appcypher
  • 很棒的MCP服务器 - 模型上下文协议服务器的策划列表

  • 1Panel-dev
  • 💬MAXKB是一种现成的AI聊天机器人,它集成了检索功能增强的生成(RAG)管道,支持强大的工作流程并提供高级的MCP工具使用功能。

  • chongdashu
  • 使用模型上下文协议(MCP),启用Cursor,Windsurf和Claude Desktop等AI助手客户,以通过自然语言控制虚幻引擎。

    Reviews

    3 (1)
    Avatar
    user_aM3lQq1l
    2025-04-17

    The Rotary Encoder Over MCP23017 by maxgerhardt is an outstanding product for anyone looking to integrate rotary encoders with the MCP23017 I/O expander. It offers a comprehensive guide and seamless implementation which is perfect for both beginners and advanced users. The detailed GitHub repository is straightforward, making it easy to get started. Highly recommended for anyone working on microcontroller projects!