// CD74HC4067 Multiplexer with Raspberry Pi Pico
// Arduino C++ Code
// Pin definitions
const int S0_PIN = 21; // GP21 -> S0
const int S1_PIN = 20; // GP20 -> S1
const int S2_PIN = 19; // GP19 -> S2
const int S3_PIN = 18; // GP18 -> S3
const int EN_PIN = 22; // GP22 -> EN (Enable)
const int COM_PIN = 26; // GP26 -> COM (ADC0)
void setup() {
// Initialize serial communication
Serial.begin(115200);
while (!Serial) {
; // Wait for serial port to connect
}
// Configure address pins as outputs
pinMode(S0_PIN, OUTPUT);
pinMode(S1_PIN, OUTPUT);
pinMode(S2_PIN, OUTPUT);
pinMode(S3_PIN, OUTPUT);
// Configure enable pin as output
pinMode(EN_PIN, OUTPUT);
// Configure COM pin for analog reading
pinMode(COM_PIN, INPUT);
// Initialize to safe state
disableMux();
selectChannel(0);
Serial.println("CD74HC4067 Multiplexer initialized");
Serial.println("Channels 0-3 connected to potentiometers");
delay(1000);
}
void loop() {
Serial.println("\n--- Reading all channels ---");
// Read from all 4 potentiometer channels
for (int channel = 0; channel < 4; channel++) {
float voltage = readChannel(channel);
Serial.print("Channel ");
Serial.print(channel);
Serial.print(": ");
Serial.print(voltage, 2);
Serial.println("V");
delay(100); // Small delay between readings
}
delay(2000); // Wait 2 seconds before next reading cycle
}
void enableMux() {
digitalWrite(EN_PIN, LOW); // Active LOW
delayMicroseconds(100); // Allow settling time
}
void disableMux() {
digitalWrite(EN_PIN, HIGH); // Disabled
delayMicroseconds(10); // Brief delay
}
void selectChannel(int channel) {
if (channel < 0 || channel > 15) {
Serial.println("Error: Channel must be 0-15");
return;
}
// Set address pins based on channel number (binary)
digitalWrite(S0_PIN, (channel & 1) ? HIGH : LOW); // Bit 0
digitalWrite(S1_PIN, (channel & 2) ? HIGH : LOW); // Bit 1
digitalWrite(S2_PIN, (channel & 4) ? HIGH : LOW); // Bit 2
digitalWrite(S3_PIN, (channel & 8) ? HIGH : LOW); // Bit 3
delayMicroseconds(100); // Allow channel switching time
}
float readChannel(int channel) {
// Safe reading sequence
disableMux(); // Disable first
selectChannel(channel); // Select desired channel
enableMux(); // Enable multiplexer
// Read analog value (12-bit ADC: 0-4095)
int analogValue = analogRead(COM_PIN);
// Convert to voltage (3.3V reference)
float voltage = (analogValue * 3.3) / 1023;
// Optional: disable after reading for power saving
// disableMux();
return voltage;
}
// Alternative function to read raw ADC value
int readChannelRaw(int channel) {
disableMux();
selectChannel(channel);
enableMux();
int rawValue = analogRead(COM_PIN);
return rawValue;
}
// Function to test all 16 channels (if you have more sensors)
void testAllChannels() {
Serial.println("\n--- Testing all 16 channels ---");
for (int channel = 0; channel < 16; channel++) {
float voltage = readChannel(channel);
Serial.print("Ch");
if (channel < 10) Serial.print("0"); // Pad single digits
Serial.print(channel);
Serial.print(": ");
Serial.print(voltage, 3);
Serial.print("V ");
// New line every 4 channels for better formatting
if ((channel + 1) % 4 == 0) Serial.println();
}
disableMux(); // Return to safe state
}Loading
cd74hc4067
cd74hc4067