Now that we have everything set up it’s time to have some fun! we are going to write some code that will let us interact with the micro:bit through buttons and an LED.

Transcript

Hello again, Clinton here from Core Electronics. Welcome to Chapter 3 of our MicroPython for Micro:bit workshop. In this one, we're finally going to start getting to doing some actual MicroPython, so this should be fun. We're going to wire up a switch so that we can turn on and off an LED by pressing the A button like this.

So let's get started by having a look at what we're going to need and wiring up the circuit. What we're going to need is, we're going to need two alligator clips, an LED, and a 330 ohm resistor. And what we want to do is we want to wire our first alligator clip from pin 0, from pad 0, to the positive leg, which will be the long leg on your LED. Then from the short leg of the LED, we want to go to our 330 ohm resistor. And from the bottom of our resistor, we want to go to the second alligator clip, which we will take to ground. If you're, there's a picture of this in the tutorial description, so if you get lost, don't worry.

Once we have our circuit wired together, we now need to have a look at the code required to make this work. So let's get straight into that. Okay, so the code's available in the thing, and you can either copy and paste that in and go through with me, or you can write it out as I explain what the code is.

The first couple of lines here, we're going to be importing libraries for Python, or in this case, MicroPython. And this works in very much the same way that it does in normal Python. So we just import Micro:Bit, which this is the library associated with the hardware we're using. And then we're going to import time, which is a library for dealing with actually managing real time as humans see it, not as time as a computer sees it, which is in clock cycles.

The next couple of lines, we're going to be declaring variables. And variables are containers for code, so they hold different bits of information. In Python, they can hold any information we want. We can assign them to numbers, to letters, to strings of letters, or as we've done here, we can assign them to objects. And these objects are our button A, which represents this button here, and pin zero, which represents this pin here. So now that we've saved those as these names, we no longer need to write this Micro:Bit.pin0, we can just write output pin.

The next line here is while true, and the while true loop just continues to run that code forever. So anything that's tabbed in from that loop will keep running. If I were to put another line down here and put another line of code, this piece of code would never run because the while true loop would just continue to run forever.

So inside this tabbed section of code, where we're running inside the while loop, we've created some other code. And the first one we're going to look at is an if statement. So an if statement will have a look at whatever's evaluated here. And if this term is true, evaluates to true, it will run the code that's tabbed in from it. If it's false, it will ignore this block of code and move on to this next step, where we have Micro:Bit.sleep, which this will sleep for 125 milliseconds.

If in this case, our code does evaluate true, we then get to this second option, which is another if statement, but this one's slightly different in that it has another statement associated with it. So what this piece of code does is says, if this evaluates to true, I want you to do this thing. If it's not true, I want you to do this thing. This is really useful logic for programming, and you will use it a lot. And if you've already been programming, you will be very familiar with these structures.

The next thing that we'll look at here is these functions here, what we're evaluating. So this myButtonIsPressed equals 1. What we're doing is we're checking this myButton, so the Micro:Bit button A, and we're having a look and seeing if it's pressed, because whenever the button's pressed, this will return 1. Whenever it's not pressed, it will return 0. So if the myButtonIsPressed is equal to 1, then we run this code. We can then read from our output pin. So what we're doing here is we're saying, is the pin already turned on? So is digital output 0 equal to 1, which is its logic state? If it's equal to 1, we're going to then switch to write mode. And so this will then write a 0 to the output pin, which will turn off the LED. If the LED's already off, this code will go, oh, the LED's not on, so I'm going to turn it on. So this digital.write, this output pin.writeDigital will write 1 and then turn the LED on.

So this concept of a state-based switch is really important in programming. So this program's aware of when the light is on and off, and the program changes behaviour based on that switch. This is called a latching switch and is actually really quite important for computer science and is actually how memory works in a lot of computers.

We then have this time.sleep, which functions in a very similar way to the Micro:Bit.sleep, except we're in seconds. So 0.5 is actually equivalent to 500 down here. I thought it was important to point this out because if you're used to Python and you're coming to MicroPython and you use the Micro:Bit.sleep function, you might try to use the other way or vice versa, so I thought it was worth including.

Now that we have our code all written in, we can then simply flash this to the Micro:Bit. Once that code's flashed on there, you should be able to press your A button and turn on the LED. Pressing it again will turn off the LED.

So I hope you've enjoyed and learned something from this tutorial, and in the next tutorial we're going to start taking a look at analog inputs and outputs. So I'll see you next time, and thanks again for watching.

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.