Did you remember that the micro:bit module’s accelerometer also has a z-axis?
Like the hyp calculation from the previous activity, the accelerometer’s z sensing axis can give your script information about the overall level of tilt.
The main advantage of using the z-axis is that it can also tell your script whether the accelerometer is facing up or down. In fact, the hyp calculation is normally only used with two-axis (x-y) accelerometers. Since the micro:bit has a three-axis (x-y-z) accelerometer, scripts can use a combination of atan2(y, x) and z-axis measurements and calculations to get both rotation and tilt level.
In other words, the script can know exactly how the micro:bit is oriented in space.
Example script: test-z-axis-tilt
- Enter, name, and save test-z-axis-tilt.
- Click the Send to micro:bit button.
# test-z-axis-tilt from microbit import * sleep(1000) while True: z = accelerometer.get_z() print("z =", z) sleep(750)
- Check the results in the serial monitor, and experiment with various levels of tilt and rotation.
- Try holding the micro:bit level with its LEDs facing the floor. Were you able to get a z-axis measurement in the neighborhood of 1024?
- Next, try holding the micro:bit vertical. Is the z-axis measurement close to zero?
- Last, try holding the micro:bit level, but with the LEDs facing up. Verify that the z-axis measurement is in the 1024 neighborhood.