An accelerometer chip measures the forces acting on a tiny weight in each of three dimensions. If you imagine the micro:bit sitting flat on a desk, the x dimension is left to right; the y dimension front to back and the z dimension into and out of the desk.
If the micro:bit is perfectly flat, the forces on the x and y dimensions will be zero. The force acting on the z axis will have a value (when read in your JavaScript Blocks or Python code) of about -1000 due to the pull of gravity. If you then tilt the back of the micro:bit up then the force in the y dimension will increase a little.
Its these changes that allow you to use an accelerometer to detect the orientation of the micro:bit, since gravity will always be acting in the same downwards direction.
Movement Alarm Example
As an example of using the accelerometer lets make a sensitive movement alarm from out micro:bit.
The project works like this:
When you press button A, the display changes top show a triangle, indicating that the alarm will arm itself after two seconds. Once armed, the display will be blank.
If you now try and move the micro:bit then the LEDs will all light indicating that the alarm has been triggered. Put the micro:bit down, so that it's still and then press button A to arm it again.
JavaScript Block Code
Here is the JavaScript Block version of the alarm program. The JavaScript Blocks Code editor is embedded directly on this page below. From the editor, you can click on Download button (bottom right) and then copy the downloaded file onto your micro:bit. Alternatively, you can Click here to open the editor in a separate browser tab.
The code works like this:
The on start block will be run the first time the program runs after the micro:bit has been powered-up or reset. This sets a variable z
(the acceleration in the z axis) to zero. It also sets a Boolean (true or false) variable (alarm triggered
) to false. The variable alarm triggered
will be used to indicate to other parts of the program that the alarm has been triggered.
The forever
loop will run over and over again and is in three parts, each part being an if
/then
block.
The first if
/then
block checks for a press of button A, and if button A is pressed, it displays a triangle symbol, waits two seconds (2000 milliseconds) then reads the acceleration in the z axis into the variable z
. Finally it clears the screen.
The second if/then in the forever
block checks the current acceleration in the z axis against the value stored in the variable z
and if it is less than the stored reading by a margin of 30, it sets the variable alarm triggered
to be true.
The final part of the code determines what happens when the alarm triggered
variable is true. If the alarm is triggered, then the cross symbol will be shown on the display otherwise the display will be cleared. The alarm triggered
variable will remain true until button A is pressed again.
from microbit import * z = 0 alarm_triggered = False while True: if button_a.was_pressed(): # Wait for 2 seconds while the alarm is armed display.show(Image.TRIANGLE) sleep(2000) z = accelerometer.get_z() # take a reading while still alarm_triggered = False # the alarm is now armed # Check to see if acceleration on z axis changed by 30 if accelerometer.get_z() < z - 30: alarm_triggered = True if alarm_triggered: display.show(Image.NO) # display a cross else: display.clear()
The code is almost a line by line copy of the JavaScript Blocks code, but translated into Python.
Arduino
The Arduino version of the program is listed below and contains a few differences from the other two versions of the code.
#include "Wire.h" #include "MMA8653.h" #include <Adafruit_Microbit.h> const int buttonA = 5; // the number of the pushbutton pin const uint8_t triangle[] = { B00000, B00100, B01010, B11111, B00000 }; Adafruit_Microbit_Matrix microbit; MMA8653 accelerometer; int z = 0; boolean alarmTriggered = false; void setup() { pinMode(buttonA, INPUT); microbit.begin(); // start the display accelerometer.begin(false, 2); // 8-bit mode, 2g range } void loop() { accelerometer.update(); if (digitalRead(buttonA) == LOW) { // Wait for 2 seconds while the alarm is armed microbit.clear(); microbit.show(triangle); delay(2000); z = accelerometer.getZ(); // take a reading while still alarmTriggered = false; // the alarm is now armed } // Check to see if acceleration on z axis changed by 5 if (accelerometer.getZ() < z - 5) { alarmTriggered = true; } if (alarmTriggered) { microbit.clear(); microbit.show(microbit.NO); // display a cross } else { microbit.clear(); } }
The main thing about the Arduino version of the code is that there are no predefined triangle symbol to use with the display and so you have to define this yourself as the triangle
byte array.
Each byte array is an array of 5 bytes each of eight bits. Each bit can be either on or off (1 or 0). If the bit is 1 then the LED in that position will be lit otherwise it won't. If you look carefully at the byte arrays triangle and cross, you can just make out the bit pattern.
The setup
function (like the on start block in JavaScript Blocks) is only run when the micro:bit starts or is reset. In this case the button A pin has to be defined as an input explicitly. The microbit.begin
method starts the refreshing of the micro:bit's screen.
The loop
function will be run repeatedly and does actually work just like the JavaScript Blocks and MicroPython code.
Text editor powered by tinymce.