Accelerometer

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 device that measures acceleration - how fast something is speeding up or slowing down.

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 of these axes is at a right angle to the other two, 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!), there are no forces acting on Marty. It's like they're floating along in space - and the accelerometer will show nothing!

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/s/s (this might vary a bit depending on your location on Earth!).

Our accelerometer is calibrated to read in G's, where 1G is the acceleration due to gravity (9.81 m/s/s).

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 MartyBlocks

You can get the accelerometer readings from Marty using the Accelerometer blocks in MartyBlocks, they're available in the Sensing section:

Scratch Acc 1

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:

Scratch Acc 2

Moving Eyebrows when tilted

We can make Marty react as we tilt them from side to side:

Scratch Acc 3

Drawing a graph

Add the Pen extension to Scratch, by clicking the extensions button Scratch Extensions Button in the bottom left, and then selecting the pen extension:

Scratch Pen Extension

Now, we're going to draw a simple circle sprite to use. First, let's get the stage up by clicking the stage button Scratch Stage Button.

Select the sprite button and click Paint:

Scratch Add Sprite Paint

Then draw a small circle in the middle:

Scratch Acc Sprite

Click on the “Code” tab to go back to the blocks screen, and then we can add our blocks to make the graph:

Scratch Acc Graph BlocksIt should all look like this when you're done:

Scratch Acc Graph Screen With Blocks

Run the code, and then tilt Marty from side to side. You should see a graph appear that changes as you tilt Marty!

Next Steps - now try and make a graph that shows all three accelerometer axes at once!

Task Runner