In this video, we will get into the thick of it and learn to set digital outputs on the pins of our Pico. We will take a look at digital outputs in the context of the Pico, then go through a practical example of it where we flash the onboard LED and also set some voltages on our external pins.

Transcript

Let's start with the easiest of all the basic IOs, outputting a digital state. The Pico has 26 GPIO pins, and all of these are capable of being used as a digital output. When we set up a pin as a digital output, we can control the voltage on that pin by turning it to either an on or off state. It's kind of like being able to turn a battery or a voltage source on or off with code.

Now the Pico runs on something we call 3.3 volt logic, which means that when you turn the digital pin on, it will output a voltage of 3.3 volts, and when you switch it off, it will supply 0 volts. There is also some other terminology used for these digital states. When we say that we set the pin high, it means that we're setting the pin on and supplying 3.3 volts, and if we say we're setting the pin low, we're setting the pin off. And you will commonly hear these terms being used interchangeably. It's all describing the same thing.

The best way to learn is through examples, so let's just drive straight into one and explain how the code for all this works along the way. So we're first going to open Thottie and ensure that we have a new editor tab open. If not, you can just hit file new and open up a new one. Then we're going to paste in our demo code, which can be found on our course page, and if you're watching on YouTube, we'll have a link to that in the description below.

Our Pico is always going to read the code from top to bottom in sequential order, meaning it will read it line by line. And you can think of each line as a step that the Pico is going to follow to complete a task that you want it to do. Anytime we reset or restart our Pico or unplug it and plug it back in, it will start reading the code from the very top all over again.

So at the top of the code, we have these import statements here, and long story short, these import libraries and modules into our code, which let us use certain commands. Like a lot of things, we're just going to mention importing libraries now, and then we will cover it in depth in a later video, because we just want to focus on the basics.

Next, in this line here, we set up one of the Pico's pins as a digital output, and there is some syntax or grammar associated with this. So on the right side here is the actual command to set it up using the pin function. And in the brackets there is where we put all the settings for how we want to configure the pin. So in the first part in the brackets here, we are telling it what pin we're going to set up. Now, for this example, we're not actually going to use one of the Pico's 26 GPIO pins. We're instead telling it to set up that pin that is connected to the onboard LED. We're doing it just for this example here, just for the first one, but we will do an example using a normal GPIO pin next.

Separated by the comma, we have the next setup option, which is whether we want to use this as an input or an output, and we obviously want to set it to an output. So we will do so with pin.out. Now, we need to store our setup pin in something called a variable. And for this example, we're calling ours LED, but you can pretty much call it anything you want. And we will be talking about variables in depth in a later video as well.

Now at this point, the code will have told the Pico how to set up the pins. And moving down, we come to this while true loop here, and this is a really cool piece of code as it will keep infinitely repeating the code that we place inside of it for the rest of time or until we turn off the Pico. We will be exploring how this works in the next chapter, but for now, just think of it as an infinite loop that we can put commands into.

Inside of our loop, we finally get to the meat of our code, actually controlling the digital output. And what we're going to do here We're going to try and set the pin on for two seconds and then set the pin off for two seconds. And then because it's inside that infinite loop, it will keep repeating that pattern forever. So these two commands here are what actually turns our pin on and off with dot value with these brackets on the end. And as you can see, we've got our LED variable here, which means we're telling the Pico to set these digital states on the pin that we set up as LED.

How this works is dot value one turns the pin on and dot value zero turns the pin off. Between them both, we have these two sleep commands here. Remember this import time up here? This imports something called the time library, which allows us to use these sleep commands. This time dot sleep is a very common function you will use as it pauses the Pico for the amount of time that's in the brackets. In our case, we're telling it to pause for two seconds.

Now here are some important syntax when it comes to MicroPython. See how there is some indentation here? This signifies that all of this code here is nested inside of this while true loop because we've indented it into it. And so these four lines of code here will be repeated over and over inside of that loop. And you can choose how many spaces you want to use to indent your code as long as you are consistent and everything is nice and aligned to the same amount of indentation.

