const int MUX_S0 = 33; // Multiplexer control pin S0
const int MUX_S1 = 32; // Multiplexer control pin S1
const int MUX_S2 = 35; // Multiplexer control pin S2
const int MUX_S3 = 34; // Multiplexer control pin S3
const int MUX_SIG_PIN = 15; // Multiplexer signal pin connected to ESP32 analog input
const int NUM_POTENTIOMETERS = 5; // Number of potentiometers connected
void setup() {
// Set up the multiplexer control pins as outputs
pinMode(MUX_S0, OUTPUT);
pinMode(MUX_S1, OUTPUT);
pinMode(MUX_S2, OUTPUT);
pinMode(MUX_S3, OUTPUT);
// Initialize the Serial Monitor
Serial.begin(115200);
}
void loop() {
// Loop through the number of potentiometers
for (int pot = 0; pot < NUM_POTENTIOMETERS; pot++) {
// Set the multiplexer control pins to select the correct channel
setMuxControlPins(pot);
// Read the analog value from the selected channel
int potValue = analogRead(MUX_SIG_PIN);
// Print the value to the Serial Monitor
Serial.print("Potentiometer ");
Serial.print(pot);
Serial.print(": ");
Serial.println(potValue);
// Delay before reading the next potentiometer
delay(500);
}
}
// Function to set the multiplexer control pins
void setMuxControlPins(int channel) {
digitalWrite(MUX_S0, bitRead(channel, 0));
digitalWrite(MUX_S1, bitRead(channel, 1));
digitalWrite(MUX_S2, bitRead(channel, 2));
digitalWrite(MUX_S3, bitRead(channel, 3));
}