This guide will help you get started with a PiicoDev 3-Axis Accelerometer and a Micro:Bit. We'll walk through some examples to read acceleration, infer tilt angle (from gravity), and detect tapping and shaking!

Transcript

An accelerometer is a device that measures acceleration. They're used in engineering to measure vibration in cars, buildings, or machines. Really high-end accelerometers are even used in navigation and most smartphones contain an accelerometer to keep the display upright when the screen is rotated. The effects of gravity on an object are indistinguishable from acceleration, which means that an accelerometer at rest on the surface of the earth will measure a total acceleration of about 9.8 meters per second squared upwards, while an accelerometer in freefall will measure zero.

Let's get started with the PiicoDev three axis accelerometer. We'll walk through some examples to read acceleration, infer tilt angle using gravity, and even detect tapping and shaking.

The PiicoDev accelerometer is a three axis device that measures acceleration separately in three orthogonal axes. The axes are labelled on the board as X, Y, and Z. This is the end of the arrow that's pointing straight out of the board. Each arrow indicates the direction of positive acceleration. There are two PiicoDev connectors to allow daisy-chain connections. The address switch labelled ASW allows you to select one of two addresses. For this tutorial, the ASW switch should be in the lower off position.

For more experienced makers, there's also a solderable breakout that brings out two independent interrupts as well. If at any point in the tutorial you have any uncertainty or some questions, you're not alone. If you're in a school, ask your teacher. If you're in a maker space, then ask the makerspace coordinator. Otherwise, open a thread on our forums. We're full-time makers and happy to help.

Okay, let's connect the accelerometer to a MicroBit and get started with some examples. Plug a MicroBit v2 into the PiicoDev adapter for micro bit, making sure...

The buttons are facing up. Connect a PiicoDev cable to one of the PiicoDev connectors and connect the other end to a PiicoDev three axis accelerometer. Again, make sure the ASW address switch is in the off position. Then, connect your MicroBit to a computer with a USB cable.

This first example reads the acceleration in three axes and prints the data in meters per second squared. We'll be referring to the guide article a lot in this video. If you don't already have it open, find a link in the video description. Find the download section of the article and download the two files we'll need. Right click PiicoDev Unified and save link as. I'm going to save my files to a PiicoDev directory in my documents. And right click the device module and save link as.

Open Thonny navigate to where you saved those files and upload both to your micro bit. We know that's successful if we see MicroBit in the files pane and we have those two files uploaded. If you need help getting started with Thonny for micro bit, check the article for a deeper dive in how to get started with your first Micropython project.

Back in the article, find the first example and copy the example code. I'll paste this into the untitled window in Thonny and save that to your MicroBit as main.py. Click run and you ought to see some data streaming up the shell. I have the plotter open here plotting that data. If you don't see the plotter, you can enable it from the view plotter menu. This is the acceleration data coming straight off the accelerometer. We can see three lines, one for each axis. This red z-axis is at about 10 meters per second squared and that makes sense. The z-axis is pointing straight up, so it's seeing 1g or about 9.8 meters per second squared. If I shake the accelerometer, the data goes pretty crazy. I'll slowly turn the accelerometer and...

We can see that the plot slowly changes so now we have the x-axis at about negative 10 meters per second and that makes sense because the positive x-direction is pointing down. If I point the positive x-direction up, it goes to 10 and for completeness I can point the y-axis up or roll the y-axis down.

If we take a look at that example code, we start off with importing the device driver and a function to create a delay. We call the accelerometer initialization function and that returns an accelerometer object that we call motion. So, every time we see motion in this script, we're referring to the accelerometer. We set the range property to 2. This is the full scale range that can be read on each axis in g, so we're setting to 2g which is the most sensitive, but you could also use 4, 8, or 16g.

In the infinite loop, we read from the acceleration property which is a tuple for x, y, and z acceleration and so we assign values to three variables x, y, and z. We round that data to two decimal places which makes this print look a bit nicer and then we just create a string which concatenates a label in this case x with the data the string of that data x. Then we do the same for y and the same for z. Finally, we print the string and there's a short delay before returning to the top of the loop.

