The full Arduino Workshop in step-by-step format can be found here: https://core-electronics.com.au/tutorials/arduino-workshop-for-beginners.html In this section, we'll learn how to use the digital pins to read inputs and control outputs.

Transcript

Now that we've got our circuit connected up and we know a bit about using variables, structuring our program, and building a breadboard circuit, let's take a look at using the digital pins of the Arduino UNO to read digital inputs and control digital signals. Microcontrollers and computers are 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 two states, on or off. And if you know much about binary numbers, this is exactly how binary operates, A value can either be a 1 or a 0, on or off. And by grouping these 1s and 0s 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 these high or low. When it is high, or a 1, it outputs 5 volts on that pin, and when it is low, it outputs 0 volts.

Now this voltage can be used to control motors, LEDs, transistors, and other devices, but while it can output 5 volts, bear in mind that the current that pin can output is only quite small. Well, in fact, for the Arduino UNO, it's larger than most because it's a really sturdy chip designed to be used by makers. However, it's still only 25 milliamps, or an absolute maximum rating of 40 milliamps. Fortunately, an LED only draws about 20 milliamps at maximum brightness, so we're safe there, but you can't go and control high current loads such as hefty motors or things like that, or large light bulbs, otherwise you can damage that pin or potentially burn out your entire chip. So just keep that in mind.

You're going to need transistors or switches or relays to use those larger load devices, which we'll look at later. A digital input uses the same concept, only in reverse. Instead of outputting a high or a low, the input pin can detect whether the voltage connected to the pin is either a high or a low, 5 volts or 0 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 it's released, a 5-volt signal. That's known as active low logic, which is what we're going to use for some reasons that you'll understand once we put the circuit together and got it working.

So let's take a look at everything we've learned so far and look at turning an LED on and off with a push button using the digital write and digital read functions. So we know a bit about variables, and in the first example, the hello world that we looked at in the very first chapter, we simply used the numbers of the pins directly with the functions that we wanted to use them in. But now that we know about variables, we're going to make our code a little more sophisticated and reusable because we can actually define those pins using variables, as we looked at in the variables section.

So first of all, let's set up our code. Let's get everything in place so that we can put the actions or the controls in that we want. So we'll create two variables. We'll call them int LED pin, and we've got that connected to pin 3. So int LED pin will equal 3, semicolon to end the line, and int button pin, which is going to be our button, is equal to 2. Now, as it stands, that doesn't actually mean that our Arduino knows that we've got a button connected to pin 2, and it's called button pin. All it knows is there's a variable called button pin, a container. And if we look in that container, the number 2 is in there. So first, we need to tell our Arduino that that pin, that variable we're 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. It's really good for the setup. And in the setup, we need to set our pin mode as we've already looked at in the Hello World. But instead of putting pin 3 for our LED, we're going to put LED pin. Now, when the code gets to this point, it sees LED pin, says, oh, that's a variable. I know what that is. Looks into the container that is LED pin, sees a 3, then puts a 3 in place. And we're going to use that as an output. Next up, we've got pin mode and our button pin. But instead of an output like we're familiar with, we're going to be using an input. And we're going to be using a special type of input, something you might not be familiar with. And it's called input pull-up. Now, what this does, we're going to need to take a look again at the whiteboard to properly understand this.

So here is the schematic circuit that we drew in the previous section to understand how our circuit works. And you can see our LED and our button, all well and good, except we're going to be using input pull-up, not just input. Because a standard input pin on the Arduino, when you declare it as an input, it's actually highly sensitive. It will respond to static changes in the air, random transient voltages that are happening on your breadboard, things like that. And so you're going to get a really uncertain signal, which means that when the switch is open, that's all the circuit is. That pin isn't actually connected to anything. It's just an empty wire hanging around for all the Arduino cares. There's no set voltage on it, so it's going to be floating. And if it floats between the threshold that is the on or the off, you're going to see the value of the input on that pin fluctuate and be, oh, one, zero, one, zero. It's going to alternate and it's going to be really random, which isn't good. So what we need to do instead is use a pull-up resistor. Now, what a pull-up resistor does is there's these small resistors on the ATMEGA chip, which we can configure and set using code. You could also use a physical resistor here, but we're going to set it in code because it saves us a component and there's no extra hassle.

