This is yet another episode of basic Arduino modules.
Today we will take a look at a multiplexor or so called a MUX. Actually, this could multiplex
or demultiplex, so output or input, it could work with both digital and analog signals,
so analog read, PWM signals and digital write or read. This can divide one track
into multiple paths and that could be useful. So guys, let’s get started.
What’s up my friends, welcome back. Today we will take a look at another useful module used
with Arduino. This time is not an ADC, is not a DAC or other type of sensor. This time we
will use a module called a MUX or multiplexor. This module is very useful when you want to use
more inputs or outputs than the Arduino has, let me tell you why with a real life example.
So, the Arduino UNO has 14 digital inputs or outputs and 6 analog inputs.
What if your project needs more than that. That’s exactly what happened with my new version of the
Arduino based multimeter. This is the old circuit of that project and if you haven’t seen it,
please check it out because is very interesting. These are all the pins of my microcontroller
and they are all used for different parts of the circuit as inputs or outputs.
I want to remove these two slide switches because they are too big for my PCB,
they don’t work very well and they are still controlled manually. But I want to make my
multimeter to be able to digitally change between modes and not have to slide any switch. For that,
thanks to your feedback, I want to use SSRs or solid state relays.
Basically this multimeter has two main inputs. The negative probe and the positive probe,
just as any other multimeter. But on the circuit we have a block for measuring voltage,
another for measuring resistance, one for current and so on. The input for these blocks can’t
be connected together so we can’t connect the positive probe to all the blocks at the same time.
Because the voltage part works with up to 60V but the resistance part for example works at digital
values of 5V so they can’t be connected together. The current mode, for example,
is basically a short circuit through a current measurement module so this block can’t be
connected to the voltage mode neither. That’s why I have the sliding switch. This is in charge of
switching the positive input between these modes. But it is done mechanically by sliding the switch.
I want to make it digital. Basically, I want to add a few solid state relays, one for each mode.
And to enable or disable these relays we need signals from the Arduino.
But as I’ve shown you, all my analog and digital pins are already in use, I only have 3 left. But I
need more than 5, so how to get that? Well we use a MUX IC. With a MUX module like this one with the
IC we can control 16 outputs or inputs using 4 control pins from the Arduino.
So we lose 4 pins but we get 16, so we can have 12 extra pins. Let me tell you how this works.
I connect the MUX to the Arduino and some output pins to the each SSR.
By sending a 001 I activate the first output. By sending a 010 the second one and so on. In
this way I can control 5 SSRs or more. Ok, that’s enough story for today, let’s use the MUX module.
This module has 16 inputs or outputs. But for my multimeter module I’ve used the 74 HC 40 51
which has only 8 but it works the same as this one.
First we use this in output mode and then with the input mode since it could go both ways.
For the output mode make these connections.
To test each output I’ve placed an LED and a resistor at each of the 16 pins. We have to
supply 5V and connect ground to the module at the VCC and GND pins. In order to select the output,
since we have 16 different outputs, we need 4 pins which are pins S0 to S3.
And the signal pin is used to define the value of each output, between HIGH, LOW or maybe a PWM
signal. Let me explain. The MUX can’t control the value of each output separately by itself.
It only makes a connection between the signal pin and one of the 16 outputs or inputs.
So let’s say that you want to set HIGH the output 5. You send a 0101 to those control pins
and that will make the connection between the signal pin and the sixth output. Now you place
the signal pin to high or low and that will also set the sixth output to high or low.
So the value is given by the signal pin and not by the MUX which only selects on which output
will that signal be placed. That means that you could only control one output at a time
and that's the disadvantage of this module. If you want to set pin 5 and 6 to HIGH at the same time,
well, you can’t. So, we have these connections, now let’s check the code.
We select the control pins as digital pins of the Arduino 3, 4, 5 and 6.
It could be any other 4 pins if you want. I also set the signal pin to be pin 9 for example.
In the setup loop we set all pins as outputs. Now, for example, let’s turn on output 5 of
the MUX module. For that we need to send a 0101, remember. So we set S0 to 1, S1 to 0,
S2 to 1 and S3 to 0. Now all is left to do is to set the signal pin to high and then to low . I
run this code and as you can see, the output 5 is now blinking on and off. That was easy…
If you don’t want to control each of the control pins separately,
there is an easier way to do that. We use the bit read function.
You have to know that a byte has 8 bits and the positions of the bits are marked from 0 to 7.
If I write the function bit read of number 5 in binary it will fill the byte with 0000 0101,
which is 5 in binary. If I tell the bit read function to give me the position 2 for example,
it will give out the value of the second position, which in this case is a 1.
So in the code I make this function using bit read.
This will associate S0 to the first position of the byte, S1 to the second and so on.
So if I write channel 5, it will automatically send a 0101 to the control pins. We don’t have to
do it manually every time as before. This is the new code. So I call the set mux channel function
and select channel 10 for example. I run the code and not the output 10 is turned on.
Now I run this code where I make a for loop in order to pass through all the outputs from 0 to 15
with a delay of 100 ms in between. I run the code and as you can see I can control each of the 16
outputs by first selecting the MUX output and then switching the signal pin to HIGH and then to low.
But hey, since the MUX is just a connection between the signal pin and any of the output,
we could also apply a PWM signal, right? The SIG pin is connected on D9 which is a PWM pin
of the Arduino. And instead of digital write, in the code, this time I make an analog write.
I run this code and as you can see, I could also fade in and out an LED with any of the outputs.
Ok guys, now let’s use the multiplexor for reading inputs instead of controlling outputs.
The process is the same. We use the S0 to S3 pins to multiplex one of the inputs to the signal pin.
Then we use analog read in the code. This time we need to connect the signal pin to one of the
Analog Inputs, let’s say the A0. As you can see in the code, again we select the input pin and
then we make an analog read. I run this code and as you can see I get the analog value from
a potentiometer connected on pin 6 of the MUX. And again, it could be any of the 16 pins if you want.
The same could be done with digital read.
The code is the same but we make a digital read. I add push buttons connected to the MUX.
I run this code and as you can see on the monitor, when I push the buttons I get the digital read.
So this module is a Multiplexer and a Demultiplexer.
Things to have in mind. This module works with voltages up to 7V.
This is the truth table for each output according to the S0 to S3 pins.
The ON resistance between the signal pin and the outputs is 60 to 70 ohms at 5V which is quite high
but working with digital signals is ok. Also, the propagation delay time between the switch in and
out is around 90ns and the turn ON delay could be up to 375ns, so that might limit your frequency.
So guys, that’s how you could multiplex your inputs and outputs
for both digital or analog signals. I’ve actually bought a bunch of these modules to use with a lot
of capacitive sensors for a future project. I want to be able to create a grid of capacitive sensors
and read the columns and rows and with a simple Arduino, is impossible. I need a lot of inputs and
that’s why I will use MUXes. But you could now use this to read values from LDRs for example,
from a lot of push buttons, control a lot of LEDs or other stuff, and in general,
increase the pinout of your microcontroller. Have in mind a MUX has its limitations,
the bad one being that you could only select one channel at a time. Stay tuned for my new version
of the multimeter using relays and this mux. I hope it works and I also hope you have learned
something new. If so, consider giving me a like or comment below. Thanks again and see you l guys.