Arduino library 路 Input

CD74HC4067: sixteen analog inputs on one Arduino pin

The CD74HC4067 is a 16-channel analog multiplexer: a rotary switch made of transistors. Four select lines, S0 to S3, choose which of C0 to C15 is connected to the common SIG pin, and the connection works both ways, so the same chip can read sixteen potentiometers into one analogRead() pin or route one output to sixteen places.

Built in on Mokxi: #include <CD74HC4067.h> and it builds, with nothing to install. Free, and no account needed.

What CD74HC4067 does

The CD74HC4067 library by waspinator is small: the constructor takes the four select pins and makes them outputs, and channel(n) sets them to n鈥檚 binary value, S0 being the lowest bit. Your code then reads the SIG pin with analogRead() as if the selected sensor were wired to it directly.

Two wiring details matter. EN is active low, so tie it to GND or hold it low, or every channel stays disconnected. And the switch has about 70 ohms of resistance plus some capacitance, so after changing channel a high-impedance source needs a moment to settle before the reading is right.

Library Manager nameCD74HC4067
AuthorPatrick Wasp
Include#include <CD74HC4067.h>
BoardsArduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, ATtiny85, Raspberry Pi Pico, Raspberry Pi Pico W, Seeed XIAO SAMD21, STM32, STM32 Nucleo-F411RE, Arduino Uno R4, ESP32 DevKit, ESP32-S3-DevKitC-1, ESP8266 NodeMCU, Arduino Pro Mini 3.3 V, Seeed XIAO ESP32-C3, Seeed XIAO ESP32-C6, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Arduino Nano RP2040 Connect, Seeed XIAO RP2040, Waveshare RP2040-Zero, Arduino Uno R4 WiFi
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesCD74HC4067 analog multiplexer

Install it

It builds on Arduino Uno, Arduino Nano, Arduino Mega 2560, Arduino Leonardo, SparkFun Pro Micro, Arduino Micro, ATtiny85, Raspberry Pi Pico, Raspberry Pi Pico W, Seeed XIAO SAMD21, STM32, STM32 Nucleo-F411RE, Arduino Uno R4, ESP32 DevKit, ESP32-S3-DevKitC-1, ESP8266 NodeMCU, Arduino Pro Mini 3.3 V, Seeed XIAO ESP32-C3, Seeed XIAO ESP32-C6, ESP32-DevKitC V4, LOLIN32 Lite, Wemos D1 R32, Cheap Yellow Display, TTGO T-Display, Seeed XIAO ESP32-S3, Arduino Nano ESP32, Arduino Nano RP2040 Connect, Seeed XIAO RP2040, Waveshare RP2040-Zero, Arduino Uno R4 WiFi.

In Mokxi it is built in: #include <CD74HC4067.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "CD74HC4067" by Patrick Wasp from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add CD74HC4067 to lib_deps in PlatformIO. Mokxi follows the calls of release 1.0.2, so a sketch written here builds there too.

Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ATtiny85 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

CD74HC4067 my_mux(4, 5, 6, 7)

Creates the mux with S0 to S3 on pins 4 to 7 and makes them outputs.

my_mux.channel(5)

Connects C5 to SIG by setting the select pins to 0101.

analogRead(A0)

With SIG wired to A0, reads whichever channel is selected. This is the Arduino鈥檚 own call; the library only switches.

for (int i = 0; i < 16; i++) {
  my_mux.channel(i);
  delayMicroseconds(50);
  Serial.println(analogRead(A0));
}

digitalRead(SIG_PIN)

Reads a button on the selected channel instead of an analog value, for sixteen buttons on one digital pin.

Example: Three inputs read through a CD74HC4067

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include <CD74HC4067.h>

CD74HC4067 my_mux(2, 3, 4, 5);  // S0, S1, S2, S3
const int g_common_output = A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  for (int i = 0; i < 3; i++) {
    my_mux.channel(i);
    delay(5);
    Serial.print("channel ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(analogRead(g_common_output));
  }
  delay(1000);
}

Wiring

  • Arduino Uno R3 2 to CD74HC4067 analog multiplexer S0
  • Arduino Uno R3 3 to CD74HC4067 analog multiplexer S1
  • Arduino Uno R3 4 to CD74HC4067 analog multiplexer S2
  • Arduino Uno R3 5 to CD74HC4067 analog multiplexer S3
  • Arduino Uno R3 A0 to CD74HC4067 analog multiplexer COM
  • Arduino Uno R3 GND to CD74HC4067 analog multiplexer EN
  • Arduino Uno R3 5V to CD74HC4067 analog multiplexer I0
  • Arduino Uno R3 GND to CD74HC4067 analog multiplexer I1
  • Arduino Uno R3 3V3 to CD74HC4067 analog multiplexer I2
  • Arduino Uno R3 5V to CD74HC4067 analog multiplexer VCC
  • Arduino Uno R3 GND to CD74HC4067 analog multiplexer GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Every channel reads the same value, or random noise

EN is floating or high, which disconnects all channels. Tie EN to GND. Then check that each sensor shares the mux鈥檚 ground and that S0 to S3 really go to the pins in the constructor.

Each reading looks like the previous channel鈥檚

The ADC鈥檚 sample capacitor still holds the last channel鈥檚 voltage. Wait a few tens of microseconds after channel(), or read twice and keep the second value, especially with sources above 10 k惟.

Channels come out in the wrong order

The select pins are swapped: S0 must be the lowest bit. Match the constructor鈥檚 order, S0, S1, S2, S3, to the wires, or test by connecting one known voltage and stepping through the channels.

Try CD74HC4067 without installing anything

Open the example, change the sketch, and keep your version in a free account.