Tip: If you simply want to view Marty's accelerometer readings as a graph, you can use the Sensor Dashboard.
Marty has a 3-axis accelerometer built in - you can use this to sense Marty's movement, but also tilt!
The Physics
What's an accelerometer?
An accelerometer is a sensor that measures acceleration, which is how quickly an object's velocity changes. This can happen when something speeds up, slows down, or changes direction.
If you're on your bike and stationary, and then you pedal fast until you're moving at 10kph, you've accelerated. Your speed has increased so you had a positive acceleration. If you then pedal normally so you stay at the same speed, your acceleration is zero. Then, when you apply the brake to slow back down, you have a negative acceleration. An accelerometer on your bike would read positive, then zero, then negative.
If you had a weight at the end of a spring, attached to your bike, when you speed up the spring will bend backwards. When you're slowing down the spring will bend forwards. And when you're moving at the same speed the spring will be straight.
What are the 3 axes?
An axis refers to a straight line of motion - our three axes are forward/backward, left/right, and up/down.
Each axis is perpendicular to the other two, meaning they meet at right angles, so we say they're normal (or orthogonal if you want to be fancy).
You'll probably have seen axes before when talking about co-ordinates - in three dimensions you have an X, Y and a Z axis.
On Marty, these axes are:
| X-axis | Forward/backward |
| Y-axis | Left/right |
| Z-axis | Up/down |
Acceleration due to gravity
If you drop your accelerometer, what readings will it give you?
You might think that it will show acceleration on the Z-axis, since it's accelerating downwards towards the ground. The reality is a little more mind-bending, but makes sense when you think about it - it'll show no acceleration!
The reason for this is that the accelerometer shows acceleration due to an external force acting on it. Let's think about what would happen if Marty was in space, floating along at 100kph. According to Newton's first law of motion, an object will keep moving at a constant velocity unless acted upon by a force. With no forces, Marty will float along in space - our accelerometer will read nothing. Now let's say our Marty is equipped with rocket boosters, and it fires them to slow down - Marty's speed will decrease, and our accelerometer will read the change - the force on Marty has led to an acceleration. That's in line with Newton's second law of motion, F=ma.
Alrighty, let's come back down to Earth.
If we drop Marty (please don't!), gravity will pull him towards the ground. However, because Marty and his accelerometer are falling together, the accelerometer will read approximately zero. This is called free fall.
But if Marty is just stood on a table, there is a force acting on Marty - gravity is pulling Marty down, and the table is exerting an upward force onto Marty to counteract gravity. The accelerometer reads this force, and it'll show the value of Earth's gravitational pull - 9.81 m/s2 (this might vary a bit depending on your location on Earth!).
Marty's accelerometer is calibrated to read in units of g, where 1 g is the acceleration due to gravity (9.81 m/s2).
If we go back to thinking of our accelerometer as a mass on a spring, it's easy to think how the spring will be squished down due to the weight of that mass.
Measuring tilt
So, we know a couple of things:
- Our accelerometer has three axes, which are all at right angles
- Our accelerometer will show a reading due to gravity
Which axis will the reading due to gravity be on? If Marty is standing up straight, it'll be on the vertical z-axis. If Marty was lying down, it would be on the forward/backward x-axis. As Marty is tilted, the components of the reading due to gravity will be split between the axes. We can use this to measure tilt!
Remember though - the accelerometer shows the result of the total forces acting on Marty. If Marty is speeding up or slowing down, that force will combine with the force due to gravity, and confuse the reading.
Using the Accelerometer in Blocks
You can get the accelerometer readings from Marty using the Accelerometer blocks in Blocks, they're available in the Sensing section:
Stopping when fallen over
Sometimes Marty falls over, especially if he's walking on uneven ground. We can use the accelerometer to stop our program if Marty has fallen over:
Moving Eyebrows when tilted
We can make Marty react as we tilt them from side to side:
Viewing accelerometer data
You can also use the Sensor Dashboard to view and graph Marty's accelerometer readings in real time.