Ep.54 Analog Input Multiplexing with the 74HC4051 – 8 Sensors on 1 Pin

Ep.54 Analog Input Multiplexing with the 74HC4051 – 8 Sensors on 1 Pin


What’s going on guys, vald with EEEnthusiast.com
here. In todays video, we’re going to be talking about multiplying your analog inputs with
the help of the 74HC4051N IC, which I have in my hand. This was inspired by a previous
video that I’ve made on a multiplexer which essentially allowed you to drive different
outputs. Essentially up to 16 outputs with the use of only 4 pins of your Arduino. Without
any further delay, lets take a look at how you can actually read up to 8 different analog
channels on your Arduino by utilizing only one single pin on your A0 channel. The first
step is connecting the hardware, and as you’ve often heard me say, you will need to look
up the datasheet to fully understand what each of these pins mean and how to actually
connect the IC. I highly encourage you to take a look in the description of this video
and look at the datasheet for the 74HC4051N IC which I’m using on my breadboard right
now. I’m going to be walking you through the pins, so you don’t really have to go in depth.
You will also see a screenshot of the pin configurations right now on your screen. Lets
take a look at some of the connections that we have. First and foremost, the green cables
right here, represent the S0, S1 and S2 on pins 9, 10 and 11 of the IC. This pretty much
allows you to select which analog channel you want to use from the IC itself and then
feed it back to the Arduino. They are connected currently to pin 8, 9 and 10 on the Arduino
itself. I have the channel which feeds the main, or the selected analog signal back to
the Arduino; which goes into A0 in order to read the analog value. Next, I have my power
and ground busses, so fairly simple. One thing you want to pay attention to, is that the
enable which is pin number 6 on the IC, needs to be connected to ground. As you can see,
that is labeled by a black cable. I have pretty much pins 6, 7 and 8 of the IC going to ground.
Next, all of my blue cables, that you see going to the buttons and potentiometer, are
pretty much analog input or analog channel pins. You will have as I have in my case,
7 buttons and then a potentiometer. What I’m going to be reading essentially is 7 different
signals and a potentiometer. The last one, as you can see, is jumpered through the button,
so if I want, I can use the button or the potentiometer. Obviously, the potentiometer
just acts as a voltage divider, so I should see a value of 0 to 1023 on the Arduino serial
program. You will be selecting one of these 8 channels and feeding them back to the Arduino
on pin A0. Essentially, utilizing one analog channel of the Arduino and three selector
pins in order to read up to 8 different channels. I’m going to be showing you the actual software,
but before that, I wanted to make a quick mention. You can also get these breakout boards
which would allow you to drive up to 16 channels. 16 Analog channels, same one pin on the Arduino
and then you get, obviously, one extra selector switch. I’ll post the link on my website for
this particular board, so you can check that out if you want to drive even more analog
signals. Lets take a look at the software side. Alright, so just as a quick reminder,
all of this code is going to be available on my website as well as my github page. Lets
jump straight into the code. As I’ve mentioned before, S0, S1 and S2 on the mux are mapped
to Arduino pins 8, 9 and 10. That’s what I am doing right here. The input from the mux
is mapped to A0, this is coming back to the analog pin of the Arduino. The state of the
8 mux channels mapped into this array, so mux1 state 8. It is initialized to 0. The
setup function; fairly simple. Three pins set to output. I had experimented with a digital
input as well for the mux, so that’s commented out. Serial dot begin 9600, once again this
is used for the console and we’re going to be looking at our array through this feature.
Loop function update. This is a function which will be defined below; lets take a look before
we look at the rest of the code. void update mux1, I’m going through the 8 channels. A
very simple for loop, going from 0 to 8; incrementing every single time. I’m going to write a digital
output high based on the bit that is set for the i variable. Essentially, what this is
doing, this is breaking down your decimal, 1, 2, 3, 4, 5, 6, 7, 8 into a binary write.
It writes them accordingly into pins 0, 1 and 2. Imaging it being a 000 001 010 etc.
All the way up to 8. At every stage of that setting, I’m reading the analog signal that
is being sent to pin A0. Essentially, through these lines, I am selecting the channel on
the mux and reading the value of the said channel. Obviously, storing the said value
into my mux1 state array. Lets come back to the loop function. Once I’ve updated the array
through one cycle, what I do is, go through the for loop through all the channels. 0 to
7. If i=7, what I want to do is essentially create a jump line, so this is a println Mux
state of i. Otherwise, I do a simple print on the mux1 state of the current channel and
put a comma. Lets quickly verify the program, upload it to the Arduino and I will show you
exactly what it does. You can see the useful application. Serial monitor. Right now, as
you can see, I have all my buttons and my potentiometer initialized. Lets start with
the buttons; if I press the button, it reads a signal of 1023. Obviously, if the pin is
unpressed, it reads a 0, if it is full voltage, or 5v, it’s 1023. Similar to every single
other button, all the way to 7. On the last pin, I have the potentiometer, so this divider
will go all the way from 0 to whichever value, all the way up to 1023. As you can see, I’m
reading 8 distinct analog inputs on the Arduino and only utilizing the A0 pin. You can already
imagine that you can expand this pretty much indefinitely and read all kinds of analog
signals and sensors. Flow, temperature, if you want to read just a potentiometer reading
for whatever reason. If you have 20 potentiometers, you can implement all of this very easily.
Let me know what you think. Let me know what kind of applications you would use this for.
Let me know if you want to have some kind of a modification; this IC also allows to
essentially send out a different analog signal to the 8 ports. In other words, doing an analog
out the same way as we’re doing an analog in. As always, thank you guys for watching,
make sure to subscribe, make sure to visit my web page and thank you again for watching.
Bye.

