Let's measure how our Raspberry Pi Pico moves using a PiicoDev motion sensor. The motion sensor is a six-axis inertial measurement unit, which means that it can measure linear acceleration and angular velocity in three axes each. We'll wire these two together and get some example code working to see what this thing can do. Let's get started.
To follow along, you'll of course need a Raspberry Pi Pico, this one with pins soldered facing down, a PiicoDev motion sensor and expansion board for Raspberry Pi Pico, and a PiicoDev cable to connect your sensor.
Start by plugging your Raspberry Pi Pico into the expansion board, making sure that the USB connector is on the same side as the two-pin battery connector. You can double check that pin number zero on the expansion board is to the left of the USB connector. Find the PiicoDev socket on the bottom edge and connect your cable, and then connect the other end to 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 lead.
In the article for this tutorial, find the download section and download the three files that you'll need for this example. Right-click each link and select save link as. Save these somewhere that makes sense, I'm saving mine to a PiicoDev folder in my documents. Open up Thonny and use the file pane to navigate to where you saved your files. We have a getting started guide if you need help with Thonny.
Select all three files with a shift click and upload to. The files are uploaded to the Raspberry Pi Pico, we're ready to run the example. Just go into the shell and press ctrl d to reboot, and we can see immediately there is some acceleration data streaming up the shell. It's a little bit tricky to read, but the x and y values.The output values for x and y, or the acceleration along the x and y axes, are very close to zero, and the z value is about 9.8 meters per second squared. A little bit easier to see that in the plot. So we have blue is x, orange is y, and z is red, and we can see z is floating up there at about 10 meters per second squared.
Let's check that makes sense. On the silkscreen label on the motion sensor, we can see the x, y, and z axis labels, and positive z is pointing straight up. This little dot is the head of an arrow that's pointing straight up, but that would mean that the device is accelerating upwards at 10 meters per second squared. Now I know that that's about gravity, but why is it in the positive z direction?
Well this is an inertial measurement unit. If we were in a rocket ship in space that was accelerating straight up at 1g, that would feel to us exactly the same as standing on earth's surface under the effect of gravity, and that's why the z acceleration is currently positive 9.8 meters per second squared.
If I roll the sensor onto the side, I can make z fall off to zero, and now x is at positive 10 meters per second squared, and if we return to that label, that makes sense. The x arrow for positive acceleration is pointing this way, so if I point that arrow straight up, we get a positive x acceleration.
Now if I give the sensor a shove to the left in the positive x, we can see a positive spike and then a negative spike, and if I shove it to the left, we get a negative spike followed by a positive spike in that x-axis.
If you think about it, that makes sense. This is an accelerometer that we're using, so to shove it to the left, I have to first accelerate it in the positive x direction, which gives the positive spike, and then to come to a stop, I have to decelerate it, which means accelerating it in the negative x direction.To bring it to a stop, I get a positive spike followed by a negative spike. If I move it forward, that's positive y. The same can be done with the y-axis, and up and down is the z direction.
Let's take a look at this example script. We'll work with the script that's saved to the Raspberry Pi Pico. Stop the script with Ctrl+C to halt execution and open up main.py on the Pico. Here's what's going on:
We first import the PiicoDev motion sensor package and also import the sleep function so we can create a delay. We create a variable called motion and assign that. We create an instance of the motion sensor and call it motion.
Then, in an infinite loop, we read the acceleration data with motion.readExcelData that reads our three axes of the accelerometer, and that returns a dictionary. We read the x-value from that dictionary and assign it to a variable, and repeat that with y and z. The next line just prints all that data. We print x colon and then we print the data for acceleration in the x direction. Because that's a number, we convert it to a string and concatenate it and repeat that with y and the string of that data, and z and the string of that data.
If we come down past all this commented out code, at the bottom, we have a sleep for 0.1 seconds or 100 milliseconds. There's a little bit more going on in this script though. I'm going to first comment out using Alt+3, comment out that print statement to keep our shell a bit cleaner, and uncomment this whole block using Alt+4. Before, we were reading from the accelerometer, and now we'll read some gyroscope data. Save the script and restart with Ctrl+D. Now we have angular velocity coming through in the script.Shell, and the plot for that is much more interesting.
Just as before, we can look at the axes and read off which is the positive direction for each axis. We have the x, the positive x direction arrow pointing this way, and using the right hand rule, that means positive x rotation is rolling this way.
So if I tip the whole platform towards me, I should see a positive spike in the x axis. And that is the blue axis. I'll try to be as steady as I can and then do a roll towards me. And there it is. And you can see that that peaked out at about 250-degrees per second, which means that if I flick back to flat, that should be a negative roll. And that was at 250-degrees per second.
Reading the z axis, positive z in the acceleration is this way, and positive z in the rotation is this way. You can see that arrow curling anti-clockwise. So that means an anti-clockwise rotation is positive z. And let's give that a test. There it is. There's that positive z hump. Just like acceleration, we get a positive impulse, and then it falls to zero as I slow to a stop.
This time though, because it's not acceleration, it's angular velocity, the velocity is remaining positive. It's just that the rate then falls to zero as I come to a stop.
All right, last example. We'll comment out the gyroscope print and we'll skip temperature. That's not as interesting, I think. We'll jump straight to g-force. So I'll uncomment this block with alt-4, save the script, run with ctrl-d. And now we are just measuring the absolute g-force experienced by the sensor. So we're very, very close to 1g, and that makes sense. We're experiencing one of Earth's gravity.
Interestingly, if I very slowly roll the sensor and just rest it on the desk in any orientation, that value always settles out to about one.So this is like the absolute g-force. If I accelerate the sensor side to side, I can create higher g-forces. But what I think is more interesting is what happens if the sensor experiences free fall. If I carefully drop the whole platform without breaking anything, we can see that the acceleration or the g-force gets very close to zero.
There we go. That was a good one. If the sensor experiences free fall, the g-force gets very close to zero. Free fall is zero g. So we can detect free fall by looking for samples that are very near or at zero g-force.
Now, the unit that's being plotted is g's, but that's because we have this argument that we're passing into the function, which is g equals true. If we stop the script, ctrl-c, and either replace that with a false, or if we just remove that, we could just leave it as no argument and rerun the script. Then now the g-force is being plotted in absolute meters per second squared.
So you can choose to either have g's or meters per second squared. So there you have it, a bit of instant gratification reading motion data using the PiicoDev Motion Sensor. If you make anything cool from this little starter project, we'd love for you to share it in the Core Electronics forums. That's also the best place to ask technical questions.
Until next time, catch you later.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.