Reference

Last revision 2026/01/15

The article is a detailed guide on the Rotary Encoder Module, covering its I2C communication protocol, address settings via DIP switches, and main API functions for encoder control, including reading module information, getting and setting encoder values, and detecting button presses.

Library

Download and install the Rotary Encoder Module Library. (About how to install the library?)

Communication Protocol Description

This module uses I2C (Inter-Integrated Circuit) communication. Key parameters:

  • Default I2C address: 0x54 (configurable via DIP switches).
  • Supported addresses: 0x54, 0x55, 0x56, 0x57.
  • SCL (Clock): Connect to A5 (Arduino UNO R3) or the corresponding I2C clock pin of your microcontroller.
  • SDA (Data): Connect to A4 (Arduino UNO R3) or the corresponding I2C data pin of your microcontroller.

I2C Address Setting:
There are two address switches 1 and 2 on the back of the rotary encoder. Set the address switches according to the following table to obtain four different I2C addresses.

1 2 ADDR
0 0 0x54
0 1 0x55
1 0 0x56
1 1 0x57

API Description

Main API Functions

 /* @brief Read module basic information
  * @param pbuf Address to store the readings
  *             The first element: Module PID
  *             The second element: Module VID
  *             The third element: Firmware version 
  *             The fourth element: Module communication address 
  */
  void readBasicInfo(uint16_t* pbuf);
  /**
  * @brief Get encoder current count value
  * @return Return value: 0-1023
  */
  uint16_t getEncoderValue(void);
  /**
  * @brief Set encoder count value 
  * @param value range[0, 1023], the setting is invalid when out of range
  */
  void setEncoderValue(uint16_t value);
  /**
  * @brief Get encoder current gain coefficient, accuracy value for rotating one detent. 
  * @n Accuracy range:1~51, max is 1(light up one LED about every 2.5 turns), max is 51(light up one LED every detent)
  * @return Return value: 1-51
  */
  uint8_t getGainCoefficient(void);
  /**
  * @brief Set encoder gain coefficient, accuracy value to rotate one detent 
  * @n Accuracy range: 1~51, max is 1(light up one LED about every 2.5 turns), max is 51(light up one LED every detent)
  * @param gainValue Range[1, 51], the setting is invalid when out of range
  */
  void setGainCoefficient(uint8_t gainValue);
  /**
  * @brief Detect if the button is pressed 
  * @return Return true if pressed, otherwise, return false
  */
  bool detectButtonDown(void);

Was this article helpful?

TOP