85 thoughts on “Ep.54 Analog Input Multiplexing with the 74HC4051 – 8 Sensors on 1 Pin

  1. Nice! Comes in really useful when you want to measure slower signals.
    From looking at the datasheet, the atmega328p can sample 15000 times per second at max 10 bit ADC resolution which can measure 1 x 7.5 KHz signal (good old Nyquist…should apparently sample slower than this to avoid aliasing but whatever.). Using your setup you can now instead measure 2 x 3.75 kHz signals, 3 x 2.5 KHz signals etc using only one pin!
    I would use this setup for a temperature measurement systems where you want to map the temperature at hundreds of different locations, for example to find where the heat is distributed in my apartment and find out where the heat is leaking out in winter 🙂

  2. This is very well done and super clear. The application I have experimented with that led me to your video is creating a multi-cell battery discharge tester. I would like to use the multiple analog inputs measure the voltages of a number of 18650 lithium cells (recycled from a laptop, e.g.) under load over time, all at once. The challenge with these old cells for ebike and electric vehicles is getting batteries of equal capacity. There are easy ways to measure the capacity of single cells but not many cells at once. A multiplexer solution would allow testing of the many cells needed in a shorter time frame. Thanks for the excellent video.

  3. Very nice.
    I am going to use this to put 54 thermistor into a beehive.
    I have bought 5x 12channel breakout, but after watching your video I wonder.
    Can i use 1 pin, connect to a 74HC4051 that connect to 8 74HC4051, that then each have 8 termisistor? I like to only use 1 pin on arduino 🙂

  4. Very cool explanation. You mentioned flow in terms of sensing and analog input. So if I had an array of flow sensors how would this count each pulse and increment a flow value?

  5. Thank you very much Vlad. Very nice tutorial and very nice explanation as always. Love your tutorials. Please make them coming.

  6. I'm a new comer to arduino and I need a shield to help control 4 different items with various power for each. I need to supply 10W to a pair of speakers each needing 5W and another 6W to another set of speakers each needing 3W. I also need about 2.3V for my VU meter by Great Scott with just a little more power for my lcd screen by arduino. I'm also including TDA7492P 25W+25W Wireless Bluetooth 4.0 Audio Receiver Digital Amplifier Board. So I'd like to add this features together with 2 analog inputs and 3 digital for on and off of whole system, switching from radio to Bluetooth, and one to vary the on and off of the VU meter

  7. I have a real world use for this circuit. I have about 15 strawberry planters that will freeze this winter on my balcony if I don't do something. Bringing them inside is not a good option.
    I want to measure the temp of all my planters with an LM35 and I want to use some resistors to give them just enough heat so as not to freeze. It will get down to -10'F. So if I can use perhaps 5 watts worth of 1/2 watt resistors and keep the temp to about 37'F. I should be okay.
    In summary I need to Mux 15 Analog inputs and 15 digital outputs to the tune of perhaps 5 watts.

  8. awesome job!…do you know of a way to use an arduino mega as a game controller? I didn't know enough and bought one not realizing they're not preprogrammed to emulate a controller like the teensy or the leonardo.

  9. Great job, i looking for video for combing many temperature sensors with this IC chip, can you help me to get the codes and connections.
    any help will be appreciated

  10. I was thinking in using an arduino with multiplexed pots and switches, but I'm not sure if this boards tracks quickly enough or if it can take several control signals simultaneously without going crazy or mistracking sliders or pots, or jumping around values… Is it powerful enough to handle at least 16 muxed analog inputs, and 16 digital signals? :/

  11. i am going to read 4 sensor analog values into pin A0 for web interface its should read 4 analog sensor values one after another ……i need know what's the method for this and what are the changes i need to make in Coding

  12. Can this read multiple analog inputs at the same time? (Like for example pressing two buttons at once and reading both of those values at the same time)

  13. i tried the mulplexing using your codes on the same Arduiuno, the codes seem like your but it keeps bringing error can I have your help?

  14. Sir, can this read multiple analog signals simultaneously? I'm trying to measure the current of different appliances using current sensors, but the number of analog pins are limited. Please help. Thanks 🙂

  15. It's possible to multiplexing a multiplexor?? I'm serching for the cheapest way to digitalice 577 analog inputs of LDR for a musical proyect. Thanks!

  16. I'm not clear about how ur updateMux1 function works. for ex if I = 4 in the for loop, will S0 be set to 4 00000001
    S1 -> 4 00000010 and
    S2 -> 4 00000100
    sorry I'm a newbie and desperately want to know how multiplexer works with this code. can u pls explain?

  17. hi all. new to this. can i ask if this will be better for back ground noise or worse. and if its one pin will this work on atTiny 8 pin. Thanks in advance, great vid,

  18. Very good job your friend .. !!

    I would like to send this data through the "tx" bluetooth module "hc-05" and receive in another module "hc-05" and turn on the led's on the other side. How do I get the code to receive this data on the other side ?? ?

  19. could you real time control an 8x8x8 led cube this way? I can't think of any reason why it would not work on a mega but I am probably missing something.

  20. What happens when you push multiple buttons at the same time? Do you get a reading on multiple channels or just one?(whichever button was pushed first, last, etc)

  21. Respected sir,

    Can you please tell how to connect GPIO pins with raspberry pi for 'AUVDEA B101 with audio in'. this is HDMI input board for raspberry pi. i'm stuck in my project for audio IN, if you help me with that you're equal to god for me!!
    https://auvidea.com/product/70501/

    thanks

  22. If you are using push buttons you could also tie one side of each button to ground with a different value resistor. That way each button will have a different analog read value so you can diferenciate them. And only uses one pin

  23. Can i connect three 74hc4051 ic's inputs in parallel?. Or what can i do if i want to use more than 1 multiplexer?. Because i've 24 analog inputs, and i only have these ics.

  24. i did something wrong because my pins never read 0 even with a pulldown/up resistor. my wiring is probably wrong, i'll redo it. as to functionality and/or code, all working great. thank you!

  25. Great tutorial! Thanks so much! I have used the 74HC595N before for a series of digital outputs. With limited understanding of how they actually work. 😀 For a new project a need to read a series of analog inputs (where the 74HC4051 might come in handy). And also control a series of analog outputs (or digital with PWM). I am still struggling with the difference between mulitplexers like this and shift registers. How specific is their use? Could I use another one instead? Could I use this one for analog outputs, too? Or could I just the 74HC595N I already have for this job? Would be super greatful for any hints. THANKS!!!!

  26. can i read the value indivisuali from each pin ?
    i want to use multiple analog sensor but i have only 1 analoginput (Nodemcu esp8266)…..want to extend ………………….so thanks alot .

    can u make another tutorial on above topic …………..BIG HELLP if you do.

  27. Is all 8 pins are analog (0 – 1023)???
    How can we read each pin individually ???
    Plz make a tutorial, 😌

  28. I was wondering. Can you use many resistive inputs on an arduino pin like for example for controlling the indicator function including left right and hazards on a vehicle?

  29. Hello, I am looking to use an Arduino and a 74HC4051 to make the Switch of an RGB Scart between 8 Scart Inputs and 1 Scart output. Can you guide me on how to do this?

  30. Dear respected sir, Hope you are in good health with peace and happiness.
    I am really happy for your awesome tutorials.
    Now, I am doing a simple project where I need to display 24 switches state. If the switches is ON then it will show ON and if switch is OFF then it will show OFF.

    But I am not getting how to get 24 input into Arduino.

    Please sir, I need your help.
    I will be really grateful for your support.

  31. how about reading 9 analog sensors if the 3 of them are voltage divider and the rest are acs712? as i know the voltage divider and acs712 have different codes in callibration? sorry, i'm just a beginner

  32. You cannot keep adding more sensors indefinitely. As you increase the sensor you increase the selector pins, which is basically your constraint on how many pins your micro-controller as. Just a side note there and to correct you.

  33. Late to the party here, but can you press two buttons at the same time and get a reading on two input pints at the same time? or even all the buttons+potentiometer at the same time?

  34. If i press all buttons at once (or even more buttons, lets say 50 buttons) will the arduino read them all correctly or there will be some kind of error?

  35. I have a similar-ish setup, but I try to read really fast (38 KHz) and I’ve run into the problem that when I switch the input the first three to 8 readings (depenging on the impedance, using an impedence) becomes inaccurate (High spikes when switching). The strange thing is that it’s the first three readings no matter the read speed (9kHz up to 64 kHz) so I find it hard to believe that it’s the adc which needs time for charges to settle (since then it would take a fixed amount of time, not reads). Do you have any ideas what’s going on here?

  36. hi, can i replace those switches to piezo? could your program work on this? i'm planning to extend the my analog pins on my arduino drum kit.. on my case, i'm using serial midi (virtual serial port over USB) and loop midi(loop a MIDI part independent of the project playback), and FL studio(Digital Audio Workstation). i hope you can help me. thank you 🙂

  37. Great stuff, this just took me from 2 megas down to one, with a bit more PCB work.

    Next step for me is to figure out how to read 0-120v via an analog pin, once I can do that my project will be a go.

  38. Is there a way to connect two analog devices to one analog input without having to connect either of the two through the digital pins as well? Trying to get two hall sensors (analog) connected to one analog input on arduino with MMJOY software.

  39. Good video but you don't press buttons and/or the potentiometer at the same time to show that it really is reading multiple values at the same time. Would be more impressive to do that.

  40. There is a new approach in science for reading an unlimited number of sensors in parallel. It is called the photon-pixel coupling method. Maybe you can do it with an Arduino.

  41. hi nice tutorial is it possible to read analog values and put them seprately in a variable for uploading it to a iot cloud i am asking it for nodemcu cause it have just one analog pin

  42. sir,,,, in the below link there is a device from which i want to get pulse out put regarding each input currency note… can i get it by reading the led display signal of this device into arduino

    https://youtu.be/FcOn6A0agu0

  43. nice clear tutorial! would there be any electrical implications if I use two back to back to achieve routing from multiple inputs to multiple output? like increase in resistance, a decrease in the signals amplitude or maybe a delay?

  44. Hats off to your logic. Working perfectly but spent the whole day but did not understand
    " digitalWrite(pin_Out_S2, HIGH && (i & B00000100));"

  45. hello, great video,very inspiring, …it gave me an idea.

    how can i control 8 Fotorezistors and log the lighting conditions over from each individual Fotorezistor(placed in different areas) using just arduino or just one raspberry pi ?

    basicaly i want to place 8 sensors around a small room and log the light intensity throughout a day.

    i'm thinking that neither arduino or raspberry pi has enough gpio pins…

    please help me understand how i could log data from 8 Fotorezistors at the same time placed in different areas.

Leave a Reply

Your email address will not be published. Required fields are marked *