Arduino Workshop – Chapter Two – Using Digital Pins
You know to read digital inputs and controls, digital signals, microcontrollers and computers. As digital devices, they only work with digital signals, binary signals 1 or 0 and analog signals must first be converted into digital signals, but more on that later. First of all, what do we mean by a digital signal? Well, a digital signal is one that only has to state on or off and if you know much about binary numbers, this is exactly how binary operates a value community 1 or a 0 on or off, and by grouping these ones and zeros together, you can create Much larger complex data, a digital pin is exactly the same. It has either on or off States, and we call this high or low when it is high or 1 it outputs 5 volts on napkin when it is low at output 0 volts. Now this voltage can be used to control motors LEDs, transistors and other devices, but whilst it can output 5 volts very that the current that pin can output is only quite small. Well, in fact, the Arduino you know its larger than most because its a really sturdy chip designed to be used by makers. However, its still only 25 mil answer an absolute maximum rating of 40 milliamps. Fortunately, an LED only draws about 20 milliamps at maximum brightness, so were safe there, but you cant go and control high current loads, such as hefty motors or things like that or lives like loads.
Otherwise you can damage that pin or potentially burn out your entire chip. So just keep that in mind going to need transistors or switches or relays to use those larger load devices which well look at later. A digital input uses the same concept only in Reverse. Instead of outputting a high or a low, the input team can detect whether the voltage connected to the pin is either a high or a low five volts or zero volts. A button is a good example of a digital input. When it is pressed, it could output a low signal to indicate the press and when its released a five volt signal, thats known as active low logic, which what were going to use for some reasons that youll understand. Once we put the circuit together and got it working so lets, take a look at everything, weve learned so far, and look at turning an LED on and off with a push button using the digital right and digital red functions. So we know a bit about variables and in the first example, the hello world that we looked at in a very first chapter. We simply use the numbers of the pins directly with the functions that we wanted to use them in. But now that we know about variables were going to make our code a little more sophisticated and reusable, because we can actually define those pins using variables, as we will start in the variables section.
So, first of all, lets set up a code lets get everything in place so that we can put the actions all the controls in that we want so well create two variables and pull them in LED, pin and weve got that connected to pin three in LED Pin will equal three semicolons on the line and int button pin which is going to be. Our button is equal to two now as it stands, that doesnt actually mean that our Arduino knows that weve got a button connected to pin 2 an app called button pen. All it knows is theres a variable called button in a container, and if we look at that container, the number two is in that. So first we need to tell our Arduino. Does that pin that variable we using, we actually want it to be an input or an output, so we can control it so into the void setup. We go remember that void. Setup runs once and once only its really good. For alpha setup and in the setup we need to set our pin mode as weve already looked at in the hello world, but instead of putting pins 3 for our LED were going to put LED pins. Now, when the code gets to this point at Cs, LED fences up thats a variable, I know what that is looks into the container that is LED. Pins sees r3, then put a 3 in place and were going to use that as an output.
Next up, weve got pin mode and our button pin, but instead of an output like less familiar, were going to be using an input and were going to be using a special types of input, something you might not be familiar with and its called input pull up. Now what this does were going to need to take a look again at the whiteboard to properly understand. So here is the automatic circuits that we drew in the previous section to understand how a server works. You can see our LED and Now button all well and good, except were going to be using input, pull up, not just input, because a standard input pin on the audrina when you declare it as an input, its actually highly sensitive in response they had it changes In the air random transient voltages that are happening on your breadboard things like that and so youre going to get a really uncertain signal, which means that when the switch is open, thats, all the circuit is that pin isnt actually connected to anything its just an empty Wire hanging around for all the hard we know cares: theres no set voltage on it, so its going to be closing and if its slope, between the thresholds, it is the on or the off youre going to see the value of the input on that pin, fluctuated Vr 1, 0. 1. 1. 1, 1. 0 is going to alternate and its going to be really random, which is good.
So what we need to do instead is use a pull up resistor. Now, what a pull up resistor does is theres these small resistors on the 80 mega chip, which we can configure it in a set using code. You could also use a physical resistor here, but were going to set in code because it saves us a component and no extra, no extra hassle, so lets extend doing a pin it out this way a little bit now. What a pull up resistor does. Is it snaps or is it dark humming that to the pin right to the input pin and it connects that 5 volts? Now I can hear things, but if its connected to 5 volts whats our whats our button going to do, how is it going to ever know that we press the button its going to be connected to ground, which is a zero? Well, the pull up. Resistor value is quite high. It has a really large resistance and depends on the chip that, for example, anything between a 10 kiloohm resistor could walk through yeah, say 100 kilo, resistor or 100 ohm resistor and the reason its so large is. It allows barely any current to flow, but it still ties that pin to 5 volts now what happens is is when the pin is not been when its released its time to affirm that the voltage v hull. So when the pin is not been pressed, it will read a 1 or a 5 volt.
Now, when it is pressed that team goes directly to ground and following the path of least resistance means a very large failure, this up or straight to ground its going to take ground. And that means that it will register a zero, because that resistor value is quite high, which means we get a positive voltage when its not been pressed and ground when it is been, pressed a really firm, definite voltage, its never left floating, which is fantastic, so thats. The curve so now that we know what a pull up resistor is, we can use a physical resistor or we can configure to use the resistor on our chip. How do we do it? Well, instead of input, we use input port just like that and when the code runs that will configure and set that coop resistor inside very cool. So now that we have our pin setup, weve used two global variables for a pin. Definition now were going to go into this boy loop and the goal here is: you can see that Ive got this working on my circuit here is that when we push the pin, it will turn off Im going to release the pin. The LED will turn back on again now. The reason for this, as I said, is because were using active low logic, meaning that when we activate the switch, when we do something with it, its a zero and when we release it when its not active its one, which is a little bit inverted to the Intuitive way of thinking, but by doing that, allows us to use a pull up resistor inside the chip.
Now we could also get an external resistor and use it as a pulldown resistor flip that schematic that we were using, but it introduces an extra component and its not needed. Active logic works very well. It is usually standard for things like buttons, because what resistors are standard on these chips, so we have our four resistor. So how do we do this? Well, as with most challenges or goals in programming, theres lots of different ways, you can do it thats. No one set approach, but were going to look at what I consider to be the cleanest, most efficient and easy to understand approach. Now that weve got the setup of our code ready to go. Weve got our global variables for our pins declarations. Weve got our pin loads. All set were going to make a do something now the goal is to turn the LED on when the button is not been pressed and off when it is been pressed, and the reason why this is really easy to do is because our uses whats called active Low logic, meaning when its being pressed it will output a zero because when it is being pressed, it will connect the pin to ground or a zero when its not being pressed that pull up. Resistor will tire high. So we can also invert that in code, but for now lets keep it really simple. Theres going to be lots of ways to do this in code.
No one way is the absolute best way, but were going to look at a really simple, really module and easy to expand method using a local variable. The first thing inside our void, loop, which is going to iterate over and over again, is going to be a variable declaration, were going to create button state and were going to use that to store the state of our button. Now were going to be using the method called polling, which means, rather than triggering only when the button is pressed our lips iterating so quickly, hundreds of thousands or millions of times per second just going over and over and over again, so that many times per second, Its going to check the state of our button and do something with it really clean, really efficient either to use button States is going to be equal to the state of the input pins. We can store, it were going to use digital reads and you can see the digital read as weve mentioned before. Foil is the same formatting and syntax of our variables and as most things in programming, weve got all lowercase. We could capitalize let up for each consecutive word now. Digital read only takes one argument. One thing inside the bracket, which is the pin were reading from which is going to be button, pin put that in there fantastic. Now we can read the value of the pin and store it in a variable now lets do something with it.
Something pretty handy is that we can write variables directly to output. So now you can use digital write and were going to be writing to the button. Can it take two arguments, two things inside the brackets, a pin that were writing to in the value that were writing at a high or low led pin now were going to use the variable button state, and we can do this because button state is either going To be a 1 or a 0, a high or a low, and when it reads that it puts it in to do dourados. Currently, updating the state of the LEDs weve got our semicolons to finish off all the lines and make sure, as weve covered already, that youve got the correct board and comport selected and hit upload. Who asked you to save it so lets? Call it button LED test and well compile your sketch make sure theres, no errors, if you do get an error, will come up in the debugging area in orange. Otherwise, youll go straight to uploading and give you a success. Message and the LED turns on Im going to press the button the LED turns off earlier, so it turns on again nice simple moment: research very, very cool, further on in the next chapters, once we get into some decision making and logic were going to look at Creating a total switch which will turn it into one state when we press it and another state again and thats a little more complex systems, decision making and logical flow that we need to look at, but for now, thats using digital write and digital functions.