2.DigitalReadSerial
This is an example of the AnalogMultiplex class. It prints the states of all 16 switches connected to a multiplexers to the serial monitor.
- Boards: 🛈
- AVR, AVR USB, Nano Every, Nano 33 IoT, Nano 33 BLE, UNO R4, Pi Pico, Due, Teensy 3.x, ESP8266, ESP32
Connections
Optionally you can connect the enable pin as well, this is useful if you want to use multiple multiplexers with the same address lines and the same analog input. Otherwise, just connect the enable pin to ground.
If you are using a 3-bit multiplexer, like the CD74HC4051, you can uncomment the code specific to this multiplexer, and use only three address pins.
Connect a switch or push button between each input pin of the multiplexer and ground. A pull-up resistor is not necessary, because we'll use the internal one.
Behavior
Open the serial monitor (CTRL+SHIFT+M) or the serial plotter (CTRL+SHIFT+L), and press some buttons, you should see all 16 signals printed or plotted.
Written by Pieter P, 2019-08-08
https://github.com/tttapa/Arduino-Helpers
2,
{3, 4, 5, 6},
};
void setup() {
Serial.begin(115200);
}
void loop() {
for (
pin_int_t pin = 0; pin < mux.getLength(); ++pin) {
Serial.print('\t');
}
Serial.println();
}
void loop2() {
for (
pin_t pin : mux.pins()) {
Serial.print('\t');
}
Serial.println();
}
constexpr PinMode_t INPUT_PULLUP
Dummy header file for Arduino builder.
PinStatus_t digitalRead(pin_int_t pin) override
Read the digital state of the given input.
void begin() override
Initialize the multiplexer: set the pin mode of the address pins and the enable pin to output mode.
void pinMode(pin_int_t pin, PinMode_t mode) override
Set the pin mode of the analog input pin.
AnalogMultiplex< 4 > CD74HC4067
An alias for AnalogMultiplex<4> to use with CD74HC4067 analog multiplexers.
PinStatus_t digitalRead(pin_t pin)
An ExtIO version of the Arduino function.
uint_fast16_t pin_int_t
Integer type used internally to store the index of (extended) GPIO pins.
Type for storing pin numbers of Extended Input/Output elements.