How connect multiple switches to one pin, Arduino?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
right so i wanted to show you this i think this circuit kind of shows the power of resistors this is a microcontroller you program it and you can get it to do all sorts i'm using here to read the inputs of switches you just click them and it does something i wanted 12 for a 4x4 grid one of the ways of doing like reading a switch is you just read the input of this switch whether it's open or closed to one of these pins the digital pins and then hungary but with 12 if i have 12 switches and i've got one switch to each pin i'd run out of pins really quickly as in i've got 12 on this side i've got 12 pins on this side it'll fill up my pins very quickly and i want to do other things i want to attach other things to this board so we need a better way of doing it well this circuit here for all 12 buttons i only need to use one pin so i wanted to more discuss how how we can achieve this how this is achieved how we can use just one pin so if i bring the schematic out uh we've got this here so this is the schematic for this circuit you can see how i've got resistors and it all just looks a bit of a mess and confusing so i thought i'd explain how this works hopefully by the end of this you'll understand what's going on here if you don't know already yeah so we'll start with a re with the resistor so the first thing about a resistor we've got like the equations so our equation b equals ir so voltage equals the current times the resistance resistance is the value the value of how much it slows the flow of current down so you can rearrange this to equal i equals v over r so that means if we make r bigger the current gets smaller uh because it's divided so we've got a the convention of current current goes from positive to negative even though the flow of electrons goes from negative to positive uh so we've got i goes that way and the voltage is the voltage drop across the resistor so if we to write this out for this strictly we'll go uh i equals v c c minus g n d over the resistance so you've got you've got this but in this case gnd equals zero so that equals zero so you can cross that off we don't need to talk about it and this in this scenario so we've we've established what how the resistor works so if you want to reduce your current then you put a bigger resistor so the next step to getting to this circuit we want two resistors so effectively how this circuit works is get you to be able to read lots of different uh switch values we need something different about them what we've done to provide a different signal to the board is make when you switch the switch on it provide a different voltage to the board so that's where this circuit comes in and this is a potential divider so depending on the values of rg and r1 it can just be called r it's just because it's pulled from here um you get a different voltage and this is the voltage signal that's going to the board so depending on this ratio we can get a different voltage out so the way this works you've got we assume that there's no current so uh s equals zero so there's no current going to the signal it's just kind of a a block and there's someone reading it um so we have one current going this way that's all the current so the total resistance of this is r one plus r g so we've got i equals v c c because the g and d is equals to zero so we just kind of forget about it for now and then we've got r one plus r g because it's so we've got v over r you add these together um because they're in series you can add them together and anyway so you've got vcc divided by r1 plus rg so we've got the overall this gives us because we'll when we're making the circuit we'll know what rg is r1 and we'll know what vcc is so these are all knowns so we know this one we know this one know this one so that means we can obtain the current going through this potential divider as is so then now we want to find out what vs is so if we know the current so now the currents are known and we don't know the voltage so now we we can use the current we know we can go i equals and then we want vs because we're going to work out what the voltage is from here to here so right from them to work out what the voltage is from there we've got the voltage from here to here there to there which is vcc and this is going to be vs so vs and then our resistance between vs and ground is rg so once we get that we can we've got current in this one current in this one so we can make them equal to each other so we'll come over here for that uh so we've got b c c r one plus r g equals v s r g so we've got these two equations that come together we've got to remember we we know vcc we know r1 we know rg no rg again and then we're just trying to look for vs so all we need to do is times both sides by rg to get the vs on its own so we get v c c r g r so that is our equation for our potential divider so that is what gives us vs got a bit messy but that's what gives us vs so we know we know vcc we know v we know rg we know r1 we know rg again so that means we've got our voltage that we're going to provide to the microcontroller so and they obviously we need to be able to change the signal so we can't have all of them providing their voltage at the same time because that would get messy with this circuit you can only use one switch at a time pressing two switches will send a different signal so you'll make two parts anyway so we got that and then next we had a switch we had a switch to the scenario so it gives two options effectively it gives us it gives us two states to manage so we have the first state which is our vs equals vcc so we've got we've got this first state and that's when this switch this is the switch symbol so when this switch gets closed we have this state and vs equals what these end up being and then when we have it open any sort of charge that any sort of voltage that vs has got gets dissipated to ground so it's either vs equals zero oh oh g n d or v s equals this so we've got two clear states well make it so we've got 12 different signals going all we need to do is change r1 so we change this value and by changing this value we're splitting the signals up see so then when we write it into the microcontroller provide a different voltage so we can read that voltage off so now we're going to want to find the values for r1 that give us like we want equal values for r1 so we can equally step uh vs so we want to divide to get equal values of vs by 12 we want to divide our total voltage vcc divided by 12 kind of equals our v s step so we've got like our voltage step that we want to achieve so to do this we want to rearrange this equation so we'll have this one so to do this we'll go v s and then we're going to divide the whole side from this because wanting to get r1 by itself because of this equation here vs is now our known so we know this we know vcc we know our g we're going to select that and it's going to be the same there's only one value so we can select that beforehand and then we just need to know our two r1 three r four r five r six seven alpha i don't know about twelve uh so we just need to know these resistance values so we need to rearrange for r one uh r one to twelve so we're going to divide both sides by v cc and rg e c c r g equals 1 over and then we'll we'll invert them both so we've got v so we we flip them both flip both sides and then we can minus rg of both sides so we need to modify this slightly so uh we so we'll take this equation and we'll simplify it a bit just to make it easier to work with as in we'll redefine some terms so we've got v c c so we'll redefine the terms uh we'll use this equation and we'll redefine the terms just make it easier to think about r g equals v v c c because we discussed earlier that our vs kind of step is going to be vcc divided by 12 times and then we'll call it n which is the the number attached to the resistor so we're working one to twelve work our way down minus r g equals resistor rn so basically the number we put in front of the resistor after the resistor is the number we put there so then we can get our voltages we'll set well so then we'll define our numbers the to keep the current down what we do is uh the general like a good ballpark is 10 kilograms so we will set our g r g equals 10 kilo ohms uh and then we have vcc on these boards it's five volts equals five oh that's all we need to know so well what you just effectively put in you will make a little table r1 uh two r three r four all right so we've got our resistances i just as a rule of thumb if you have more switches your resistances will as well as you'll have to have a steadier voltage supply so you don't get any ripples for the crossover you also need to have like a more accurate representation of what the ideal resistance so these are the ideal resistances and then you have the resistance that you use in your circuit so we've got our ideal resistances here i made a rule that i wouldn't use any more than two resistors uh to make up these values uh which is i've found that they're perfectly near enough to what they should be for all to work so hopefully that's clearer you've got so we work through here it's a lot quicker than using your calculator if you use like a programming language like python because you can just loop through so this is done in seconds it does it all for you prints it out so now you can see if we have these different voltages so we're going to have so you can see now we've got our resistor values um we've set up our potential dividers here our two phase two state potential dividers here we've got low like them coming down and they're all vs is all connected to the same pin that goes to the board so when this button's pressed it'll give off 0.08 around that whatever it ends up being uh volts to the microcontroller so when it receives that voltage it's basically it's either zero or a voltage because all of them have got the open state so when the the buttons open the holes read zero volts and then when they're closed they all read a different voltage so when this one's pressed it reads 0.083 volts just enough above zero for it to be red and then when we move up we get different voltages the microcontroller knows if it sees a value of 0.25 volts or around that it it'll say oh that's button three and it works like that when you're writing the code you want to have kind of your voltages to be kind of bracketed so i have so that that would be i would have the the voltage be in that range where kind of the cut off points halfway between these two voltages that way you have a bit of you have room for that lee you have leeway built into your program but i feel like this i hope some of that was helpful and made a little bit of sense i just thought that this uh this circuit kind of shows the power of resistors to like define the like give you voltages uh and how they can be used for for various things it's not just for slowing electricity down yeah i might make a another video on how to just make a short program see so you don't have to do this all by hand on the calculator because i find that sort of thing tedious and error-prone so i liked it on the computer i didn't want to cover that this time yeah i hope that was interesting uh all right cheers
Info
Channel: MEC DER
Views: 15,717
Rating: undefined out of 5
Keywords: How read multiple switches from one pin Arduino?, How read multiple switches with one pin Arduino?, How use multiple switches to one pin Arduino?, How connect multiple switches to one analog pin Arduino?
Id: UJu-vUQjenQ
Channel Id: undefined
Length: 17min 25sec (1045 seconds)
Published: Thu Aug 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.