At the heart of every Adafruit Circuit Playground Express lies a three-axis accelerometer. This allows us to write programs that take the orientation of the board into account. We can detect orientation and acceleration on any axis. With MakeCode there are two different ways to interface with the accelerometer, events and live data. We can look for specific gesture events like a shake, freefall, tilt, or specific G or acceleration. We can also read live data which returns the immediate acceleration for a given direction in milli-g (1/1000 of gravity).

Transcript

Hi, Steven here from Core Electronics. Today I'm going to talk to you about using the Accelerometer on the Adafruit Circuit Playground Express.

At the centre of every board, we have a three axis Accelerometer, that allows you to detect the orientation of the board at any moment. So, Accelerometers work by, detecting the force of gravity bearing down on the board and normally when the board is flat on the table. The z-axis runs up and down from the board, the z-axis will be recording a positive one gravity going straight down. When we tip it to the side, the left and right side of the board is the x-axis, when we tip it to the side then the x-axis will read that force of gravity on it and the same with the y-axis and using that data we can tell at any time the exact orientation of the board, we can also read its acceleration, so if the force it's reading is greater than gravity or if there's it's detecting no gravity, then it would be in freefall and we can also create other gesture recognitions like a shake or detect the rotation or something like that.

So today we're going to use make code to program it, so just to give you a better look at the program that we've made, it detects a tilt in any direction on the board using live Accelerometer data and turns lights on that side on when it's tilted. So, let's look at MakeCode and to get started I want to show you how to how to view live data coming from the Circuit Playground Express to get a better feel of what's coming out of that Accelerometer. So, we're going to use an advanced feature this time, something we haven't used before and we're going to go down to console. So, the console is something that the board will output to the computer so that we can view it in real time. so, we're going to use "console log value", which says: x = and then 0 and in that 0 we're going to take an input, now there are two ways to interface with the Accelerometer in MakeCode and the first one is these on shake command. So this is an event type interface with the Accelerometer you can put a block of code in here and it will wait for that action to happen and you have a lot of options you have shaken, you have tilt up, tilt down, face up, face down, freefall and then acceleration that's greater than gravity, like 3 G's 6 G's or 8 GS and then whenever those conditions are met it will run whatever codes inside it. That's not going to work for us right now, but I just want to introduce you to it the other way is the live data which is the "acceleration" in millig-unit (mg), which is thousands of a gravity is what it returns, so one thousand would be one gravity. So, we'll say, "console log value x = acceleration" and I'm just going to right click and duplicate this a couple times to save us some time and we'll change this one to "Y" and this to "Z" and then the same in each field, so we have the same axis aligned with the same acceleration data.

So, if we download this to our board, there we go. We will be able to do show console device. Now something that's important to note here is that I'm using the Windows app version of the MakeCode editor, if you're using the in browser there's no way for the show console to device option to appear but you will be able to have the simulator, so you can get the same results by moving around on the Virtual Circuit Playground Express but I think there's no real substitute for moving your actual board and seeing the live results, so I would recommend on the Windows app store, it's a free program get them MakeCode from there and install it from me to your computer and then you don't need to have access to the Internet to work on your code either. So just to show you what we have here, we have down at the bottom, we have the serial monitor of the data coming back, so it's hard to read because it's flying by fast, but we can pause it. So, it's printing X, Y, Z and the value with the board in the orientation of sitting flat on the table so X is 0, y 0, Z is about a thousand. So, it's reading one gravity straight down which is correct and then one of the cool features about the show console in MakeCode is that it graphs it for you. So, we'll go back to recording it and this is graphing all the live data from our Accelerometers and it looks jerky right now because the limits of the graph expand to fit the maximum sets of data, so even though it works, it has a little margin of error to it and when I bumped the table it shows up. So just to make the graph look a bit clearer, we'll move it around a little bit, so it gets through the full extent of its range and then the graphs going to look a little bit more like we'd expect it to. So as the board is sitting flat on the table we see our z-axis is reading negative one thousand or one gravity right now, if we tilt Y up, there's a bit of a delay there but now we have an almost zero reading on our z-axis we have about a 1,000 on our y-axis and a little bit of noise here from when we move to our x-axis. If I move it to the side, so the X is pointing up then we'll see that our X is pointing down the X drops, Y drops. So, there's a bit of delay but you can really get a feel for what to expect when you put the board in in various positions and if you were to use the Circuit Playground for a science project or something, you can collect some very real position data with this and export it straight to an Excel spreadsheet which could be quite cool.

So, we'll go back to our main project which is our Accelerometer lights and I'll go over how we made that. So the basic part of the program sits in a forever loop which you're should be familiar with by now and we use live data acceleration rather than events because there's less delay if you use the event, when you tilt the board once let's put this program back on the board, when you tilt the board once the lights will turn on and then they'll be a bit of delay before something else happens and I find using live data just makes for a bit cleaner of a result, unless you have one long event, one long piece of code that you want to play whenever it's tilted on its side. So, we used one large "if" loop and I'll make another version right next to it, so you can see. So, within a forever loop we put an "if-else" loop and now it says "if, true, then". So, we have a ring of lights that we want to turn on when it's in a certain position, but we want only wanted to turn on say when Y is greater than 1/2 of gravity. So, when it's tilted at least 45 degrees, so we need a comparison there. So, we'll go into "logic" again and in our "comparison fields" we have the ">". So we'll drag this comparison into our "if" get a live data acceleration as our input and we'll set that to "Y" and then I'm going to change this to be, when "Y is > or =" to and the value to 500 if we set it we can set it to a thousand, but then it'll only turn on when the board is completely vertical and if we set it to, too small of an amount then the lights all flicker a little bit when the board is slightly moved, so you really can just play with the sensitivity there. I chose 500 because it would be difficult to activate two axes at the same time with a value of 500 at normal gravity since 1000 is the total gravity and two of them can't point straight up at the same time or you'd have to get in a very exact position it's unlikely for that to happen.

So, another thing to note about our if-else loop, is there's only one field here, we need to have a lot of fields. There's a little plus mark at the bottom if you click that then it gives you additional fields to fill in. So, our program will run through look for enough acceleration on the Y axis, if it doesn't see that it will move to negative acceleration on the Y axis, the same with X plus and minus and then our finally our "else" is if it's sitting flat, if it's not tilted at all then we'll clear all our lights. So that makes for a simple project, I think it's really rewarding to make projects that use the Accelerometer data because it's just something that you don't really encounter too much in retail products is position responsive items, except in maybe some more high-end electronics. So, it's fun to be able to make a project like that on your own.

So that wraps up my tutorial, on how to use the Accelerometer on the Adafruit Circuit Playground Express. If you want to learn more about the other sensors and features of the Adafruit Circuit Playground Express and how to program using those, head over to our Circuit Playground tutorials section, we've got a lot of great content for you.

Thanks.

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.