Analogue signals are continuous meaning that they can have any value in some range (for example 0-1000). Let's have a look at how to use them.

Transcript

Hello there! Clinton here from Core Electronics, welcome to Chapter 4 of our MicroPython for Micro:bit tutorial.

In this lesson what we're going to be having a look at is analog inputs and outputs, as well as “while structures” and “for loops” to do this we're going to be building this circuit where we can change the frequency of this LED by turning this potentiometer dial. So, let's get into it and look at what we need to build this circuit.

So, what we're going to need to build this circuit is an LED, 2 330 ohm resistors, a 10k potentiometer and 4 alligator clips and what we're going to do, is we're going to wire pin 0 to the positive leg of the LED from the negative leg of the LED, we're going to go to the 330 ohm resistor to the ground from ground we're just going to have green wire, then from ground we're going to go up to the bottom leg of the potentiometer over the 330 ohm resistor. This is just so that there will never be 0 volts here, then from the middle leg of the resistor we're going to go to pin 1 and then from that leg we're going to go to the 3.3 volt lead and so what this will mean, is as we change the potentiometer the voltage at this pin will also change.

So now what we're going to do is we're going to have a look at the code required to use this information to actually change some things, so now let's have a look at the code so what we've got to do first is we need to import the Micro:bit libraries, this time we're doing that a little bit differently we're going to import “from microbit *”, in this case what the “*” means is that we want to import everything from that library. This can be really handy because it means that we no longer need to prefix our pin numbers or any Micro:bit functions with “microbit.” this can also be bad because like in the last video we had the time library and the Micro:bit library that both had a function named “sleep”, if we import this way the most recent one will override the old one, so we can lose functionality or we may not be sure which functions we're getting, particularly if we don't know the name of all the functions in the library.

So, again we're going to define some variables to be our input pins, we're also going to define two more variables in this case, the first being steps and this is how many steps were going to take going from our minimum light value to our maximum. In this case I've chosen 1000 because this relates closely to the maximum LED analog out value, which is 1024, I could change this to 1024 and the code would be almost indistinguishable, but I chose 1000 for the roundness. The next variable which we haven't seen this type of variable yet which this one's a string so whenever I enclose characters in quotes this treats it as one block of characters and so the state is a string and strings have unique properties.

So now we're going to have a look at “while loops” and you'll remember from last time we had the “while true loop” and so what a “while true loop” does is it's always set to evaluate as true, we can also use a “while loop” like an if statement and have a condition to check and so here on this line we've created a “while state == “on”” and so whenever this state is on it will continue to loop through this loop, when that evaluates to false, it will break out of that loop so at the end of this line if it gets down to this part here and it's false it'll go, I'm not going to run anymore, so I'm going to move on to the next step which will be this “analogue.right” which is 0 then we're going to sleep for a bit, which is just to buffer between the two states and then there's another “while loop” here where it will check if the state is off and this loop will run until it turns on.

Inside this loop we are going to check if the button is pressed, much in the same way we did in the last tutorial, taking that digital read. If the buttons pressed while we're in this off state, we're going to turn it to on then this whole program because we're within the “while true loop” will cycle back to the start and check again whether it's on. If it's still off, it's going to skip over this block and run through here again but if we're on, which is our original state we're going to step inside this “while loop” inside this “while loop” we have a few other things and so we'll have a look at what the analog read is, so this first line of our on state we're going to collect cycle time and cycle time will define how long we want it to loop for, in milliseconds, we're going to read from the analog in, which is reading our value from this pin, the potential voltage values are 0 to 3.3, in terms of the program and the microbit, it sees 0 volts as 0 and 3.3 volts as 1024. So, at 1,000 we're about 1 second, so our dial will take us from 0 to 1 second.

Then we need to create our time delay between steps, so we're going to divide our cycle time by the number of steps we're going to take, so this means the amount of time it will take to go from 0 brightness to full brightness, is the cycle time that we've chosen based on our analog read. So, we'll just ignore for the moment the “for loop” and have a look at the “analog write” so “analog write” works in much the same way as the “analog read” except we're telling it a to have a value, so at “analog write = 0” the light will be off and at “analog write = 1024” it will be at maximum brightness. So, what this means is anything I put in here where this “I” is will be the value that the LED is at, so the brightness.

So, now let's go back and have a look at this “for loop” so a “for loop” is a particular type of loop that has a set number of iterations, so with the “while loop” it tends to go until some conditions met, but with the “for loop” we have some set number of things we need to do. So, the usage of it in Python is we go, for our variable, so it doesn't matter what name we use here we can use anything but I've used “I” and “I” tends to be the standard notation for loops when you can't think of a variable name. “In range” so this means “range” creates a list of elements from 0 to whatever you want or from whatever you want to whatever you want, could be -100 or anything. So it creates a range of numbers from this one, to this one too and then takes a step of this size, you can leave the step size off and by default it will assume “positive 1” which can be good and can be bad, because if you have a higher number here to here then that will mean that it will try to loop forwards to go backwards and will typically throw an error.

So, what we're doing within this loop is we're writing to the analog and so it's writing “I” so for every value in this range, it will write the number that it is at that value. So, for the first loop it will write 0 and our LED will be off on the second it will write one and then it will keep going until it gets to 1000 which is our maximum brightness, between each step we're going to sleep our time delay this will make the total time take the second that we want and the frequency and without it we probably wouldn't see the light changing at all, it would just be on and off so quickly that we wouldn't see the change. Then for each step in the loop we want to check if someone's pressed the off button which will be “a” if they press the off button it's going to switch the state variable to “off” once the state variables “off” it's going to run this break command and break works for any loops and what break does, is it tells it to stop looping. So, if we're at step 500 out of our 1000 and I press the button, so half brightness then it switches straight to the off one cancels that loop and then runs the program again. So, pressing it again I turn it back on.

The second for loop does almost identically the same thing, we'll talk about this in a later video using functions where we can use reuse the same code, but for now we're just going to write it out again. The only difference is, this time we're going from our steps so we're going from 1000 down to 0 and we're using a step size of -1 so again this will loop through starting at our high number and then stepping down lower and lower and then everything else remains the same as the previous step. Once our loops finish or break, they will either loop again or if the state's been changed it will then move on to the next piece of code which we now, as soon before we write “analog to 0” which turns off our LED we sleep for a second, and then go into the off state where we just wait for the on state again.

So that should cover analog inputs and outputs and loops, if you have any questions about any of the other things or more expansion on it just mention it in the comments and we can help you out in the next video, we're going to take a look at using the radio on the micro bit and we're also going to have a look at using lists which are an incredibly useful data structure. So, thanks again for watching and I'll see you next time!

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.