14. Lesson 12 Accelerometer

../../_images/5RJ1KJn.jpg

14.1. Introduction


micro:bit has integrated multiple sensors including accelerometer. Today, we are going to use accelerometer to make a level device and display the inclination on NeoPixel ring in bar chart format.

14.2. Component List


Hardware:

  • 1 x micro:bit Board

  • 1 x USB Cable

  • 1 x micro:bit Breadboard Adapter

  • 1 x Transparent Breadboard - 83 * 55 mm

  • 1 x 8 RGB LED NeoPixel Ring

  • n x Breadborad Jumper Wire 65pcs Pack

Tips: If you want all components above, you may need Elecfreaks Micro:bit Starter Kit.

../../_images/W4tseua.jpg

14.3. Major Component Introduction


Accelerometer

There is an accelerometer on your micro:bit which detects the speed change of micro:bit. It converts analog information into digital form that can be used in micro:bit programs. Output is in milli-g. The device will also detect a small number of standard actions, e.g. shake, tilt and free-fall.

../../_images/kzqAOK4.jpg

The corresponding X, Y, Z axle direction of accelerometer are showed below:

../../_images/FQ6zBkH.jpg

14.4. Experimental Procedure


Hardware Connection

Connect your components according to the picture below:

  • Connect the signal wire of the LED ring to the P0 port of the breadboard adapter.

../../_images/NPvcrUo.jpg

After connection, we can see:

../../_images/SOD2TLb.jpg

Software Programming

Click to open Microsoft Makecode, write the following code in the editor.(https://makecode.microbit.org/)

../../_images/JHZUvh2.png

Add Package

Click “Advanced”in the choice of the MakeCode to find more choices.

../../_images/smtcNoB.png

Click “Extensions”, search “neopixel”in the dialog box and then download the “neopixel”.

../../_images/umQwUC2.png

Program as the picture shows:

../../_images/bGPUFxy.png

Details for the code:

  • Set P0 port as the pin for LED beads and set it in RGB mode, then light on all the LED.

../../_images/NcVWUhr.png

  • Set the color of the light is changed with the accelerated speed.

../../_images/GBBTLVt.png

Reference

Links:https://makecode.microbit.org/_0Y07f36Y77sa

You can also download the links directly:

---

14.5. Result


The RGB LED ring lights on in different color with the movement of the micro:bit.

../../_images/iq9Hxs0.gif

14.6. Exploration


If we want to set 4 of the LEDs to light on in turns, how can we design the circuit and program?

14.7. FAQ