Multiplexers

Multiplexing rotary encoders

This sketch demonstrates the implementation of a multiplexer with rotary encoders. You can hook up a multiplexer with a maximum of 16 channels.

NOTE: Make sure to check the datasheet of your multiplexer to determine if the default 1 microsecond switching interval is sufficient. For example, a Sparkfun CD74HC4067 running at 5 volts will probably operate fine at a switching speed of 1 microsecond. However, if you run it at 3.3 volts, it will need a switching interval of around 2 microseconds or higher. You can set this with: mux.setSwitchInterval(2). There are, however, more factors that determine how responsive your MUX is, such as signal integrity and power supply noise. Always add some decoupling capacitors to your MUX power supply and at any other place where noise might be generated.


The code

#include <CtrlMux.h>
#include <CtrlEnc.h>

/*
  Create a multiplexer and provide the following parameters:
  - signal pin.
  - s0 - s3: the channel select pins (s3 is optional).
*/
CtrlMux mux(1, 2, 3, 4, 5);

// Define an onTurnLeft handler for encoder 1.
void onTurnLeft1() {
    Serial.println("Rotary encoder 1 turn left");
}

// Define an onTurnRight handler for encoder 1.
void onTurnRight1() {
    Serial.println("Rotary encoder 1 turn right");
}

// Define an onTurnLeft handler for encoder 2.
void onTurnLeft2() {
    Serial.println("Rotary encoder 2 turn left");
}

// Define an onTurnRight handler for encoder 2.
void onTurnRight2() {
    Serial.println("Rotary encoder 2 turn right");
}

// Create two rotary encoders with the following parameters:
// clk signal pin & dt signal pin (both correspond to the attached channels on the multiplexer),
// onTurnLeft (optional) & onTurnRight (optional) handler,
// & a reference (&mux) to the multiplexer.
CtrlEnc encoder1(0, 1, onTurnLeft1, onTurnRight1, &mux);
CtrlEnc encoder2(2, 3, onTurnLeft2, onTurnRight2, &mux);

void setup() {
    Serial.begin(9600);
    mux.setSwitchInterval(2); // In microseconds.
}

void loop() {
    // The process methods will poll the encoder objects and handle all their functionality.
    mux.process();
}
Previous
Multiplexing potentiometers