Chip Chat #4 CD4051 Analog Switch / Multiplexer / Demultiplexer – Ec-Projects

hello and welcome to easy projects a lot of you seem to like the chip chat video so here’s another one for you and it’ll be number four today we’re going to talk about this chip it’s an analog multiplexer / D multiplexer also sometimes called an analog switch and it is in many cases very similar to this which is a standard rotary switch and to those of you who might not know such a switch it’s basically you have one common pin in the middle but you can solo wire to and when you rotate this you alternate between connecting this pin so for example this one the next time this one this one this one and so on this particular one has 12 pins and one common pin so it can make 11 clicks you can also get them with two common pins and then it will have six pins that will connect to this and six pins that will connect to the other this will switch both pins when you turn this and then you only get five clicks if we look at the texas instrument datasheet for this chip which is the CD 4051 and you can also get the 52 and the 53 the 51 is the one in the top here and you can see it works just like our rotary switch you have one common pin that can connect to these other pins in this chip there’s eight pins but instead of rotating a knob like you doing this one it’s digitally controlled of course and you can see this number here that goes from 0 0 0 to 1 1 1 selects which channel is connected to the common pin we also have a master disconnect over here so you can just disconnect the common pin from anything the 52 you just have two common pins and it switches the pair between this one this one this one or this one and the 53 has three common pins but it can only alternate between two sets of inputs the chip we will be looking at is the 51 and that’s the one I have right here I’ll also quickly mention that these tips can be used both ways so you can send the signal either from the common to the selected pin or from the selected pin to the common pin hence the name multiplexer demultiplexer some chips called multiplexers can only do it from the common pin and out to the switched one and T multiplex us only the other way or at least some of them but this can do both ways just like the the mechanical one here so what I’m going to use this chip for and I’ll show that in another project is to read the voltages of a whole lot of lithium-ion cells whether he arduino nano micro controller the microcontroller only has eight analog channels but i need to read 12 cells so i’ll have to switch them in somehow and this chip was the first that came to my mind and i thought it would make a good video explaining how it works so the reason i want to build that voltage reader is because i had two of these lithium ion batteries but one of them failed on me but i just measured the voltage across the entire battery and it turned out one of the cells were bad in the other battery and that ended up swirling because it probably over discharged that cell i mean it shouldn’t have done that if the battery was in good condition because it should rain the cells evenly but i just don’t want to risk another battery swelling and potentially catching fire so that’s why i’m building this little unit that can continuously check all the voltages to make sure that everything is okay I’ll of course be uploading all these videos when they are finished so you’ll see this unit being made and you’ll see the final project as well and now that we’re talking about microcontroller the way the ADC works and this is quite similar to this chip actually you have eight analog channels on this arduino nano but in fact there’s only one analog to digital converter so it looks something like this you have the the ADC but then you can connect to eight

different pins and then this D multiplexer will just select whatever channel you select in your code if you get a bit more advanced microcontroller they can have multiple ADCs and also multiple channels and there might be controller so you can set it up to a run continuously and sweep across the inputs and store them in memory but as far as I know the ad we know can’t do that and that’s not really the point of this video anyway I just thought I would mention it now since we’re using this like most digital chips you can also use it manually so you can instead of using your microcontroller to control which channel is being selected you can just put chocolates witches and you can select the digital value yourself but really if you’re doing that you might as well just use one of these maybe just another tip when talking about chips and when you get them the legs are spread out so you can’t put them in the breadboard and instead of going with your fingers like this and try to straighten the pins I find the easiest ways to just hold it with two fingers like that put it against the table and then just apply a little bit of pressure that will straighten all the pins evenly but actually I think we’ll use a manual switch to begin with and then a multimeter just to get the idea of how it works and to demonstrate it so let’s get rid of the Arduino and let’s start by hooking up the power rails and as we can see on the pin out here the VSS is pin eight and VDD is pin sixteen so I’ll just connect the switch up to VDD as well so that when we turn it on we get a high so that is it and then we need some pulldown resistors on the other side of some switching to keep it at zero volts when the switch is off and I’ll just use some 22 K for that it doesn’t really matter which well you as long as they are not in mega ohms and they are not so low they will throw a lot of current then we need to hook the switch up to the ABC inputs and as you can see that’s 11 10 9 and we can also see that up here that a is our least significant bit and C is the most significant actually I just noticed this tip has a B EE input as well I don’t know why I missed that before but it can actually work with negative voltages as well but it should work if we just round this to the same signal as VSS as long as we’re only using it about 0 volts there is a quite long explanation down in the end of the datasheet but but I didn’t read all of it it should do what we wanted to do so we’ll just connect those two and so see if it worked properly we will have to have a whole bunch of different input voltages and the easiest way to do that I think would be to use a resistive divider so we’ll have a positive 5 volts up here then we’ll just have some resistors and just imagine there’s eight of them and then it goes to ground then as long as these have the same value we’ll divide the voltage evenly between them so in theory we should have two and our faults in the middle here and then one point two five and three point seven five but of course we’ll have eight inputs so it won’t be the same numbers in our case so I’ll just be using 1 kilo ohm resistors for this so our first input input zero is pin 13 and we’ll connect that to 5 volts with the resistor so just like that and then we’ll go from that pin to the next one which is pin 14 also just with a 1k

