This guide will help you get started with a PiicoDev 3-Axis Accelerometer and a Raspberry Pi Pico. 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.

Okay, let's get building something. Plug your Pico into the expansion board, making sure that the USB connector is on the same end as pin 0. Connect a PiicoDev cable at the bottom of the board and plug the other end into your accelerometer. Again, make sure the address switch is in the off position. Now connect to your computer with a USB cable. Find the download section in the article and download the PiicoDev Unified Library and the device.

Driver, right click each link and select "Save Link As". I'm saving my files to a PiicoDev directory in my documents. Open Thonny and navigate to where you saved those files and upload them to your Pico. If it's your first time using Thonny with the Pico, check the article for help setting up Thonny to program a Pico. You know everything is correct if you can see that you're connected to a Raspberry Pi Pico and you have those two files uploaded to it.

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 makerspace, then ask the makerspace coordinator. Otherwise, open a thread on our forums. We're full-time makers and happy to help.

This first example reads the acceleration in three axes and prints the data in meters per second squared. Copy that example code and I'll paste it into this untitled Thonny tab and save it to the Pico as normal. Hit the save icon, select Raspberry Pi Pico, and call it main.py.

This example is most interesting if we can see the plotter, so go to View and show the plotter, and then click Run. In the shell, we can see three lines and these are the three axes of acceleration data as measured in meters per second. We can see one of those axis, the z-axis, is nearly 10 meters per second squared and that kind of makes sense because the z-axis is pointing straight up and remember for an accelerometer that's resting on the surface of the earth that's indistinguishable from accelerating upwards at one g or 9.8 meters per second squared.

Things will get really interesting when we start to rotate the accelerometer. I'll tip it up so the y-axis now points straight up and look at that. You can see the z-axis has fallen off to about zero and now y has replaced it at about 10 meters per second squared. acceleration

If I point the x-axis up, then we have the same thing with the x-axis and this is really responsive. If I just rotate the device around, I can create these sinusoidal waves in the plot. That's pretty cool, so now there's all sorts of experimenting you could do here. You could try to excite just one axis; here I'm trying to shove the y-axis or maybe the x-axis and the z-axis just for completeness. So you can see I can create lots of motion on the z-axis and just a little cross-coupling onto x and y there because I can't shake it precisely up and down.

Let's take a look at the code. We start by importing the device driver and a function so that we can create a delay. We initialize the accelerometer as an object called motion and then we set the motion.range to 2. This is setting the full scale acceleration in g that this accelerometer can measure; you can select 2, 4, 8, or even 16 g.

Next up we have the infinite loop and we're getting the acceleration property and storing that in x, y, and z. So this returns three variables or a tuple of three and we're putting those in variables x, y, and z. We round those to two decimal places; that just makes our print look a little bit nicer. Then we concatenate that data with some nice labels so we have a string that we're building here. We have the label x and then we concatenate onto that the data for x, y, and then the data for y and so on. And that's how we get this nice print statement in the shell.

Because finally we just print that big string. There's a 0.1 second sleep and then we return to the top of the loop. This example measures the tilt angle. We use the right hand rule to determine which axis we're working with. Pick an axis and point your thumb in the positive direction. Here I've chosen the y-axis; the direction your fingers curl is the direction of increasing acceleration.

Angle positive angle can be found in the article for reading angle and copying that code block into main.py. Replacing everything that is there, pressing ctrl r for run will save and run the file. When the code is run, the angle can be seen in the shell and the plot can be seen moving as the sensor is tilted.

In the code, the same setup is used as before. In the infinite loop, the motion dot angle property is read and assigned to angles about x, y and z. A print statement is used to print the angle in the wide direction. Increasing angle is in the direction that the fingers are curled, according to the right hand rule.

Next, an example to detect tapping is found and the example code is copied and pasted into main.py. The set tap function is called and a 1 for single taps and a threshold of 40 is passed in. In the infinite loop, the motion dot tapped property is checked and a one or a zero is printed when a tap has occurred.

I've run the code and it's currently plotting all zeroes. There are no taps, but if I knock the desk you can see a spike in the plot. That's our tap event and you can make this very sensitive by setting lower thresholds or less sensitive by setting higher thresholds. Let's change that threshold to something really high, like 80, and re-run the script. Still works. I have to knock quite hard for that one to register, but of course if you tap the device directly you'll probably get a tap event.

But what about double taps? We can remix this to register double taps by setting the tap type to 2 for a double tap and I'll return that threshold to something sensible, like 40. It's best to have a longer loop time for double tap so that you don't hairline the crossing of that loop. I'll set this to one second to give us lots of time. So now we're printing zeros, there are no double taps. If I tap once, still nothing, but there it is. I had to knock twice to get that spike. There it is again.

In this example we'll detect shaking. Copy that next example and paste it into main. This time around we're calling the motion.shake function. This also has a threshold that is setable so you can tune how strong the shaking needs to be and we'll see that if motion.shake returns true then we'll just print a shaken message, otherwise we'll print a blank line. From that code and now when I shake the device we get that shaken message. This is different to tapping though, it won't register if all we do is knock.

Now it's possible to connect two accelerometers to the same PiicoDev bus as I have before me. The only requirement is that each device has a different setting for its address switch. Taking a look at the example for multiple devices, you can see we have two lines where we initialize.

We call the initialization function this time with an argument ASW equals zero that's the address switch that is off on our first device. For the second device, the ASW address switch is on so when we initialize accelerometer b we initialize it with ASW equals one for on and now we're free to read from each device independently. Here we're reading the acceleration property first on accelerometer a and storing that in three unique variables and the same for accelerometer b we're reading the acceleration property and storing that in three other unique variables.

Here we're printing just the x acceleration for both sensors and that means that if I rotate just accelerometer b I can change one of those lines independent of the other and in fact I'll point accelerometer a in the negative direction so now we have one at positive 10 and one at negative 10.

And so we can see these are pretty versatile devices accelerometers. Not only can we measure linear acceleration but we can use that data to infer things like tilt angle or shaking or tapping. From here you can 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 some questions or just want to share something cool that you've made using the PiicoDev 3-axis accelerometer and 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.