So let's extend the Arduino pin out this way a little bit. Now, what a pull-up resistor does is it connects a resistor, funny that, to the pin, right, to the input pin, and it connects that to five volts. Now, I can hear you saying, but if it's connected to five volts, what's our button going to do? How is it going to ever know that we've pressed the button and it's 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 it depends on the chip. But for example, anything between a 10 kilo-ohm resistor could work through to, say, a 100 kilo-ohm resistor or a one mega-ohm resistor. And the reason it's so large is it allows barely any current to flow, but it still ties that pin to five volts. Now, what happens is, is when the pin is not being pressed, when it's released, it's tied to a firm, definite voltage, five volts. So when the pin is not being pressed, it will read a one or a five volts. Now, when it is pressed, that pin goes directly to ground and following the path of least resistance, a very large value resistor or straight to ground, it's 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 it's not being pressed and ground when it is being pressed, a really firm, definite voltage. It's never left floating, which is fantastic.

So back to the code. 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 pull-up. Just like that. And when the code runs, it will configure and set that pull-up resistor inside the chip. Very cool. So now that we have our pin set up, we've used two global variables for our pin definition. Now we're going to go into the void loop. And the goal here is, you can see that I've got this working on my circuit here, is that when we push the pin, it will turn off. When we release the pin, the LED will turn back on again. Now, the reason for this, as I said, is because we're using active low logic, meaning that when we activate the switch, when we do something with it, it's a zero. And when we release it, when it's not active, it's a one, which is a little bit inverted for 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 pull-down resistor, flip that schematic that we were using, but it introduces an extra component and it's not needed. Active low logic works very well. It is usually standard for things like buttons because pull-up resistors are standard on these chips. So we have our pull-up resistor. So how do we do this? Well, as with most challenges or goals in programming, there's lots of different ways you can do it. There's no one set approach, but we're going to look at what I consider to be the cleanest, most efficient and easy to understand approach.

Now that we've got the setup of our code ready to go, we've got our global variables for our pin declarations. We've got our pin modes all set. We're going to make it do something. Now the goal is to turn the LED on when the button is not being pressed and off when it is being pressed. And the reason why this is really easy to do is because our button uses what's called active low logic, meaning when it's being pressed, it will output a zero because when it is being pressed, it will connect the pin to ground or a zero. When it's not being pressed, that pull-up resistor will tie it high. So we can also invert that in code, but for now let's keep it really simple. There's going to be lots of ways to do this in code. No one way is the absolute best way, but we're going to look at a really simple, really modular and easy to expand method using a local variable.

So the first thing inside our void loop, which is going to iterate over and over again, is going to be a variable declaration. We're going to create button state and we're going to use that to store the state of our button. Now we're going to be using the method called polling, which means rather than triggering only when the button is pressed, our loop's 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, it's going to check the state of our button and do something with it. Really clean, really efficient, easy to use. Button state is going to be equal to the state of the input pin, so we can store it. So we're going to use digital read. Now you can see that digital read, as we've mentioned before, follows the same formatting and syntax as our variables. And as most things in programming, we've got all lowercase with a capitalized letter for each consecutive word.

Now digital read only takes one argument, one thing inside the brackets, which is the pin we're 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 let's do something with it. Something pretty handy is that we can write variables directly to outputs. So now we can use digital write. Then we're going to be writing to the button pin. It takes two arguments, two things inside the brackets, the pin that we're writing to and the value that we're writing it, a high or a low, so LED pin. Now we're going to use the variable button state. And we can do this because button state is either going to be a one or a zero, a high or a low. And when it reads that, it puts it into digital write and is constantly updating the state of the LED. We've got our semicolons to finish off all the letters. We've got our semicolons to finish off all the lines. And make sure as we've covered already that you've got the correct board and COM port selected and hit upload. It will ask you to save it. So let's call it button LED test. And it'll compile your sketch, make sure there's no errors. And if you do get an error, it'll come up in the debugging area in orange. Otherwise, it'll go straight to uploading and give you a success message. And the LED turns on. The LED turns off. And we release it, it turns on again. A nice, simple momentary switch. Very, very cool.

Further on in the next chapters, once we get into some decision making and logic, we're going to look at creating a toggle switch, which will turn it into one state when we press it and another state again. And that's a little more complex. There's some decision making and logical flow that we need to look at. But for now, that's using digital write and digital read functions, reading and controlling, outputting digital pins. It's fantastic. In the next section, we're going to take a look at doing the same thing, but with analog pins using analog read and analog write.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.