If I deleted one of these indentations, this is no longer inside of the loop. And in fact, the Pico will never actually run this line here because it will execute the code from top to bottom in that loop. So it'll go one, two, three, get to the bottom of the loop and restart. One, two, three, and it will keep repeating that over and over. And because it keeps repeating that loop over and over forever, it never reaches that bottom line. Indentation is a vitally important piece of syntax to learn.

All right, let's now run this on the Pico. Ensure that it is plugged into your computer and that you've selected the right COM port, which we can do so by going down here and selecting our Raspberry Pi board. Then hit save and save it to somewhere on your computer. Then we can run the code by hitting the green play button at the top here. And if everything goes all right, you shouldn't see any errors thrown at you in the shell and you should now be able to see that the LED is flashing on and off on your Pico every two seconds, exactly like we told it to do in the code.

So what's going on here is that our code tells the Pico to set the pin to high and this supplies 3.3 volts to the LED on board and this turns it on. Then it tells the Pico to pause for two seconds, which keeps it on, it pauses and it keeps doing whatever it's doing currently. And then we tell it to turn it off and pause for another two seconds. And then we keep looping that over and over.

Now in the beginning of the code, we told it to use the on board LED with LED in quotation marks there. So it's the one that we're currently controlling. But now that we have this code, it's really easy to edit it to set that same digital on off pattern on one of the regular 26 IO GPIO pins that we can actually access here. If we go back into our code, we can stop it from running and then all we need to do is change the pin in the setup to use the pin that we want. If you wanted to use GPIO 4, you would just put 4 in there. If you wanted to use GPIO 20, you would just put 20 in there. But for this example, we're going to be using pin 16, so pin 16 there. And the reason we're doing that is because it's a really easy one to find. It's the pin on the corner here. So now if we hit run, we can see that nothing happens. Well, nothing that we can see. We are no longer telling the Pico to set the voltage on that internal LED pin so it's no longer flashing. Now I've wired up a voltmeter to pin 16 here, one side on pin 16, one side on pin ground. And as you can see, we are setting 3.3 volts on and off on that same 2-second on-off pattern.

I've gone ahead and wired up this LED here. And you don't need to follow along right now because next video is about making circuits and we will make this exact one here in that video. So if you want, you can wait till then, we'll make it together. Then you can run this code. I'm just doing it now to demonstrate it.

So I've just connected pin 16 to an LED and resistor here. And as you can see, if I run the code, it lights up in that exact same pattern as before. Real-world things being controlled with our Pico exactly how we want to through written code. How cool is that?

And that is how you set up the pins of your Pico to use digital outputs. Remember, you can use any of the Pico's 26 GPIO pins to do so. And you can even do it with multiple pins at once.

But what else can we do with this? What can we achieve now that we can turn 3.3 volts on and off on a pin? Well, you could hook up multiple LEDs and make a visual warning system. Maybe we're monitoring the temperature of something and using what we'll learn later, we can write code to turn on different LEDs depending on the temperature. Green is safe or good. Yellow, it's getting hot and red means it's overheating.

Moving away from LEDs, you can find many parts and modules that can also be controlled with 3.3 volts. A bit more advanced, but you could use a MOSFET as a switch to turn on and off other higher powered components. Here we're using one to control a car headlight, a solenoid, an electromagnet, and even a Peltier module. Now there is a bit of setup and more knowledge required in getting those things working. But the important thing here is they are all controlled with a simple digital output on a pin of our Pico.

There is a lot of information in these videos. So at the end, we're going to try and boil it down to three key takeaways. The most important things for you to remember.

Number one, the Pico reads code from top to bottom and the while loop has this very important indentation syntax.

Two, you can use any of the 26 GPIO pins of the Pico as a digital output. But first you must set it up and assign it to a variable in MicroPython.

And three, you can use the dot value function to control the state of the pin. One in the brackets is on and zero is off.

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.