Otherwise, we’re going to be working on using some buttons we’re going to control LED brightness using those PWM signals. We’Re gon na learn about a little bit more about the Arduino programming environment, we’re, going to learn how to use functions in the Arduino programming, environment and a whole bunch of other great stuff. So let’s get started your best friend when working with the Arduino or any set of electronics for that matter is going to be a breadboard. This is the first stop for prototyping your circuit and makes everything really easy. The way a breadboard works is all these vertical pieces right here are vertical rails, usually kakum to power, ground and they’re electrically connected to hallway through. So if you look on the left side, for example, you can see these two rails I’ll hook, one up to power and one up to ground and in the middle you have these bigger pieces of breadboard. These are connected horizontally across, of course, not connected, though through the middle, and usually those from acting integrated circuits on there. So you can connect the pins on each side of the circuit and it’s very useful for prototyping next thing. Well, that will be using there. Are these resistors today I’m using a 220 ohm resistor to work with the LED, because you want to make sure that you’re not drawing too much current through it and the other one is a 10k resistor that I’m gon na use.
As a pull up or pulldown for the button, that’ll keep the voltage level at ground when it’s not pushed. I won’t go into how to you how to read resistors here, but you can look that up online. Just Google, how to read resistor codes, it’s, really pretty straightforward, they’re, all color coded, so you can tell the the values an LED or a light emitting. Diode is directional like all diodes, so you need to make sure you put it in the circuit in the right direction. Some of them have little indicators on the plastic part. This one doesn’t, this one you can tell which direction is by the length of the leads. The shorter lead is the one that goes to ground. The last thing here is our push button. The push button works by you just basically push it down and it connects the leads on one side. So if we put our multimeter into diode mode, we can use it to actually see that you can see why I touch the leads together that the reading on the screen goes to zero, indicating that there’s a connection between the probes. The same thing should happen when we connect it to both sides of the button and we press on it. So here you can see that there’s no connection, but if we keep the leads there and push down on the button, you can see that it goes down to zero, indicating that we’ve completed circuit.
Okay, so let’s get our arduino wiring up with a push button. So we can start writing some programs with it. First off let’s connect the ground from the arduino and the power from the arduino, the 5 volts to rails on the breadboard, so that we can use them plug a wire into the ground. Pin on the Arduino I’ll connect it to our left rail here and now do the same thing with 5 volt line so plug it into the connector marked 5 volts and we’ll hook that up to the rail on the right side. Next up, we want to hook our little switch up, so we’ll put it across this gap in the middle pop it in there just like that, and now we can go ahead and connect it. So what we’ll do is we’ll attach a pulldown resistor from one of the pins that can get connected on this side down to ground. So I’m gon na use a 10k resistor for that the exact value isn’t really important here, but it should be relatively large. I usually choose 10k or 1k, so remember our ground is our left side, so I’ll hook that up into ground – and all this is gon na – do – is make sure that when the push button is not being activated that its value is pulled down to the ground. Now we’ll connect the other side of it to the five volt line, so that one we push it down.
It goes to five volts and sends a digital one to the microcontroller so hook that up right there. Now all we have to do is actually hook up. The same place where the resistor is hooked into the the button over to the Arduino, so let’s do that plug one end of a wire into the side of the button that’s pulled down to ground, usually and the other side, I’m gon na plug into pin 8. You can choose any pin you like, really they can all do both digital input and output, but I’ll pick eight and that’s it. We can now use this circuit to send a digital one or zero to the Arduino. Okay, now I’m, just gon na put our external LED on pin 13 of the Arduino. Remember 13 already has an LED on it, but I’m gon na put another one in there make sure the short pin goes to ground, as we mentioned before, and just like that. You don’t need to put a resistor in here because there’s already a resistor for the internal LED on pin 13 great. Now that we have our circuit setup, let’s write a program to control it to start off. Let’S just have it say when we hold the button down and therefore it’s getting a digital one on pin eight keep the LED on. This is simple straightforward. In fact, you don’t even need a microcontroller to do this, but this was a good place to get started.
So let’s define our variables up at the top that we’re going to use we’ll have a pin that the switch is connected to and that’s pin hate and a pin that the LED is connected to which is pin 13. As always, we’re going to need our setup function, so let’s do that in our setup function. Now, instead of just having an output, we have an input as well, so let’s set both goes just as with last time. We use the pin mode command and we have our switch pin, which is an input, and then we also have our LED pin, which is an output, great that’s. All we need in the setup now let’s do our loop, so we’re going to read the value of the switch and, if it’s high we’ll set the LED high and if it’s low will set the LED low it’s. Just that simple, so we’re going to use some. If statements to do that to look at the values, so if we do a digital read of the switch pin, remember it’s digital because it’s just a switch there’s, no analog values here that see they’re 0 volts or it’s 5 volts. So if we look at the switch pin and if that’s high or if it’s at 5, volts, currently we’re going to make sure that the LED is high, otherwise we’re gon na make sure the LED is low and of course, doesn’t matter. If it’s already assigned to one of these it’ll just keep it at that, our sketch is uploaded so let’s see if it does what we want and sure enough.
I hold down the button and the LED stays on great but that’s kind of boring right. You can do that without even using a microcontroller, so let’s make a little bit more interesting let’s try to make it when we push the button the LED turns on, and when we push the button again, the LED turns off. That would be a little bit more annoying to do without a microcontroller, so let’s try to do that. So, if we’re not going to be using this system to toggle the LED on and off, the first thing I have to do is add two new variables up here that allow us to keep track of this state of the system. We’Ll make these boolean boolean scan. Basically, just have two values: true or false, high or low on or off zero or one. They all basically mean the same thing. We’Ll keep track of what the button was in the previous loop, of course, it’ll initially be set to low and we’ll make a boolean to keep track of the current state of the LED, so that we can toggle it, and this will allow us to check things Within the loop and set them to what they need to be, the setup function is going to remain the same as before. We still just have do things that were controlling, but we’re, definitely enough to change some stuff down here right. The first thing where I can do is we’re no longer just check and see if it’s high we’re checking to see if it is currently high and was previously low, because that would mean the button was just pressed so let’s check for that.
Instead of using this, if statement that we have here now, we’ll still read the switch pin and we want to see if that’s high. But we want to see if that was high and the last button was read as low right and now what we’re gon na do, instead of writing to it right here, we’ll still do that later, so let’s copy that down there, but we’re gon na write a Variable now so we’re gon na use that led on that we created before and just flip that value. So what that exclamation mark there does is just invert it from what it previously was, and it will make it the opposite value, which is perfect that’s exactly we wanted to do. We want to switch it on or off and then let’s make sure we update our variable that’s gon na keep track of everything. Obviously this is gon na equal high. Now, because that’s part of the. If statement, let me read that it was high okay, otherwise we’re not going to write here anymore, like I said, we’re gon na do that afterwards, but we need to still set the value of last button because we set it here and we need to have it Set so let’s set last button and that’s going to equal to whatever value we’re currently out here, and you can do the digital read just once at the top and then use a variable to assign it, but for the purpose of this program, this will work.
Just fine and now we’re not writing it always high right, we’re, just gon na write it to the value of LED on okay, and that should do it. That should do exactly what we need. So let’s see how that works. That’S, weird that’s! Definitely not what we expect. Oh that’s, interesting that time it went on with the other times it didn’t. Okay, that time he went off and that time we went on that time. It stayed on when I clicked it something’s, definitely not right here. Why is this only turning on and off some of the time, if you guessed the switching that the switch is bouncing you’d be correct, here’s, what a switch looks like when it’s bouncing up and down? Basically, what that means is with a simple switch like this. You have to worry about the signal going up and down before he stays a certain state, whether it’s pressed or undressed. So we have to write something into our software that deals with that. So let’s go. Do that now so let’s add a function to D bounce, this switch and software. You can do this and hardware also but we’ll for now, with the bouncing software. So the first thing we’re gon na do that’s new, is we’re gon na make our own function and it’s gon na return a boolean, so I’m gon na define as a boolean, whereas these other ones don’t return. Anything. This is gon na return.
A boolean and we’re gon na call the function D bounce and will also feed it, a boolean value which in the function will be referred to as last, and this will be the last date of the switch that will pass. It just like in the other functions that are defined by default in Arduino. You enclose it in brackets, so we just did. Is we made a function? Called D bounce, just like digital read, is a function with an input switch. It switch, pin D bounce is now function with input last, and we can now to find that first, like let’s, make a to more global or make another global variable here. That’S gon na keep track of what the current button value is. Then I’ll also be a boolean and we’ll call that current button, and we need this to use it with our D bounce function and as before, it’ll be sent with a default value of low. Okay and now let’s fill in this function here, so the first thing we’re going to do is we’re going to need to determine what the current value of the switches so we’ll set that equal to current and we’ll do a digital read of that switch pink. Okay. Now so what this is doing is this is just setting the value current to the input that it reads from the switch pin and now we’re gon na have to compare that to the last one. So since we passed the last value, we cannot compare it and see if they’re, equal, that’ll tell us if someone hit the switch.
So if last is not equal to current that’s, what the exclamation mark means remember: we’ll do something. So if the switch change States first thing we’re going to do is we’re gon na delay by 5 milliseconds, and what this is gon na do is. This is going to give the switch enough time to finish debouncing, but is short enough that your finger will not. You have been released from the button because it’s basically faster than we can move, then what we’ll do is we’ll read it again. This is just a very simple way to do a deep bounce, but it works quite well. So what we’re doing here by reading this again is we’re waiting for it to finish we’re, detecting that changed waiting for it to finish bouncing by playing this delay in here and then reading it again, where it’s presumably had its steady value, and then we can return That value from the function so we’ll return current and that should give us our D, bounce value and now, of course, we’ll have to change our loop down here to work with that right. So first thing we have to do is actually run that function, so we’ll set current button equal to the value that’s returned from that because remember returns, the current button State, so current button is going to equal the debounce function and we’re going to pass it last Button, great okay and now, if last button, we don’t need to read it anymore now, because this the D Bands function is doing that for us.
If last button is low and current button, remember, the value of current button has been D bounced already, so you don’t to worry about. It is high. That means someone has just pressed the switch, which is exactly what we’re looking for so now we can still invert this like we did before, but we no longer need to track this because the D bounce function is taking care of that. In fact, we can get rid of our else statement also it’s also taken care of in the D bounce, but what we will need to do is save our last button here and make sure it gets set always to the proper value and that’ll just get set Back to current button, once we’ve set our button so that we can use it next time and through the loop and now this is gon na write led on as before, and that should do just what we want so let’s run that closed. It goes on click. It goes off seems to work pretty reliably. It looks like our switch d. Balancing function worked great job, all right, so that’s, pretty cool, but now let’s try to make the LED change brightness when we click the button to do that. We’Re gon na use something called pulse width, modulation, which is basically a way of annulled, emulating an analog output for an LED or basically any other device. However, some of the digital pins on the Arduino support that so we have to make sure we pick one that does.
If you look at the market on the board here, you can see that PWM pins are marked with a little tilde sign and 13. The one that we currently have the LED hooked up to is not one of them. So let’s pick another one. We’Ll pick eleven. Eleven is a pwm pin, so we’re gon na hook our led up to pin 11 using the breadboard. Now first make sure you unplug the Arduino. We don’t want to do anything while there’s power connected top out. Our LED and let’s put it over on our breadboard. Making sure we hook this short end into our ground, our ground is our left side of the rail over here great, so that led is going between that line over there and ground, and now we can use a resistor to go from the Arduino through the LED To ground and we’ll hook that over into pin 11 making sure that we’re not shorting out any wires. While we do that. Okay, this is gon na, be a piece of cake to fix in here. First off we defined our LED pin up here so let’s make sure we change that the pin 11 because we changed the pin it sucked up next we’re, going to change our LED on value 4, which is a boolean to an LED level variable. So when you’re communicating with a pwm pin, you set it up as a value from 0 to 255 to control it and that’ll control the brightness of the LED.
So, instead of setting it to false we’ll, set it equal to zero and we’ll change it from a boolean to an integer. So now this can take on any value, but we’re only going to concern ourselves with values between 0 and 255, which will set the brightness up. The debounce function can stay exactly as it is, and we’re just going to change some things around over here now. So now, instead of saying flipping, the value of LED on will just increment its brightness, so we’ll say LED level instead of LED on and we’ll say it’s equal to its previous value Plus 51. I chose 51 because it’s 255 is divisible by it, but you can choose any number. You want really we’re going to make sure we take care of cases above 255, but that’ll work. Fine and that’ll give us five different increments. This will still set as before. Now the key thing we’re going to do here is make sure that when we get up past 255 or when we click one more time, the LED goes back into the off state. So if the LED level is greater than 255, because we can’t output values greater than 255 we’re gon na just set it back down to zero. Just like that very simple you don’t actually even need to put in brackets, because it’s just one line, you can put it right after right after it, and the only thing we’re going to change here now is that, instead of digital right, this is going to be An analog right, and instead of LED on, we changed our variable name to LED level and that’s.
It let’s see how that works, and we can see, as I click it, the brightness of the LED increases until I click it and it goes back down to zero. Where it starts all over again, all right that does it for this week’s tutorial. I hope you guys enjoyed and you got a lot out of it. As always, if you have any questions, feel free to ask me on YouTube on hello.mat 14 on my website, Jeremy, Blum comm, whatever I’m always happy to answer. Questions I’m gon na upload the source code. From this episode to my website, I’ll have a link down in the description below shortly after I upload it and that’s about it. I’Ll see you guys next week, we’ll be talking about some basics of Electrical Engineering voltage dividers, you understanding current resistance. Things like that stuff that you need to do some really interesting stuff with the Arduino. All right sounds good. I can’t wait to see you guys next week thanks a lot for watching thanks to element14, for helping me to sponsor this video series. They were kind enough to provide a lot of the materials that I’ll be using to create. These tutorials feel free to go visit their website at element14.com check out their community, which is a great place to talk a few about electronics they already and basically anything else.
arduino button Video
arduino button news
Posted on Wednesday October 09, 2019Pickaxe Controller Is Great For Minecraft, Just Aim Carefully Hackaday … Continue Reading »
Posted on Sunday September 22, 2019A Hard Rocking Arduino Visualization Shield Hackaday … Continue Reading »
Posted on Saturday September 28, 2019Learn Arduino, Raspberry Pi, or CAD with these e-books that are on sale Mashable … Continue Reading »
arduino button Social
🔬 Now that you’ve gotten your feet wet, dive into the kit and enjoy all nine experiments. Order yours today: http://bit.ly/2MnQ7fr
Originally posted 2015-11-02 10:57:30.