Introduction: Micro:bit Robot Control With Accelerometer
In this article we're going to use TinkerGen's BitCar kit to build a Micro:bit robot and control it using accelerometer on another Micro:bit board.
BitCar is a micro:bit based do-it-yourself robot designed for STEM education. It's easy to assemble, easy to code and fun to play with. The car uses two high quality metal gear motors to drive the wheels, which have stronger force and longer lifespan than normal plastic gear motors. The car board integrates a buzzer for music or audio signals, 2 line follow sensors for tracking lines, and the 4 addressable LEDs on the bottom can be used as indicators, highlights or simply for cool decorations. There are also Grove connectors for add-ons like ultrasonic sensor, Al camera, recognizer or screens. All features of BitCar can be easily controlled with Microsoft MakeCode editor.
Step 1: Assembly and Preparation
Start by installing front and back castor wheels using M3x6 screws.
Then install battery holder on 3M sticker, try installing battery holder as close as possible to back castor wheel.
Put the wheels on motor shafts and attach acrylic plates in the order specified in assembly pictures above.
Finally insert Micro:bit and (optional) Ultrasonic Senor.
To use BitCar with Microsoft Makecode, you need to add an extension to the interface. For that, got to makecode.microbit.org, click on Advanced-Extensions and then paste this URL in search field: https://github.com/TinkerGen/pxt-BitCar. After adding the extension, you should see new tabs appearing: BitCar and Neopixel.
Step 2: Program the Controller Micro:bit
We'll begin by adding set radio group to 1 to on start block. Also we'll make LED to show smiley face to know that our program is actually working and didn't throw any exceptions. Next we need to read the data from the accelerometer and do some data conversion: data from accelerometer comes as integer values ranging from -1023 to 1023, and motors on BitCar accept integer values from -100 to 100. We'll use map function to convert the values from one range to another and them round them to the nearest integer. After that the values are ready to be sent over the radio. Finally let's check if shake gesture is detected, and if it is, send the string "stand up" over Bluetooth. This is it for controller Micro:bit, next step is to write code for BitCar's Micro:bit.
Step 3: Program the BitCar's Micro:bit
The code for BitCar's Micro:bit will have two blocks: the first one responsible for main motion commands (forward-back-left-right) and the second one solely for "standing up". Within on radio received name value block we check if the received name is "y-axis" - it is forward-backward motion. We add another if condition there, to set some threshold for forward-backward motion, otherwise the movement comes out a bit jittery, because of the conflict with left-right motion executing at the same time.
If received name is "x-axis", we are receiving information about left-right movement, we check if it's less then 0. If it is negative, BitCar needs to go left, if it is a positive value, robot needs to go right. We then control the motors accordingly.
Another block that we have is on radio received receivedString - here we check if that string is "stand up" and if it is, then we give command to BitCar to stand up with speed 100 and charge 250 ms.
Step 4: Have Fun and Make It Your Own!
Upload this program(if you encounter difficulties, can also download it from our GitHub repository) to both Micro:bits and try it out!There are some further adjustments can be made, for example adding control for stand up parameters or adding music. It is also an interesting idea to use compass heading instead to make BitCar move in the same direction as the person holding it.
The possibilities are endless and implementing your own ideas in hardware and software is the soul of Maker movement. If you come up with some new and interesting ways to program BitCar, please do share in the comments below. Also, BitCar comes with an online course you can access at TinkerGen's online course platform, https://make2learn.tinkergen.com/ for free! For more information on BitCar and other hardware for makers and STEM educators, visit our website, https://tinkergen.com/ and subscribe to our newsletter.
TinkerGen has started a Kickstarter campaign for MARK(Make A Robot Kit), a robot kit for teaching coding, robotics, AI!