# Reading and writing digital inputs/outputs for a MUX

**URL:** <https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590>\
**Category:** Programming\
**Created:** [October 5, 2018, 10:30pm UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590 "2018-10-05T22:30:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![inventori](https://avatars.discourse-cdn.com/v4/letter/i/74df32/32.png) [@inventori](https://forum.arduino.cc/u/inventori)\
**Post date:** [October 5, 2018, 10:30pm UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/1 "2018-10-05T22:30:02Z")

</div>

I'm using a 16-channel MUX (CD74HC4067) to read high/low signals from 16 hall sensors. In this prototype, only one sensor will be activated at a time. I want to light a corresponding (one of 16) LED when a sensor is activated, and turn it off only when the sensor is deactivated.

Do I need an additional MUX to send a write signal to the corresponding LEDs?

For example, the hall sensor on channel 11 is activated. I want then to also light the LED for channel 11 until the channel is deactivated.

I am using this base code as a starting point

```cpp
void loop()
{
  byte data;

  for (byte i = 0; i < 16; ++i)
  {
    // Reads from channel i and returns HIGH or LOW
    data = mux.read(i);

    Serial.print("Push button at channel ");
    Serial.print(i);
    Serial.print(" is ");
    if ( data == HIGH ) Serial.println("not pressed");
    else if ( data == LOW ) Serial.println("pressed");

      else if (data == HIGH) {
      digitalWrite(ledPin, LOW);
      
    }
    else { 
      digitalWrite(ledPin, HIGH);
    delay(2500);
  }
  Serial.println();
  
  delay(150);
  
 
  }
}

```

Ideas and/or code snippets greatly appreciated.

---

<div class="post-metadata">

**Author:** ![CrossRoads](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/crossroads/32/214033_2.png) [@CrossRoads](https://forum.arduino.cc/u/CrossRoads)\
**Post date:** [October 6, 2018, 3:10am UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/2 "2018-10-06T03:10:23Z")

</div>

You could use two shift registers, 74HC595 or TPIC6C595. Shift in a pattern corresponding to the LED you want turned on. Don't forget a current limit resistor for each LED if more than 1 can be on.

---

<div class="post-metadata">

**Author:** ![DKWatson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dkwatson/32/220535_2.png) [@DKWatson](https://forum.arduino.cc/u/DKWatson)\
**Post date:** [October 6, 2018, 3:28am UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/3 "2018-10-06T03:28:02Z")

</div>

The mux would seem a bit overkill for LEDs. How about a 16-bit shift register like the 674 or a cascaded pair of 595s?

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [October 6, 2018, 4:46am UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/4 "2018-10-06T04:46:46Z")

</div>

> DKWatson:  
> The mux would seem a bit overkill for LEDs.

Not overkill but totally the wrong component. The problem with a multiplexer to drive LEDs is that you need to keep, scanning or refreshing as it is called, the state of the LEDs because only one LED can be on at one time.

I would use a port expander MCP23017 or an MCP23S17.

---

<div class="post-metadata">

**Author:** ![DKWatson](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dkwatson/32/220535_2.png) [@DKWatson](https://forum.arduino.cc/u/DKWatson)\
**Post date:** [October 6, 2018, 6:18am UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/5 "2018-10-06T06:18:37Z")

</div>

> because only one LED can be on at one time.

If the LED reflects the channel that is open, and only one channel can be open at a time, what necessitates having more than one LED to be lit at one time?

The MCP23017 or MCP23S17 truly are overkill. Why would one implement a 16-bit bi-directional port expander and use the I2C or SPI pins to light a single LED?

---

<div class="post-metadata">

**Author:** ![inventori](https://avatars.discourse-cdn.com/v4/letter/i/74df32/32.png) [@inventori](https://forum.arduino.cc/u/inventori)\
**Post date:** [October 6, 2018, 12:49pm UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/6 "2018-10-06T12:49:10Z")

</div>

I am opening to trying to use other components. I'm just a hobbyist here, with little education on all the different components.

This product, while likely overkill, seems like it might be the easiest solution for a noob like myself to get a working version of the full project, which actually requires a way to read 31 hall sensors and then output to a single corresponding LED:

> **[Mux Shield II - DEV-11723 - SparkFun Electronics](https://www.sparkfun.com/products/11723)**
>
> Sometimes you've got way too many inputs, or outputs... or both. How are you going to get them all connected to your Arduino? The Mux Shield, that's how. T

Am I reading this correctly that I could use two rows for the 31 inputs, and then use the remain row to output to the LED? I'm assuming since I only need to light a single LED at a time, that I don't need 31 outputs, but that a single output will do.

thank you.

---

<div class="post-metadata">

**Author:** ![septillion](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@septillion](https://forum.arduino.cc/u/septillion)\
**Post date:** [October 6, 2018, 1:17pm UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/7 "2018-10-06T13:17:33Z")

</div>

Where is the single light part is coming from? If two sensors are active, don't you want to have two leds on?

And for that, there are multiple solutions. A mux is one of the more pore solutions... A shift register like a 595 is better. TPIC6C595 if you want high currents but if you just want indication leds a HC595 with resistors limiting the current to +-2mA is fine as well.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:24pm UTC](https://forum.arduino.cc/t/reading-and-writing-digital-inputs-outputs-for-a-mux/549590/8 "2021-05-05T23:24:16Z")

</div>

Closed on May 5, 2021, 11:24pm UTC