resistor and then two pin 2 and then from pin tool to pin free or input to to input free and then from 3 to 4 and from 4 to 5 from 5 to 6 and then finally from 6 to 7 and then pain-free is our common pin so the pin out is a little bit weird here I don’t know why they did it this way but that’s what they did so the multimeter doesn’t like when you just connect an antenna which this basically is without any power on it but let’s try to turn on the power we are feeding it five volts and we can see we’re measuring about four point zero seven volts on the common output now and let’s just check if if that makes sense we should have zero zero zero on the control bits here which means that channel zero will be selected and that is the one furthest to the positive rail I’ll just quickly mention the input voltage here just so we have an idea we only have one digit after the decimal my god one well that’s actually pretty close so we’ll just call it 5 volts so let’s calculate if this value makes sense because it’s pretty easy if you don’t have that many resistance but so get eight outputs like we have we put in nine resistance in a configuration like this we know if we have two resistors then the voltage in the middle is we assume that there’s a seer waltz then it’s just the supply divided by 2 if we have three resistors we get 1/3 and 2/3 so if we take the 5 volts and divided by 9 resistance that gives us this drop third resistor and we multiply that by 8 resistance so that is actually not quite what we expect I don’t know why I keep forgetting things today of course we have this inh input which determines if the common is connected so anything at all and of course we need to do something about that and we have it right here I am H it disables all the channels and we can see table 1 the link doesn’t work but anyway we need to ground it and it’s pin six and of course I also need to ground the last pin with a resistor it seems like I forgot that Alton so it should go from here round and to this pin and now it looks more like it so let’s see what was all value again it was four point four four and just to make clear the resistor that I forgot was the last one here from the last input and so ground so this one I’m sorry about that I hope it’s not too confusing so now let’s go and take a look at the next input by flipping the first bit of the dip switch here oh it’s been free of the dip switch but I like to keep the least significant bit to the right that’s pretty tight and we get three point eight nine volts and if we take it off it should be exactly that the next one should be free point three volts so I’ll send this one back off and next one on and we get that three point three months next one

