We'll explore how to use micro:bit's onboard sensors, and how to write functions in MicroPython.

Transcript

Hello, Clinton here from Core Electronics. Welcome to Chapter 6 on our MicroPython for Micro:bit Workshop. In this video, what we're going to have a look at is using sensors to make a virtual cup that fills when I hold it up and empties when I tip it. So let's first start by having a look at what sensors are and how we interact with them. Sensors are electronic devices that are built into a lot of boards and you can add yourself. In this case, they're down here. These chips allow the board to interact with the real world. So on the Micro:Bit. we have a couple different sensors. So there's the built-in accelerometer, the built-in magnetometer, which allows us to detect magnetic fields, including Earth's magnetic field, which we can use as a compass. There's also a temperature sensor built into the Micro:Bit. which can be used to detect the ambient temperature. You can also hack together a solution by using the LEDs on the front as a light sensor, but that's not a supported feature. It's just something that you can do, which is quite cool.

There's a few things that are worth talking about with sensors, and one of those being how we interact with them. In a lot of cases, when you're dealing with just code, you will use equal to. You can't get away with this with sensors because the values are never that exact because they're a device that's reading a real world continuous system in digital fashion. So they tend to be a little bit off, but in such a small way. But it's enough for your program to be messed up by it. So we use greater than and less than, or greater than and equal to, and that way we avoid kind of getting unusual behavior. The other thing that we need to be aware of when using sensors is noise. Depending on what you're doing, noise can be different things. So if you were, say, for example, measuring the acceleration of a bus pulling out of a station, then what you're interested in is the force of acceleration as the bus moves away. So your noise would be vibration, people walking around the bus, and those kinds of things. Whereas if you were more interested in those vibrations, then the force of acceleration would be the noise. Noise can be also caused by other things. So because these chips are electronic and digital, they tend to be sensitive to heat. So you will find there's more noise for more temperature around. But now that we've kind of had a look at what they are, let's get into programming.

So as with all the other ones, the code's already written there, and we'll just go through it. And you can have a look, you can code along if you would like. So the first thing we're going to do is we're going to import the library and declare a level variable. This level variable will keep track of how full our virtual cup is. We're then going to create a new list, and this list will contain images that we can display on the Micro:Bit screen. So the Micro:Bit library has an image function, and inside here you see this string. And each one of these numbers represents an LED on the front of the Micro:Bit. The semicolon represents the end of the line. You can actually make these images larger than the display, and then navigate around them with some other commands, which you can find in the microPython library. Then the numbers represent 0 to 9, which 0 being off, 9 being full brightness. We're going to create a series of images, 1 being the screen entirely full, and then slowly down to the screen being entirely empty. Once we have this structure, we're going to use this to make an animation later. But first, let's have a look at functions.

So a function is a block of code that lets us perform an action again and again by calling it. A function returns a value and receives values. We define our inputs by putting them inside brackets. So in this case, we take a direction and a level. This is the name of our function, and we'll look at how to call it a bit later. So with our sensor, in this case, we're using the accelerometer, which is a three-axis device. It can detect where the acceleration is happening, either in the up and down, the left and right, or the forward and back. And because gravity is equivalent to an acceleration, there's always gravity acting on the microBit. So even when it's on the table, it's accelerating towards the ground. And we can use this to find out which direction it's facing. So we take in a direction, and if the direction is greater than 40%, which is what this is, because our measure is from 0G to 2G, plus a negative for this sensor. So if we get 1G, we know that it's facing perfectly in that direction. If we get 400mG, we know that it's at least 40% in that direction. We can then also check the level. And because we want to check these both at the same time, we're using this AND. And that just means this one has to be true, and this one has to be true. If either of these is false, this whole statement will evaluate as false. We're then going to work out how much we're going to take off our level based on the angle. So we're going to take our current level, and from that we're going to remove, we're rounding up to the nearest integer. We're also going to take the absolute value of the direction, because we don't care if it's forward or backwards, we still want to reduce our value. And then we're going to divide by 20, just to make it smaller. So what this will do then is return. And this is what the function will hand back to the program. So if I, when I call this, we'll see that we set a variable to be equal to the results of this function. And this is what that equals will be set to. We then has an else if statement for level greater than zero. So if the cup's not empty, we're going to return the level being what it currently is without modifying it. And finally, we're going to return zero if the level is not above zero. So this is to take care of the case if we accidentally go negative, because a negative value in a cup doesn't make sense. So we return zero.

We're going to create our while true loop again. And then the first thing we're going to do is we're going to grab the values from the sensor. To do this, we create a variable, and then we're going to go Micro:Bit.accelerometer.getvalues. And this will return a list of our X, Y, and Z axis. We're going to call them roll, pitch, and yaw. What we're then going to do is we're going to measure, we're going to index those with zero, one, and two.So we're taking the first one, the second one, and the third one. We're then going to take pitch, and we're going to see if it's greater than 1,000. So 1,000 milli G would be equivalent to one gravity. So what this is saying is, in my Y direction, I want to make sure that all of gravity is acting in that direction, which means that I have to have it upright. If I make this equal to, sometimes the sensor's a bit off, or the noise will make the error slightly larger. And so using equal to here would be one of the instances where you could run into problems where it doesn't work.

We're also going to check to make sure that the level is less than 1,000, because 1,000 is a full cup. And again, it doesn't make sense for a cup to be over 1,000. It can't possibly go over full. Once it's full, we're going to add 40 to our level. So while ever it's upright, it'll keep filling up. We're then going to call our function. So the first time we call our function, we're going to put roll in, and it will return the level and make it equal to the value that we set in the function before. Whatever value our function returns. Then we're going to do your, so that each time it checks both. And so if we're tipped forward, it'll empty. If we're tipped sideways, it'll empty. And if we're tipped a combination of both, it'll empty faster.

So now we're going to create our animation. And to do this, we're going to use an else if structure. The nature of this structure is quite important, because it's easy to make a mistake that will give you unusual results. For example, if I were to create use greater than here and say with levels greater than zero, I want to display empty cup, I want to display empty cup. If it's greater than 200, I want to display a middle cup, and so on and so forth. If I'd done that, what would happen is it would get to the first one, and no matter what level it was, it would always evaluate as true. And so none of the others would be evaluated, because it skips everything after the first one if this one returns true. So we need to make sure that if we're using less than, our values are increasing as we go down. So this one evaluates as false if the water level is above 10, and so on and so forth.

Now that we have that, our code should be complete, except for one last line we're going to put in, which is print level. So print level will output our level, but we're not going to see it on the micro bit. It's going to display it on the REPL. So if we open up REPL, we get this message about what version of it is. We hit CTRL-D to reset, and then we start getting values coming. So at the moment, our tank's empty, so it's at zero. When I put it upright, we jump up. And if you were quick, you may have seen a negative value in there. We caught that with our thing, and it returned it to zero. So thanks for watching this video. That should now give you a pretty good understanding of how sensors work. If you have any questions about anything in this workshop, or any of the videos in this please post them in the comments, and thank you 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.