Multiplexing Arduino Inputs: 16 Analogue Sensors on 2 Analogue Pins!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is the first part of a short  two-part series showing how to read a full   16 multiplexed potentiometer inputs into a max  patch via arduino this is going to be the basis   of a sequencer project that i am designing that  uses machine learning to vary the pitch sequence   but for now i just want to explain how to grab  that potentiometer data this video is going to be   about getting the inputs into arduino and onto the  serial monitor this circuit here uses two cd4051   multiplexer chips which allow eight analog sensors  to be read on a single analog pin on the arduino   this means that you can have way more knobs and  buttons to play with but the downside being that   the signal is read slightly less often if you'd  like to see a longer write-up of putting this   circuit together it and the basic circuit diagram  are available on my website in the description   on the datasheet here you can see  that it has a common input output pin this pin connects to the analog pin on the arduino  and um sends all the data from the in-out channels   here these ones which are all  connected to the potentiometers   a b and c are digital inputs for binary code which  are connected to pins 8 9 and 10 on the arduino   in the datasheet there's also something  called a truth table which is what shows   how these pins work the first pin is the  inhibit which is going over to digital pin   seven on the arduino and it stops any data coming  through if it's set to one c b and a which are   these three from before allow the arduino to send  a code which tells the multiplexer which channel   to read from and i'll again i'll go over that more  when we get into the code so to start the code   the first thing we want to do is create our  constant integers we just talked about pin seven   so that's the inhibit pin on the multiplexer  so we're gonna go const int inhibit mux inhibit multiplexer equals pin seven that's  digital pin seven um we're also gonna need   a pots which is this top row of potentiometers  and those are connected to the multiplexer that   is connected to a0 so we want a0 and  the same for second row potentiometers   they're sent to pin one that's that for now  i'm going to go into setup as we're going to   be needing to send things to the serial monitor  we're going to need to set up a serial begin   object um not object i'm going to use um  serial 9600 pin mode for the inhibit pin we're just going to set to output  and we also want to write to that low so that it's not interfering with anything  to start with as you can see in this truth table   it's zero that goes with any of these channels  here and if it's one or anything else no channel   so if that's high we're gonna be getting some  issues so this is the part where the code starts   to look a bit unusual this part of the code is  taken from another tutorial by boby bob i think   which i will link in the references in the  description or somewhere and it is using direct   port access so that it can change all of the port  outputs at the same time first i want to set the   ddrb which is the port b data direction register  so this allows me to set the pins on port b   as either inputs or outputs so port b is six  digital pins eight to 13 plus two pins on the   crystal pin in the arduino and this is what  this string of eight digits is gonna be here   so there's one two three four five six seven  eight so these first two are on the crystal pin   these are eight to thirteen and they're reversed  so this is eight nine ten which links up   to pins eight nine and ten on the arduino it's  important not to change these first two bits as   this may cause issues with the crystal in the  arduino i want to set these as outputs so it's   zero zero zero zero zero and then one one one  so they're all outputs i'm just gonna write port   eight nine ten as outputs and so now that i've  set these as outputs i want to set them to low   using port b which is sort of like using  digital write in a way and that's just gonna be   for eight um zeros port eight nine ten as low  so that's our pin mode for the inhibit pin   and then writing low on that to start with this  is pin modes for eight nine and ten i'm writing   low on those to start with and now let's need  pin mode for a pots input because we want to   receive from that and pin mode b plus the same  and that's the setup sorted so um onto the loop right so i'm going straight back  in with some of this port b stuff   so i'm just setting them all to low so that  it's reading the uh zero channel or step one you can see that here so if um c b and a which  are eight nine and ten on the arduino are   all zero we will be going through  channel zero there so port b equals   one two three four five six seven eight  zeros and that gets us channel zero   data flow and the rest of it's a  little bit more straightforward to read   so i'm gonna make a new integer called step one a  which is going to be the analog read on the apox   when the data is flowing over channel 0 nice and  then i'm going to print that to the serial print   like so so serial print step one a and then as  we've gotten both of these multiplexers here   sharing digital input pins  meaning that these codes   are being sent to both of them at the same time so  it's reading in same one on each at the same time   it is nice to read the step b potentiometer at  the same time so we want to make another integer   exactly the same as b exactly the same as a i  mean but with b so step one b equals analog read   b pots ooh i've forgotten my thing there and there so it's exactly the same  serial print step one b but the issue   here is that those are both gonna come  out right next to each other in the um   serial monitor so if i upload this to the arduino  now we've just got this number and this number over and over and over again and over and over and  over and you can change them you can get it all to   show zero zero zero zero zero but it's just it's  not very intuitive so to remedy that you want to just put a space in between the two and you can  set this one as line and it will make a new line   at the beginning there so if we upload this now see this is a bit easier we've got  two separate numbers which we can use   so once we've done that the last integer  i'm going to make is a wait integer so   i'm going to set that to 100 for now and  this basically means when i put this delay in   at the end of the read i'm able to adjust it as  necessary rather than going through the code and   changing this number a million times i'm just  going to put a delay on there so it has a second   to read this channel and um that's pretty much  the entire code honestly but what you want to do   is copy this down many many many times and  um by many times i mean seven times but   um you want to take this print line away because  you want to get it all showing on one line   with the print line on the very last one on the  eighth step so that when it reaches the eighth   step it creates a new list of information to work  from what i'm going to do is just copy this down and this is a good time to explain how we get  channel one so we can see the zero zero one   so we'll actually want zero zero one  and this is step two so it's step two   and that's that's literally it so do that again  channel three channel three is zero one zero   wait it's not channel three we want channel two  and channel two there's this one it's confusing   because we've got step three channel two but  that's my decision really there isn't it so   channel two is zero one zero i want this  one to be a one and these two will be threes channel three and i'll just skip through  the rest of this just to go over it one more   time if you want to go through channel  three see here channel 3 c 0 b 1 a 1 c is connected to this one b is connected  to this one and a is connected to this one   so changing it like this one zero zero same as one zero zero well except  we want three so zero one one same as three   zero one give us channel three so one more time right this is the important part so like i said  before we want to change this into serial print   line because that way we're getting a new line and  it's a lot easier to read when you've got separate   lines of 16 rather than just a huge bar of a  million numbers with no differentiator between   them it's also important when i get to the point  of reading it into max because it has a specific   way of reading the carriage return the enter key  one more thing that's worth doing before finishing   this off is just clearing the read using the  inhibit pin at the end of the multiplexing cycle   this is just done super simply by writing  high to the um inhibit pin inhibit mux high and then delay just for a  little bit to give it a second   and then just digital write inhibit mux low which just allows the data to flow back  through so when you are putting it high   we're stopping data flow and then  back low again starting data flow   and that is um that really is the whole code a  lot of it is just the same thing over and over   again um there might be a way to loop it so if  anyone has any ideas on that feel free to tell me   in the next video i'm going to go over a max patch  which allows you to read all of this data in and   use it for rapid prototyping or even an ableton  live patch um i suppose while we're here i better   show you what comes in through the serial monitor  so that's not quite it is it what did i do wrong ah yes so in order to get this  actually working we need a space at the beginning of all the other steps and eight and now we should have the  right thing coming out let's look   yep so now we've got 1 2 3 4 5 6 7 8 9 10  11 12 13 14 15 16 separate numbers coming in   over two pins and at the moment this is doing  it at quite a slow rate at 100 milliseconds you   could technically tell it to do one um i'm not  sure that's a good idea but let's have a look that seems to work fine so that's working or  if you want a particularly slow read for some   reason maybe just to save resources to be honest  because fair play go for a thousand put that on   yeah there you can see it's coming through  coming through just really slowly i thought   10 was a good number but you can experiment with  it for whatever purposes you have but anyway um   i'm gonna leave that bit here and um i will  see you in the next video thank you very much
Info
Channel: Charis Cat // Child of an Android
Views: 2,624
Rating: 5 out of 5
Keywords:
Id: lAy5x5ZZtNA
Channel Id: undefined
Length: 14min 49sec (889 seconds)
Published: Tue Dec 01 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.