The previous page Did You Know? This Way Up explained how to use arccos to calculate the angle between the micro:bit’s z sensing axis and gravity.
When the LED display is facing straight down, so is the z sensing axis. So the angle between z and gravity is zero.
As the angle between the z-axis and gravity increases, so does the measurement. When the LED display (and z-axis) is facing straight up, it’s angle is 180°. Again, rotating past 180°, the angle decreases, like the last example where it’s back down to 135° again.
Example script: z_axis_degree_of_tilt_try_this
- Enter, name, and save z_axis_degree_of_tilt_try_this.
- Click the Send to micro:bit button.
# z_axis_degree_of_tilt_try_this from microbit import * import math sleep(1000) while True: z = accelerometer.get_z() if z > 1024: z = 1024 elif z < -1024: z = -1024 z_angle = round( math.degrees( math.acos( z/1024) ) ) print("z =", z, ", z_angle =", z_angle) print() sleep(750)
- Check the results in the serial monitor, and experiment with various levels of tilt and rotation.
- Fill in the table with your measured z_angle values.
z | z_angle | Direction LEDs are facing |
---|---|---|
1024 | 0 | Straight down |
728 | 45 | Half way between straight down and sideways |
Sideways | ||
-728 | 135 | Half way between sideways and straight up |
-1024 | 180 | Straight up |
Half way past straight up on way to sideways | ||
Tilted toward you by 45° | ||
Tilted away from you by 45° | ||
GND corner tilted down by 45° | ||
0 corner tilted down by 45° |