In this video, we will learn how to use digital inputs on the Pico through 2 examples. In the first, we will be reading the state of a button, and in the second we will control an LED with a button. We will also look at the relationship between voltage and digital input state.

Transcript

Let's learn how to read digital inputs. Like digital outputs, all 26 of the Pico's GPIO pins are capable of acting as a digital input. When we code digital outputs, we use dot value 1 to set the pin to 3.3V and dot value 0 to set it to 0V, but when we use digital inputs when we read it, it's exactly the same but in reverse. If we got 3.3V from somewhere and connected it to a digital input pin, the Pico will read that as a value of 1 in our code, and if we set it to 0V, it will read it as 0. It's kind of just measuring whether the voltage on the pin is 3.3 or 0V, and it will return 1 or 0 depending on what it's currently being supplied with.

Again, the best way to learn is through examples, so let's jump straight into one and take a look. We are using a button as it is the perfect example of a digital input device. If you haven't already wired up the circuit from the last video and you want to follow along, ensure that you have wired this up at least.

So file, new, and then paste the demo code. It's on our course on our course page. If you're on YouTube, there's a link in the description below to that code. Here is our code, beautiful and short, because we've stripped it down as much as possible so we're just kind of really getting into that digital read.

So again, we start by importing libraries and modules. By importing pin here, we're able to use the dot value and the pin setup functions here. Very important. We also import time here as it allows us to use the sleep function.

Then we need to initialize our input pin, and we store that in a variable that we call button. We use the same command as before, but this time in the settings, we want to use pin 15 because that's what we've got our button plugged into, and we're going to set it to an input this time with pin dot in. This third setting is an optional setting here where we can enable something called a pull-down resistor, and we will talk about that in just a bit.

An important thing worth highlighting here is that a lot of functions are case-sensitive. So if we change this capital P to a lowercase p, and if we run that, we can see that it gives us a syntax error because it doesn't know what the pin command is, what this pin in here is. Everything is case-sensitive. Even if I change that capital N to a lowercase, it'll still throw out errors for us. So always ensure that you have the correct capitalization. That is a pretty common syntax error, and it's often hard to find in your code.

Now to read a pin that we have set up as an input, we simply use this dot value command here with the empty brackets. So here we're saying, read the value of the pin that we set up and we called button, and then print that to the shell here. And that is all inside of this infinite while true loop. So it's just going to keep repeating that over and over. We also have this very short sleep here for 0.1 seconds. This delay is just to slow everything down a little. Without it, the Pico will check the voltage on that pin millions of times a second, but we don't need that and it causes some issues. So we choose to delay it by 0.1 seconds, which means that it will check the pin 10 times a second, which is more than enough for what we need here.

The way that we have the button wired is that when we press it down, we connect pin 15 of the Pico to the 3.3 volts that's coming from the power rail that we set up. And when we don't press it, we aren't supplying any voltage to it. And this perfectly aligns with the 3.3 volt logic level that the digital input reads at. So make sure your Pico is plugged in, run the code. And if everything is connected up correctly, you should see that the Pico is printing the current state of the button with 0 being not pressed and 1 being pressed and supplying 3.3 volts. If you want a nice graph view of this, you can hit view and then click plotter here. And if all is right, you should be able to press the button, which will connect pin 15 to 3.3 volts, which will return a value of 1.

Now a question that sometimes pops up here is what happens if instead of 3.3 volts exactly, I supply 2 volts or 1 volt to the pin, what will it read? I've set up some demo code here to show us exactly that as it's a good learning experience. Don't worry about replicating this though, it's just for demonstration purposes.

So here I'm using a potentiometer, which is a kind of variable resistor to manually create a voltage between 0 and 3.3 volts. This blue line here you see in the graph is a direct reading of that voltage. And we're going to learn how to do this reading in a future video. But the orange line on the other hand is what the Pico is going to digitally read that voltage as. And as you can see, that's either going to be 0 or 1. So you can see that it doesn't need to be exactly 3.3 or 0 volts. You have a little bit of wiggle room on both ends of that.