it’s 0.77 and that is also correct and let’s just skip ahead and select the last channel that’s 0.55 805 five nine and it should be zero point five five five repeating so that’s close enough it’s within a couple of millivolts so now you can see the chip works and it’s fairly precise it’s giving us very close to the values that we calculated the multimeter we are using can have a ten mega ohm input impedance and that’s actually important because the chip does have some resistance through it when you select a channel that’s about I think it’s up to 200 ohms between the input and the common thing no it is actually more than that if we look at the drain to source on resistance at room temperature then you can see at five volts then you can see it’s normally around 470 ohms but it can go as high as 1,000 50 ohms so that basically means if you’re going to draw one milliamp and it happens to be at 1 kilo ohm then you can drop an entire block across the chip and that will just throw off any readings that you can take so in that case you have to buffer the output before you can use it and so demonstrate that if I go and select the first channel and now I’ll just put a wire across the first resistor here so that we supply a solid 5 volts to the input pin that way we can measure the resistance rule in the chip if I didn’t do that then we would have a Grove across this resistor and all these resistors would be pulling a little bit of current through we were still calculated but it would make it much more difficult so I’ll just put a wire across the first one temporarily and you can see now we get a solid 5 volts and now if I put a 1 kilo ohm resistor in parallel with the multimeter so from the common output pin Brown then if there’s any resistance in the chip we should see the voltage drop here so let me do that and indeed there is a drop we’re down to four point one three seven votes and we can calculate what the resistance of the chip is then by using the voltage divider formula I didn’t bother rewriting it so I just solve for X you would normally know the x value and you would calculate the output voltage but but this case it’s the other way around and we get a resistance of two hundred and eight ohms so that’s actually lower than what they specify in the datasheet it doesn’t really matter for what we’re going to do that there is this resistance we just need to keep it in mind so we don’t do anything stupid now if we needed to do this we could put an op-amp buffer after the common output and then we could grow as much current as the oven portal ow and it wouldn’t interfere with the with the analog switch chip so now I’ll just remove that why I used to show out that resistor and now we’re back to the original setup now I can remove the dip switch and use and how do we know nano instead so control the which channel is being selected and then we can use that we know nano to sample all the voltages and then maybe print it so the computer or something through the serial pole so we put back the arduino nano and then the common output we connect to one of the analog channels I’ll just use channel zero then the free digital pins we connect to three digital pins on the Arduino Nano and I’ll just use from d2 to d4 so just like that and we’ll connect up some power to the Arduino of course and now we just need some code for it to work properly so I’ll just go and write that really quick and then we can take a look at it and so I’m back I just uploaded the code that I wrote on to the Arduino Nano and let’s see if it works it’s always exciting so I’ll just be recording the screen here I

hope you don’t mind and it looks like it works so I can’t remember the exact values but I think the first one should be four point four four and the last should be zero point five five but there might be a little bit of an error in the reference of the Arduino it doesn’t really matter for what I’m doing I just need a rough value of say plus minus 100 millivolts that should be that should be good enough so now let’s take a look at the code and see how I did it and it is actually very very simple I start by defining my digital pins that select which channel should be followed it to the Arduino I just call them select a B and C and I have a prototype for a function we’ll be looking at later I have an array to store the sample values and in this setup we just begin a serial connections in the computer so we can see the values we won’t be doing that in the final project but I take a look at that video when it comes out that should be interesting and I just set the pin mode for the Select outputs just to make sure and an input for easy and in the loop here we sample all the channels in this function down here we will look at that later then we go for it equal zero as long as I is less than eight we increment I needs time for the so that will run the loop eight times from zero to seven it then calculates the voltage by taking the sample value we have in values multiplying it by 5 and dividing it by 1024 the ADC will store the value between 0 & 1 5 9 23 so we need to convert it in order to see and as a voltage then we just print channel and then the number of the channel we’re currently at and then equals and the voltage that we just calculated after that after the loop finished we just print an empty line and wait a second and then we repeat and here what the sample does is that we run a loop and again we run it 8 times from 0 to 7 and the first thing in the loop is to set the pins to the according channel so we select the right one from the analog switch chip and I think the easiest way to do that is to use bitwise and for the least significant bit that would be a 1 if we end that with the channel we’ll always get the first bit and then for the second pin if we end the channel with 2 2 and binary is 0 1 and from the right so that will always give us the second bit and then for the third one we end for with the channel and for as always 0 0 1 of course that would give us the third bit so that’s how we select it and we wait 2 milliseconds and then we sample it and store the value into the values array at location channel and just to clarify how that works say the loop has been running for a while and we’re now they’re checking for channel 5 five in binary would be 1 0 1 and for the a we should enter that with a 1 so 1 in binary is just 1 and that will be any one because one here and one there that’s one for the next one we will end it with two and the two would be this number in binary and you can see they’re not both one so the result will be 0 and for the see oops sorry that should be 0 we will end it but this value and that will give us a 1 because this value match or in fact it would give us this value but since its evaluated true or false then falls as 0 and everything else is true so that’s how it works and if we just remove that you can see this gifts the same as this up here we just split it

onto the different pins so that’s about it for this video if you liked it please give it the thumbs up on YouTube and make sure to check out the video where I make the actual elysium polymer battery voltage alone or whatever we should call it anyways this was a quick look at the CD 4051 chip and I hope you liked it see you