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

Time to build and code! Today, I'm using a Raspberry Pi 4 Model B and this has an SD card already set up to work like a desktop computer using Raspberry Pi OS.

Check the article if you need help getting started. Plug a PiicoDev adapter for Raspberry Pi onto the GPIO header on a Raspberry Pi 4. The Ethernet arrow will point towards the Ethernet connector on a Pi 3B, the USB and Ethernet connectors are swapped. Connect a PiicoDev cable to one of the ports on the Pi and connect the other end to your accelerometer. Again, make sure that the address switch (ASW) is off.

I'll connect a display, connect to a network, can't forget keyboard and mouse, and apply power. My Raspberry Pi is already set up to be used like a desktop computer and has the PiicoDev drivers installed. See the article if you need help with that.

We'll be working from the article in this guide a lot. If you're not already there, then find the link in the video description. Find the first example and copy that example code. I'm going to open up the programming menu and Thonny IDE, create a new file and paste that code in, and I'm going to save this to a PiicoDev directory in my home Pi directory. I'll call this acceleration.py.

Let's give the script a run. We can see some data streaming up the shell here. If I click the view and plotter option, we can see a plot of that data and these are the three axes as being read from the accelerometer. Here we have the z-axis pointing straight up so that's reading about 9.8 meters per second squared as we'd expect. Remember an accelerometer at rest on the surface of the earth ought to read about 9.8 meters per second squared upwards.

If I tilt it so the y-axis points up, we can watch those lines cross and now the y-axis is pointing directly up reading about 10. I'll turn it so that the x-axis is pointing up. That's pretty cool.

Is what happens if I shake it? It just goes crazy. I should be able to excite just the x-axis by sliding it on the desk and the y-axis.

Can see a little bit of cross-coupling because no matter how hard I tried, there's no way for me to perfectly excite just one of the axes and there's the z-axis for completeness.

Let's take a look at this code. We start by importing the device driver and we also import a sleep function to create a delay. We initialize the accelerometer and call it motion. So, everywhere in this script where we see the word motion, we're referring to this accelerometer. Then, we set the range property to 2 and that's 2g. This is the maximum scale that the accelerometer is capable of reading and it affects resolution. You could set it to 2, 4, 8 and 16g.

In the infinite loop, we read the acceleration property that returns a tuple x, y and z for acceleration in those directions and so we apply those to some x, y and z variables. We do a bit of rounding to round to two decimal places just to make this print look a bit nicer and then we create a string. We have the x label and we concatenate that with data from the x axis and so on for the y and z axes and then we just print that constructed string.

Moving on to the next example from the article, we will infer tilt angle from that acceleration data. Copy the next example and I will open a new script, paste it in and save that as angle.py. After the same setup procedure as in the last example, this time we're reading the angle property which returns three angles about the x, y and z axes. Here, we're just printing the y angle so tilt about the y axis.

What does that mean? Remembering from the right hand rule, we point our right hand thumb in the direction of positive y axis and the direction our fingers curl should be the angle that we rotate for increasing angle. So, I should get increasing angles rotating this way and decreasing angles rotating this way. Let's run the script and find out. to a lower number then it will detect lighter taps

We can detect tapping with this accelerometer. Out where level at about 90-degrees, if I tilt this to the right that's increasing an angle and if I tilt it to the left that's decreasing in angle and if I go all the way around then it wraps at 180-degrees and wraps down to negative 180-degrees. We can also change this to tilt in the x-axis and now we have tilt in this axis. Interestingly, if I tilt it this way then there's no change, I only get a response if I tilt it around the x-axis.

Now I've just changed our printed parameter to the z-angle and we've actually got some nonsense printing here. Think about what it means to measure tilt about the z-axis. Positive z is pointing straight up and so we're measuring tilt as measured in this plane and that doesn't really have meaning because gravity is going straight down. To get meaningful data about the z-axis we actually need to flip the accelerometer up onto its edge and now we can measure a meaningful angle.

So in a nutshell, you can get data for three angles but only two of them will ever make sense for a given orientation. To do this, we can find the example for detecting a tap and copy that example code into a new script. After the normal setup, we call the set tap method, passing it a one for the first argument for single taps and there's a threshold argument that we can tune to set how strong the tapping needs to be before a tap event is registered.

Then in the infinite loop, we query motion.tapped which will return true or false if the accelerometer detected a tap. We'll print a 1 otherwise we'll print a 0. Let's take it for a spin. I'm printing all 0s so there's no tapping detected and if I knock the desk quite hard we get a tap and that's this threshold at play here. If I change this to a lower number then it will detect lighter taps. initializing the accelerometer we pass in the address of the device

We can set a threshold to something like 10 and re-run the script to make it a little more sensitive. Now, every time I tap the desk, we get a waveform in that plot. We can also detect double tapping by changing this type to a 2 for the double tap. I'll increase this loop time a little bit to give us more time for that tapping. Now, no matter how hard I knock, we shouldn't get a single tap event. I'll have to knock twice to get that tap event, so one tap is rejected but on a double tap we get that one printed to the shell and we can see on our plotter that moment in time.

While a tap is a short sharp impulse, a shaking is a little more sustained, so we can differentiate between tapping and shaking. I'll copy that example for detecting shaking and again create a new file. I'll paste it in and save this as shake.py. This time in the infinite loop, we're calling the shake method and we're passing in a threshold here for how strong the shaking needs to be. This time instead of printing a one or a zero, we're printing a message if motion.shake returns true then we'll print shaken otherwise we'll print a blank line.

Run the script. It helps to have a long PiicoDev cable and if I shake this side to side, you can see we trigger a shake event. Importantly though, if I put it on the desk, a shake is different to a tap.

Now it's possible to have two accelerometers connected to the same PiicoDev bus. Here's my first accelerometer and it has the address switch in the off position. When I introduce my second accelerometer, it will need to have its address switch in the on position so that they can be uniquely addressed. This will allow us to read data from each device independently. Here's the code for reading from two accelerometers. We have the normal import statements and this time when initializing the accelerometer, we pass in the address of the device.

We initialize accelerometer A with the argument ASW equals zero, as this is our first accelerometer with the address switch off or zero. When we initialize accelerometer B, we initialize it with ASW equals 1 because its address switch is on. Now we have two accelerometer instances that we can read the acceleration property of.

Here we're reading accelerometer A dot acceleration into XA, YA, ZA and here we're reading acceleration from accelerometer B and we're storing that result in XB, YB, and ZB. Just to keep things clean, we're just printing the X acceleration independently from each accelerometer.

So if we run that example, both accelerometers are at about zero, but if I tip accelerometer A to point downwards, we should see one of those lines go to negative 10 and if I point accelerometer B to point upwards, we should see the other accelerometer climbs to positive 10.

So we can see I'm reading data in from two independent accelerometers. 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, 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.