micro:bit Project 9 : Electronic Stabilizer
Have you heard of the accelerometer? The accelerometer measures the acceleration along 3 direction and it is getting more popular in electronic gedgets. For example, you turn the phone or laptop 90 degrees, its page will follow your barycenter and turn 90 degrees automatically. This is a very human-centered design. In this lesson, we are going to simulate this function!
Connect the servo to P0
Goal: Fix the servo on the micro:bit, servo will keep pointing upright regardless how we tile board.
Micro: bit detects the gesture automatically through the accelerometer. Gravitation force is also a form of acceleration ,and micro: bit can detect the gravitational force along X, Y, and Z directions, whereas X is along the left-right direction, Y is along the front-back direction, Z is perpendicular to the board and along the up-down direction.
The values of the X, Y, Z axes can be achieve from the function “acceleration” under "Input".
When the mainboard lays flat on the desktop, gravitational force goes along up-and-down direction, so that the values of X and Y axis are close to zero and the Z axis is around 1000 (mm / S2), which roughly equals to the gravitational acceleration.
When the board is tilted, the values of X and Y change. When it leans to the left, the value of X is negative or, conversely, positive.
So we can enable the servo to rotate a certain degrees in the opposite direction by detecting the leaning degrees of micro:bit to keep the pointer pointing upwards all the time.
STEP1: When the board is tilted to the left, the gravity sensor will produce a negative output value along the X direction. We will rotate the micro: bit toward 0 degree to offset this angle and vice versa. Since the gravity sensor has a value between -2048 and 2048, and -500 to 500 is a range that can be effectively detected relatively, we can make the -500 ~ 500 correspond to 0 ~ 180 degrees of the servo through the “map 0”.
STEP2: Fix the servo and expansion board with double-sided tape so that the two remain relatively still. Noe that the orientation of the servo head needs to be the same as that of the USB port so that the gear shaft is perpendicular to the X axis and the degree of rotating of the servo is complementary to the degree of tilting of micro: bit.
Exercises for improvement
Try composing a compass using the electronic compass built into micro: bit.