I should clarify though, this is only for digital inputs. When using digital outputs, like we did last video, the pin won't have this wiggle room. It will be extremely close to outputting 3.3 or 0 volts. That's for digital outputs.

I've got a similar setup here again. Don't worry about replicating this again, it's just another demonstration. The blue line is the actual voltage and the orange is the digital input state. And as you can see, when we push the button, it behaves exactly as we expect. It goes up to 3.3 volts and it starts reading 1 back to the Pico.

So let's see what happens when I disable the pull-down resistor, which we can do so by just deleting that like so. If I stop it and then rerun it, we'll be running this new code here. And off the bat, you can see that something is wrong. I'm not pressing the button, by the way, I'm not touching it in any way here. Now I am, and as you can see, it behaves fine. But when I let go, it's a bit haywire. It's kind of floating around, that voltage is just floating by itself.

And the reason for this is that when we press the button, we supply a known 3.3 volts to the Pico's pins and it behaves fine. But when we let go, we aren't technically supplying any known voltage to it. So it kind of just floats around like it does here. And that is why we need a pull-down resistor, as it very weakly pulls that voltage down to zero. So when we aren't supplying 3.3 volts to it, it doesn't float around. We ground it to something, a known voltage.

Now a lot of the time you need to manually wire up a pull-down resistor from the button to ground. But the Pico has tiny little ones inside of it that you can enable with code just by putting that on the end of your pin setup, whenever you set up a pin as an input, that is extremely helpful.

All right, one more example for this video. And this time it's one that you can actually follow along with, with the circuit that you've wired up. Let's mix this digital input with the digital output that we learned. So copy the second demo code and paste it into a new file.

In this code, we have our imports here exactly like before, followed by setting up two pins here, pin 15 with the button and pin 16 as an output, which is connected to our LED. Now in the while true loop, we introduce some new functions. This is an if-else statement, and we will be learning more about logic like this in the next chapter. But for now, how it works is really simple. Here it checks the value of the button pin. And if it's equal to one, it will run the code that is inside of it, which is turning on the LED here. Anything else besides that, or if it's not equal to one, it will run the code in here, which turns the LED off. And as you can see, it follows the same syntax as the while true loop. It needs to be indented in here by four spaces or one tab. And if we run that code, you should be able to see that when you press that button, the LED lights up.

The code that we have here beautifully represents the most basic application of a microcontroller. It reads in some sort of data, the state of our button in this case, then does some decision-making or calculations with our if statements and finally outputs some sort of signal to control something, our LED in this case.

So that is how you read a digital input with the Pico, but what else can you do with this? Well, this button could be a big red emergency kill switch on a robot or any project really. If the button is pressed, shut down everything for safety reasons. You could string a whole heap of these buttons together and make a keyboard. There is an entire community of people that make very fancy custom keyboards, and they use microcontrollers like the Pico to do so.

If you don't want to deal with buttons, there are plenty of sensors out there that you can use. For example, this sound sensor here will return zero volts if it's pretty quiet and 3.3 volts if it's loud. So you could make a set of lights that you can turn off and on with a clap. This is a light sensor that returns zero or 3.3 volts. You could point it down to the ground and use it to make a robot which follows a line, which is a very common beginner project. A really cool one is one of these ultrasonic distance sensors. It technically uses a digital output and input, but what it does is that it sends out a burst of sound that we can't hear. And then when it bounces back, it returns 3.3 volts. You guessed it correctly. And with a little bit of math, you can figure out how long it took the sound to travel and calculate distances with sound. How crazy is that?

Three key takeaways from this video. One, we need to import modules and libraries to use helpful functions like sleep. Two, any of the 26 GPIO pins can read a digital input with zero volts returning zero and 3.3 volts returning a one. And three, when using components like buttons, you need to use a pull-down resistor to keep the voltage from floating around the place.

Comments


Loading...
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.