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!

Transcript

We're going to measure motion using a PiicoDev motion sensor and a Micro:bit V2. Now the Micro:bit already has an IMU or inertial measurement unit on board, but if you wanted to measure the motion of something else, then you'll need an external motion sensor.

The PiicoDev motion sensor is a 6 degree of freedom inertial measurement unit, which means it can measure acceleration in three axes and angular velocity in three axes. I'll show you how to connect these two together, download some example code, and get it running. Let's get started.

You'll of course need a Micro:bit V2, a PiicoDev motion sensor and adapter for Micro:Bit. and a PiicoDev cable to connect them together. Plug your Micro:bit into the adapter, making sure the buttons are facing up. Connect your PiicoDev cable to one of the sockets on the adapter and plug the other end into your motion sensor.

I'm going to use a PiicoDev platform to keep everything nice and secure. And connect to your computer with a USB cable. In the article for this tutorial, find the download example code section, right click each link and select save link as. Save these files somewhere that makes sense. I'm saving mine to a folder called PiicoDev in my documents.

Open Thonny, connect to your Micro:Bit. and use the files pane to navigate to where you saved your files. If you need help getting started with Thonny for Micro:Bit. we have a guide for that too. If you prefer, you could follow along using python.MicroBit.org instead of using Thonny. We have a guide for that too.

I prefer Thonny though because you get the nice plots. Linked in the article. Upload all the files to your Micro:Bit. Select the first file, hold shiftAnd select the last file. This will select all of them. Then right-click, upload to Micro:Bit. And just like that, we're ready to run the code.

If I reboot the Micro:bit with control D, you can see in the shell we now have acceleration data streaming through the shell. So we have in the left column X, Y in the middle, and Z on the right-hand side. And this is in meters per second squared.

Let's take a look at the plot. That's a little more interesting. We have three axes being plotted in the plotter, and I can see two of them are very close to zero. One of them is very close to 10 meters per second squared.

And if I take the whole PiicoDev platform and roll it to my left, look at what happens in the plot. We have one axis fall down to zero and the other one go up to 10 meters per second squared. And then as I lower it back down, they swap places again.

Let's see if that makes sense. I can see from the legend that the blue line is the X axis. And if we read the silk screen on the motion sensor, we can see three axis labels for X, Y, and Z.

We can see that positive X is pointing to the right. So if I pick up the platform and shift it to the right, I can create a bit of a pulse on that blue X axis. Let's have a look at that again.

I shifted to the right, and we get a positive X acceleration, but then it's followed very closely by a negative pulse in the X direction. And if you think about it, that kind of makes sense because to jerk the platform to the right, I have to first accelerate in the positive X direction, but then I come to a stop, which means I'm decelerating or accelerating in the negative X direction.

Likewise, if I shove the platform forward,I can create a pulse in the orange Y axis. And if I lift the platform up, I can create those pulses in the Z axis as well. So why is it that when the sensor is just sitting on the bench with the Z axis pointing up, we have a positive Z acceleration? Well, that's from the inertial force of gravity. If we were in a rocket ship accelerating straight up at 9.8 meters per second squared, that would feel exactly the same as if we were standing on earth's surface being subject to 1G. And that's why just sitting on the bench, the Z axis is reading positive 9.8 or positive 10 meters per second squared.

There are other examples to look at though. Let's stop the code with control C and take a look at what's going on in this main script. This is the example code for this tutorial. The other two files are drivers for this device. I'm opening up the file from in the Micro:bit menu because we're going to edit the file stored directly on the micro bit.

We can see on the first line that we import the device driver, and we also import a function to sleep, which creates a delay. We initialize the motion sensor as motion, and in the infinite loop, we just call motion.readExcelData and store that as a dictionary called Excel. Then we access the X, Y, and Z entries in that dictionary and just store them to separate variables to make the next line a little bit easier. Finally, in the print statement, we print the string X with a colon, and we concatenate that with the acceleration in the X direction, but converted to a string for easy printing. We repeat that for Y and for Z.

There's another example below. If I comment out this print statement with alt three or inserting a hash before the print statement, and then uncomment the code below it, we can see that we can create a pulse by tapping on the device. It doesn't matter what axis we use, tapping it will create an acceleration in all three.A hash, and I uncomment this entire block with alt four, we can save the script with control S and run it with control D. Now we are plotting angular velocity data as read by the gyroscope. These are angular velocities as plotted in degrees per second, and you can see they're all pretty close to zero. There will always be some offset with gyro readings.

Now, if I perform that same experiment where I roll to my left, I can see I create a negative Y roll, and if I then restore the platform to flat, I create a positive Y roll. Before we were affecting the X axis when we rolled the sensor like this, why does this affect the Y axis now on the gyroscope?

Returning to the silkscreen legend, we can see that the Y acceleration is pointing in this direction, and by using the right-hand rule, we can point our thumb in the direction that acceleration would be, and if we curl our fingers around, that tells us the direction that the gyroscope is going to read. So when we roll the sensor in this plane, that's actually on the Y axis.

To roll the sensor in the X plane, we can do the same thing. We point the thumb in our right hand in the direction of the X arrow, and our fingers curl in the direction of X roll. Let's give it a go. I can see that if I roll it towards me, I'll get a positive X, and there it is, and if I restore it, that should be a negative X. Likewise, if I drive the platform like a bus, a anti-clockwise rotation should be positive Z, and there it is.

Stop the code again with Ctrl C, we'll comment out that print statement, and I'll skip over the temperature reading. That's not so interesting. I'm more interested in this last example, g-force.I uncomment both these lines, we can now measure the absolute g-force that the sensor is experiencing. I'll save the script and restart with Ctrl D, and we can see that sitting still on the bench, we're reading about 1g, and that makes sense. We're experiencing Earth's gravity.

If I accelerate the sensor side to side, I should be able to increase that, and this is where things get pretty cool. We can actually use this to detect freefall, because in freefall, we will be at or very close to 0g. I'll try to do this without breaking anything. There we go. We can see for a moment, I got very close to 0g.

Now, the motion sensor really was in freefall, but the sample rate is only about 10 samples per second, and so we probably just didn't align that moment with when the sensor was experiencing 0g. So there you have it, reading acceleration, angular velocity, and also g-force using a PiicoDev motion sensor and a micro bit.

Now, if you make anything cool out of this starter project, we'd love for you to share it over on the Core Electronics forums. That's also the best place to ask any technical questions. Until next time, thanks for watching.

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.