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 CircuitPython we can use the “cpx.acceleration” command to read the acceleration of the board on all three axis in m/s^2.

Transcript

Hi, Steven here from Core Electronics. Today I want to talk to you about how to use the Accelerometer on the Adafruit Circuit Playground Express and we're going to program it using MakeCode.

The Accelerometer sits at the very centre of the Adafruit Circuit Playground Express and is marked with an X, Y, Z, for each axis. The Y axis goes vertically on the Circuit Playground Express and the X axis is side to side and the Z goes straight down through the board and the Accelerometer measures the acceleration of the board in any direction and most important measures the acceleration of gravity going down through the board. So, when the board sitting on the table the Z axis is going to be detecting the acceleration of gravity going straight down, which is 9.8 meters per second squared, if we tilt the board up then the Y axis will be feeling the force of gravity going straight down and likewise with X and anywhere in between. So, we use those three Letters to determine the position of the board at any time and then we can work this into our code to have gesture recognition shake recognition and just other motion recognizing features in our program.

So, for this sketch, we made a simple program that detects, a tilt in any direction and turns on some lights and if you slide the slide switch to the right, we also added a feature that creates a pendulum effects where the lights fall to the bottom of whatever orientation the board is tilted in. So, let's look at the code. So, I'm using the MU editor, which is my favourited editor to use for editing Python and it's specially adapted for working with the Circuit Playground Express in Circuit Python, which makes it great. I'll just break down the different parts of the code here, we set up our libraries and initialize our Neopixels, which we've gone over in other tutorials and the first block here is, well I don't want to get into each individual one, but for the pendulum effect these are all the constants that you can tweak to make the behaviour of the light a little bit different, say you want more friction or you want the light to just spin forever and you want to take all the friction of it away. So, you defect that here.

In the main loop, we have our acceleration reading. It's important when you read the Accelerometer, so on the Circuit Playground Express, I should say we use "cpx.acceleration" to read the Accelerometer and anytime you take a reading from the Accelerometer you need to take a reading of the X, Y and Z. So, we do this by putting three variables separated by commas and then equals cpx.acceleration. If you try to only do two like X&Y; it won't recognize they don't know which two you want out of the three. So, we don't use Z in this sketch but it's there and available and you need to create a variable to hold it. We then convert all our X, Y, Z values into integers from floats, this isn't necessary for calculations, but it makes when we print out the values it makes them a little easier to understand because there can be a lot of decimal places there. So first, our tilt program is right here, which take some easy "if and" statements to detect when the boards tilted in one direction and not the other. So, a note about this is, I used a math function that I haven't talked to you about before, which is math.fabs, which is for absolute. So, when we want to detect when the board is tilted down with the y-axis up, we want to make sure that it's not in another position as well so we're not trying to turn on two lights at once. So to determine that the Y is the one that's up alone and not say Y and X are up, I've chosen to say that when the absolute of X is less than 3, so X isn't pointed in either direction because one way would be positive the other negative if in either direction if it's X is less than 3 and Y is greater than 3, then we'll light up the y-axis, positive axis side and then I use that same logic in each direction using the absolute value of the of the axis that we're not using. So in in case you don't know the absolute value, is the value of the number away from zero, so if it's plus or minus a number then it just returns the positive version of that number, so it doesn't matter if X is negative 3 or positive 3 the absolute value of X would be 3.

So if we open up our serial monitor, we have in this mode we have a serial print of our X, Y, Z value and this is why we converted them to integers earlier, so we'd have a nice clean nice clean display in our serial print and you can use this to see in real time the different values that the X, Y and Z are reading and when you use cpx.acceleration, the return is in meters per second squared. So, it's rounded down to 9 but when we show the float it's much closer to 9.8. So, as you tilt this straight up we're reading 9.8, nine m/s^2, acceleration on Y, approximately zero on Z and likewise in each direction. Now since measuring acceleration if you put acceleration on the board greater than gravity then you get numbers greater than nine and we can write code to accommodate that but we're looking for is a simple tilt in any direction.

So the other version of the program that we created is when the slide switches to the right and the light pendulums around, so we use that when we have a nice little chunk of math here that calculates momentum and friction of the pendulum itself and how big around the wheel is that we're using of lights and a little function here to make it wraparound, so it doesn't stop when it reaches the end of our zero to nine numbered LEDs and then we compute the display at the end, by detecting the markers last known position, the orientation of the board is taken into account and how far away the angle of the board is from the light currently and then we make a little fade effect on the LEDs, so they seem to fade out as they move away from the point of the pendulum.

So that wraps up my tutorial, on how to use the Accelerometer on the Circuit Playground Express using Circuit Python, if you're interested in learning more about using Circuit Python or various parts of the Circuit Playground Express, we've got lots of tutorials in a tutorial section and you can make programs in both MakeCode and Circuit Python or even Arduino if you want, so go check them out.

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.