The micro:bit module’s accelerometer has three sensing axes: x, y, and z. You can picture the x axis running side-to-side, the y-axis running front-to-back, and the z axis pointing upwards.
When the micro:bit is held still, the accelerometer can sense how much each of these axes aligns with the downward pull of gravity. This can, in turn, be used to sense how you have tilted the micro:bit. In this activity, you will explore how the various numbers the micro:bit reports for its x and y accelerometer axes relate to its tilt direction and level.
Let's test tilts: level, left, right, toward, and away from you.
When you vary the tilt angle from left to right, the x-axis accelerometer measurement values vary accordingly.
When you vary the tilt angle from facing toward you to facing away from you, the y-axis accelerometer measurement values also vary accordingly.
# test_tilts_intro from microbit import * sleep(1000) while True: x = accelerometer.get_x() y = accelerometer.get_y() print("x =", x, ", y =", y) sleep(750)
Direction | Angle | X | Y |
---|---|---|---|
Vertical facing away | 90° | 0 | 1024 |
Mostly facing away | 60° | ||
Slightly facing away | 30° | 0 | 512 |
Level | 0° | ||
Slightly facing towards you | -30° | ||
Mostly facing towards you | -60° | ||
Vertical facing towards you | -90° | ||
Vertical facing left | 90° | ||
Mostly tilted left | 60° | 888 | 0 |
Slightly tilted left | 30° | ||
Level | 0° | ||
Slightly tilted right | -30° | ||
Mostly tilted right | -60° | ||
Vertical facing right | -90° |
Accelerometer means acceleration meter, but what is acceleration?
Probably the most common, every-day example is when a car speeds up. During a few seconds of acceleration, a car’s speed might increase from stopped to commute speed. The physics term for this is the change in velocity over time, which is also one definition of acceleration. A car’s speed also changes as it slows down, so that’s acceleration too.
A car speeding up is typically considered positive acceleration, and slowing down is negative acceleration.
Have you ever felt pressed into a seat back as a car accelerates? That’s the feeling of the car seat back applying force to you to make you accelerate in the forward direction. In physics:
force = mass × acceleration
...commonly noted as
F = ma
So, if you know the mass (m) and the force (F), you can calculate an object’s acceleration with a = F / m. That’s essentially what an accelerometer does when it measures acceleration - it measures the force applied to a known mass inside it.
Another force that causes acceleration in nature is gravity (g). Since our bodies have mass, we feel the force of gravity pulling us to the ground. The ground has to push back equally to prevent us from falling through or sinking in.
Newton's second law of motion states that the acceleration of an object is directly
proportional to the applied force and inversely proportional to its mass.
a = F / m
To get from this to F = m×a, multiply both sides of a = F / m by m.
Your mass is all the protons, neutrons, and electrons in your body. Your weight is a measure of how much downward force your feet apply to a scale when you stand on it.
When you weigh yourself on the earth, that’s one value because the earth’s gravity is acting on your mass. If you weighed yourself on the moon, your mass would not change, but your weight would be less. In other words, your body would apply less force on the scale. That’s because the moon has less mass than the earth, so its gravitational pull is weaker.
The micro:bit accelerometer is a small chip that senses forces due to gravity and acceleration. It is the NXP/Freescale MMA8652, and it’s located on the bottom of the module, near the lower left corner.
Before talking about how the accelerometer works, remember the capacitor from the cyber:bot visible light sensing activities [1]? Picture the inside of a capacitor as two metal plates separated by an insulator. For example, two plates separated by an insulator have a capacitance—the ability to store charge. If that insulator gets compressed, it brings the two plates closer to each other, and the capacitance increases.
Inside the accelerometer, imagine a tiny object (with mass) held in the middle of a small box. Each wall of the box has capacitors with plates held in place by material that compresses with force. (In the picture, it’s shown as tiny springs.)
When the accelerometer accelerates, the box’s walls have to push on the mass to make it accelerate along with it. As it does so, the tiny springs stretch and compress, changing the distance between the metal plates in each capacitor.
The electronics inside the accelerometer convert that capacitor measurement to a number that reports to the micro:bit when your script executes a statement like x = acceleromter.get_x().
Think about how you speed up when you jump down off of something and land on the ground. That’s the acceleration due to gravity, g. In physics calculations, commonly used values are 32 ft/s² or 9.8 m/s².
The micro:bit won’t typically experience more than 1 g of force from acceleration, unless it’s taking a ride in a high performance model airplane or attached to a wheel that’s spinning really fast. But it does typically experience force equivalent to 1 g of acceleration from gravity’s pull.
These are just concept drawings. The micromachined construction of a proof mass and capacitors inside an actual accelerometer is more elaborate than a simple box with a block inside. It involves a small sliding plate anchored to a flexible material. The plate has capacitive fingers that overlap more or less with metal fingers below it, depending on how much force the plate stretches or compresses the flexible material.
The most common unit accelerometers use is 1 g, which is one unit of gravity. The micro:bit reports gravity/acceleration in steps of 8, from -2048 to + 2048 to report -2 g to 2 g.
If you orient the micro:bit so that the x-axis is pointing down and the y-axis is pointing sideways, the x-axis reports 1024 because it is fully aligned with gravity. The y-axis reports zero because it is not aligned at all.
If you rotate the micro:bit 45° counterclockwise, the measurement of 736 corresponds with the component of the x sensing axis that’s adjacent to gravity.
1024 is the ideal value representing 1 g, but your micro:bit might report more or less than that. Also, since it reports accelerometer measurements in steps of 8, if it reports a value like 1040 for 1 g, don’t worry, that’s really only two increments of 8 off from 1024.
The accelerometer methods are part of the microbit module, so no other imports are needed. The instruction sleep(1000) is always recommended at the beginning of any script that prints messages to the terminal. After that, the main loop begins.
# test_tilts_intro from microbit import * sleep(1000) while True:
With the accelerometer held still, a script can get information from each axis about how much it is aligned with gravity. Regardless of whether it’s accelerometer.get_x(), accelerometer.get_y(), or accelerometer.get_z(), the method call returns a value in the range of 1024 to 0 to -1024.
When the measurement is 1024, it means that the accelerometer’s sensing axis is aligned with gravity. As the sensing axis is tilted further out of alignment with gravity, the value decreases. When it’s level (pointing sideways and not at all up or down), the value is zero. As it starts to point away from gravity, the value becomes negative. When it’s aligned with gravity, but pointing the opposite direction, the value is -1024.
x = accelerometer.get_x() y = accelerometer.get_y()
The accelerometer x and y values are printed, and after a 0.75 second pause, the main loop repeats.
print("x =", x, ", y =", y) sleep(750)
The first tilt experiment measured how far the micro:bit tilted away from being held level. The test isolated each axis. So, when you tilted it toward and away from you, there was minimal left/right tilt. So, the y-axis measurements changed while the x-axis measurements only varied slightly. Then, when you tilted it left and right without tipping the micro:bit toward or away from you, the x-axis changed, and the y-axis only varied slightly.
You can also set the micro:bit on its edge, like it’s a wheel. As you rotate it, both the x and y axis measurements will go through their ranges.
If you instead tilt it toward a corner, the x and y axes will return measurements, each indicating the portion of gravity it senses. For example, you might see a measurement like (512, 512). If you balance it on a corner, you might see a measurement like (736, 736). Note that the measurements are different, but their ratios are the same. More about that in the next activity.
Let’s treat your micro:bit like it’s mounted on a wheel, and watch the x and y values change. In the next activity, you will use a script to calculate the rotation angles.
Angle | x value | y value |
---|---|---|
0° | 1024 | 0 |
45° | ||
90° | ||
135° | ||
180° | ||
225° | ||
270° | ||
315° | ||
360° |
Now, let's try rotation and tilt combined. In this test, you will rotate the micro:bit by a certain angle, and then tilt it away from you by 45°.
Angle | x value | y value |
---|---|---|
0° | 736 | 0 |
45° | ||
90° | ||
135° | ||
180° | ||
225° | ||
270° | ||
315° | ||
360° |
Links
[1] https://learn.parallax.com/tutorials/robot/cyberbot/visible-light-navigation-cyberbot