Reading motion data is easy with a PiicoDev Motion Sensor. We can use this 6-axis Inertial Measurement Unit (IMU) to measure acceleration and angular velocity in three axes each. We'll wire them up and perform some experiments to understand how the axes work, and then measure some G-s!


We're going to measure motion today using a PiicoDev motion sensor and a Raspberry Pi. We'll connect these two together and run some example code to measure linear acceleration and also angular rotation.

To follow along, you'll need a Raspberry Pi running like a desktop computer. I'm using a Raspberry Pi 4 model B today, a PiicoDev motion sensor and adapter for Raspberry Pi, and a PiicoDev cable. 100mm or longer is best for Raspberry Pi projects. Today I'm using a 500mm PiicoDev cable.

If you need help setting your Pi up as a desktop computer, follow our Raspberry Pi for Beginners workshop.

I've connected my motion sensor to my Pi using the adapter and cable, and I've also mounted everything on a PiicoDev platform to keep it stable for this tutorial. Follow the PiicoDev connection guide if you need some more help.

I'm now ready to connect my monitor, ethernet, and power, and power on.

In the article for this tutorial, find the download example script section and right-click that link and select save as. I'm going to save that to a folder called PiicoDev in my home directory. I'm just going to call this motion to give it some context.

Double-click motion to open it up in Thonny. If it doesn't open in Thonny, you can always open Thonny from the programming menu, Thonny IDE.

And let's just make sure we have PiicoDev installed correctly. Go to tools, manage packages, and search for PiicoDev with two I's. Hit enter. There's our result. And just install or upgrade if necessary.

With PiicoDev installed and our example script downloaded, we're actually ready to go. You can just click that green run current script button and immediately we have acceleration.Data is streaming into the shell. It is difficult to read because it goes by quickly. The X, Y, and Z acceleration data is streaming up the shell. To get a better view, I will open up the plotter. The plotter shows three lines, one for each axis (X, Y, and Z). The Z axis is at about 10 meters per second squared, which is expected since gravity is about 9.8 meters per second squared. When I roll the sensor to the left, the X acceleration becomes positive 10 meters per second squared and the Z acceleration drops to zero. The motion sensor itself has legends for each axis (X, Y, and Z). The positive X direction is indicated by an arrow. When I push the sensor to the right, there is a positive spike in the blue X acceleration line because I am pushing it in the positive X direction. Interestingly, there is a mirroring negative pulse, which makes sense. To push the sensor to the right, I have to accelerate in the positive X direction, but to come to a stop, I have to decelerate, which is the same as accelerating in the negative X direction. It is difficult to isolate one axis on the bench as any small movement is picked up. The same can be done for the Z axis, creating positive and negative Z acceleration. Pushing the sensor forward creates pulses in the Y axis as well. Let's stop the script and examine what is happening. In the first line, we import the driver for the motion sensor. We also import a function.Create a delay, which is sleep. Then we call the motion sensor initialization function, and we're going to call our motion sensor motion. There's an infinite loop and we first read motion.readExcelData, which returns a dictionary and that gets stored in Excel. Then to make the next lines a little bit easier, we get the X, Y, and Z entries in that dictionary and just store them to separate variables. That makes this print statement a little bit easier to read.

In print, we print the X axis followed by the acceleration in the X component converted to a string for easy printing. And we repeat that with Y and with Z.

Now there's a fair bit more going on here. If I comment out this print statement with alt three, we can go down to the gyroscope data block and uncomment that whole block with alt four. Now we'll demo the gyroscopes. Click run. And now the shell is streaming gyro data, which is in degrees per second. Again, the plot is a little bit easier to look at than the raw data, but we can see we have about zero degrees per second. There will always be some offset with gyros, but we can see that they're all about zero.

Now, if I perform the same experiment and stand the sensor up on its end by turning it to the left, that actually registers as a negative Y angular velocity. And if I restore it back to the bench, that's a positive Y angular velocity. Last time we did that, that affected the X acceleration. So what's going on here?

Looking at our axis labels once again, we can see that the Y axis has the acceleration vector pointing this way. And by the right hand rule, if we point our thumb in that direction, our fingers will curl in the direction.Of positive Y acceleration, which for me, that's rolling clockwise. So to stand the sensor up on its end, we're actually rolling it in the negative Y direction. And then when we restore it flat, that is in the direction of our finger curl, which is positive Y.

So reading this Z axis label, that means that if I turn the sensor on the bench this way, that ought to be a positive Z. Let's see if that works. There it is. And if I restore it, that should be a negative Z.

And again, with X, if I point my thumb this way, fingers curl this way, I need to flip the device up. And that is a positive X angular velocity. Neat. Stop the script. We'll comment out this print statement.

I'm going to skip over temperature and skip straight to G force. So I'll uncomment these two lines, run the script again. And now we're reading just a single axis and that's G force. And we can see that the value is very close to one, which if you think about it, it makes sense. The sensor is currently motionless in earth's gravity and that should be one G.

So if I accelerate the sensor in just about any direction, I can get that G force to spike up. But what I think is most interesting is what happens if the sensor is in free fall. If I drop the sensor and catch it, we can see that that G force value gets quite low. It actually approaches zero.

Now, because we're only sampling 10 times per second, and there's quite a lot of information to print, the sample probably isn't lining up right on that zero G. So this could be interesting if you have your sensor connected to a car and you're measuring the G forces going around a corner, or if you're measuring free fall of some device.Detecting the moment of free fall, that's pretty cool. So there you have it, performing a few motion measurements using the PiicoDev motion sensor and a Raspberry Pi. If you make anything cool out of this starter project, we'd love for you to share it on the Core Electronics forums. That's also the best place if you have any technical questions. Until next time, thanks for watching.


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.