Arduino Modulino Knob Tutorial

What is the Arduino Modulino Knob module?

The Arduino Modulino Knob is a demonstration board featuring a quadrature encoder with integral push switch. The Knob module (Arduino Part number ABX00107) was originally offered as part of the Arduino Plug and Make kit. It features two Qwiic connectors, which allow daisy-chain connections with the Arduino UNO R4 WiFi microcontroller as well as to other Modulino modules. This system provides a convenient breadboard-free method for students to explore common microcontroller-related components. The Knob Modulino features a low-cost ARM Cortex M0 microcontroller to handle the I2C communication and read the Bourns brand encoder.

This engineering brief introduces the Modulino Knob module and supporting hardware. The material is useful for students who wish to learn more about the underlying hardware and communication protocols.

Figure 1: Image of the Arduino Modulino Knob module. The surface-mount Bourns quadrature encoder is located in the center of the board. The tiny 32-bit STM ARM microcontroller is visible in the lower-left corner.

Tech Tip: The rotary encoder includes three mechanical switches – two for the quadrature signals and one for the push switch. Careful review of the Arduino datasheet reveals a lack of pull-up or pull-down resistors. To reduce the Bill of Materials (BOM), the engineers utilize the STM microcontroller’s internal pull-up resistors.

What is a quadrature encoder?

The Modulino Knob is based on the Bourns PEC11J-9215F-S0015 quadrature encoder.
The term quadrature describes the relationship between two signals. Each signal is separated by a 90-degree phase shift. This relationship may be visualized using the mathematical relationship between the sine and cosine waveforms. For example, COS(0) = 1 and SIN(0) = 0. The phase relationship is preserved, as 90° later, we see COS(90°) = 0 and SIN(90°) = 1. This quadrature phase relationship is suggested by the A signal and B signal outputs as shown in the left panel of Figure 2. For clarity, you can superimpose the quadrature SIN and COS waveforms over the square wave.

Figure 2: Description of the switch operation for the Bourns PEC11J series of quadrature encoders from the datasheet.

For additional quadrature encoder information please refer to these articles. The first is particularly useful as it explains the operation using the large example as shown in Figure 3. While the physical implementation is very different this mechanism shares the fundamental principles of operation with the Knob’s quadrature encoder.

Figure 3: Close up image showing the relationship between the quadrature sensors, interrupter disk, and reflector. Note that the interrupter disk is sized to either allow or to block both sensors.

Tech Tip: The quadrature encoder is classified as an incremental encoder. Position is maintained in microcontroller memory in response to observing the encoder’s A and B outputs. In fact, the position is unknown upon microcontroller startup. With reference to Figure 3, the A and B sensors cannot differentiate one blade from another. On startup, the microcontroller assumes the encoder is at a zero position.

Close inspection of Figure 3 reveals a hex nut in the upper right-side blade. The captured screw is detected by an Omron proximity sensor which is just visible to the right of the DC motor. When equipped with this reference sensor, the system can determine the absolute position of the mechanism.

On a related note, the knob in Figure 1 is marked with a black indicator line which allows the user to quickly identify the physical orientation. This may be confusing for the novice as the marking bears no relationship to the actual count maintained in the Modulino’s microcontroller. Remember that the encoder is initialized to a zero position upon startup; wherever the knob points at startup is the zero position. Consequently, the black line is meaningless unless you include routines for the user to “home” the knob to a known position.

Modulino Knob software

The software for the Arduino Modulino module is relatively simple. Like all the Modulino modules, the code is built upon object-oriented principles. This requires the use of a constructor and dotted notation to access the methods. The three methods are:

  • get( ); Return an integer value corresponding to the user rotation of the knob.

  • set( ); Overrides the Knob module’s position. This is especially useful for establishing an upper and lower limit for the encoder value.

  • isPressed( ); Returns a true when pressed bool indicating that the knob’s push switch is active.

Example code

Suppose we wanted the Modulino Knob to return a value bounded between -100 and 100. This is easily accomplished using this example code with the results shown in Figure 4. We see that the encoder’s value is rewritten using the set( ) method when the upper and lower bounds are encountered. This code facilitates a user reset of the value using the rotary encoder’s push switch.

#include "Modulino.h"
ModulinoKnob encoder;

void setup( ) {
  Serial.begin(19200);
  Modulino.begin();
  encoder.begin();
}

void loop( ) {
  int value = encoder.get();

  if (value > 100) {          // Set limits
    encoder.set(100);
  } else if (value < -100) {
    encoder.set(-100);
  }

  if (encoder.isPressed( )){  // Reset to zero when switch is pressed
    encoder.set(0);
  }

  value = encoder.get( );     // Retrieve the latest value
  Serial.println(value);
  delay(500);
}

Tech Tip: The performance of this example code is degraded by the blocking delay(500) function. The user is forced to push the button for up to half a second before the value is reset. This may or may not be a problem for your application. To construct a responsive system, you may be interested in the tutorial embedded into the beginning of this article. It describes the problem with blocking code and the traditional Arduino solutions. It then presents an elegant solution inspired by the Structured Text language commonly used for Programmable Logic Controllers.

Figure 4: Graph of the encoder output bounded between -100 and 100. The squiggly line represent adjustment to the encoder.

Communication protocol

The Modulino Knob uses an I2C protocol similar to the one describe in the Modulino Buzzer Tutorial. There appears to be two unique frames. One frame serves double duty by retrieving the count plus switch position. The other frame commands the Modulino knob to reset to the user selected value.

I leave it to you to reverse engineer the frame. The technique is similar to that used in the Modulino Buzzer tutorial.

Parting thoughts

The Modulino Knob is a straightforward evaluation board for the Bourns PEC11J rotary encoder. The class-based programming is easy to implement. It uses getters and setters to retrieve and manipulate the value held in the Modulino Knob’s ARM microcontroller.

We kept this document short as the quadrature encoder materials were explored in related documents (see links below). You are encouraged to purchase and experiment with quadrature encoders, such as the motor-mounted device as shown in Figure 5.

Master the quadrature encoder, and the unseen world of microcontrollers opens to you.

Please leave your comments and questions in the space below.

Best wishes,

APDahlen

Figure 5: Close up of the quadrature encoder installed on a Pololu #4754 DC motor.

Related information

Please follow these links to related and useful information:

About this author

Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (partially interwoven with military experience). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET-accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing educational articles about electronics and automation.

Highlighted experience

Dahlen is an active contributor to DigiKey’s Technical Forum. At the time of this writing, he has created over 190 unique posts and provided an additional 600 forum posts. Dahlen shares his insights on a wide variety of topics including microcontrollers, FPGA programming in Verilog, and a large body of work on industrial controls.