Find the next example which is all about reading angle. We can infer tilt angle from the acceleration data. Copy that example and paste it into main. This example looks really similar to the first one. We have the same initialization. This time around, we're reading the angle property and we're printing the angle about the y-axis. Run the script that will save to the microbit and run and we can see a single angle parameter being printed.

Now, to figure out which angle the y-axis rotates about, find the y-axis and point your right hand. accelerometer to detect double taps

The direction your fingers curl is the direction of positive angle. So if I tilt the accelerometer in this direction, the angle should increase and if I tilt it in the opposite direction, it will decrease. We can actually tilt all the way around until we pass 180-degrees and go into the negative angles. This will measure between negative 180 and 180-degrees.

Now if I try to run this script with the accelerometer sitting flat on the table and measure the angle about the z-axis, we actually don't get meaningful data here because the two axes used to calculate that angle are not in the direction of gravity. You can see I can slowly rotate this accelerometer around and I'm just getting nonsense data. It's only when I lift the accelerometer up and get the x and y axes perpendicular to gravity that I can create meaningful data. It's impossible to calculate a real angle about this axis because we don't have any information to measure.

To detect tapping, we call the set tap method. The first argument is a 1 because we want to detect a single tap and there's also a named parameter called threshold which we're setting to 40. You can think of this as how strong the tapping needs to be. Then in the infinite loop, we just query if motion.tapped is true and if it is, we print a one, otherwise we print a zero. By using that data in the plot, we should be able to plot tap events.

Run the script and we are printing all zeros which means that motion dot tapped is false. But if I knock the desk nearby the sensor, then we get a one and I have to knock quite firmly for that to work. If I set this to something really low, then I don't have to tap nearly as hard to register a tap event. Now it's possible to configure the accelerometer to detect double taps. independently

We can use an accelerometer to detect double taps. To do this, we set the first argument to a 2 to indicate double taps and extend the loop time so that we don't miss any double taps. When we rerun the script, if we knock once, we get no tap. It's only when we double knock that we get a spike in the graph showing that we had a double tap event. We can also comment out this line and just as easily print "tapped" and print a blank line. Now, when we double tap the desk, we get a blank shell and a tapped message.

Similar to tapping, we can detect shaking as well. Whereas a tap is a short sharp event, a shake is a more sustained event. We copy the example code and paste it into main. We have the normal setup and this time we are checking if motion dot shake has crossed some threshold and if it has, we print the message "shaken". It helps to have a long PiicoDev cable for this so we can see. We can knock on the desk all day but we won't get a shaken event. We have to actually pick the sensor up and really shake it to get that event.

Finally, it's possible to have multiple accelerometers connected on the same pikadev bus. Here, we have our first accelerometer with the address switch off and our second accelerometer with the address switch on. To have multiple devices connected on the same bus, they need to have unique address switch settings. We initialize accelerometer A using the normal initialization function but this time we're passing it the ASW argument and that's equal to zero because the switch is off. For accelerometer B, our second accelerometer, we initialize it with ASW equal to one because we have set its address switch on. Now, we have two separate accelerometer objects in our code that we can read independently.

Accelerometer A and Accelerometer B are two versatile devices. We can read the acceleration property from each accelerometer and return those values into xa, ya, and za. Similarly, for Accelerometer B, we read the acceleration property and apply those values to xb, yb, and zb. To make sure it's not too messy, we just print the x component acceleration for each device.

When we run this script, we can see that they're both about the same until we start to rotate Accelerometer B so its x-axis is pointing upwards and for Accelerometer A, we rotate that so its x-axis points downwards. We can read those two acceleration values independently and make all sorts of useful projects like a digital spirit level that can indicate if something is sitting level or maybe even a locked box that only opens when you knock on it in the right sequence.

If you have any questions or just want to share something cool that you've made using the PiicoDev 3-axis accelerometer, let us know in the comments below. Until next time, happy making!

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.