This Instructable demonstrates how to detect and respond to motion, using the micro:bit, a popular microcontroller.
We will show how Roger Wagner’s MakerBit can connect the three motion-activated sensors from the Elegoo 37 Sensors Kit to a micro:bit. These three sensors left us feeling somewhat disappointed. No problem! The article will demonstrate some of the micro:bit’s remarkable, built-in motion sensors, also.
Finally, we will see how to write code directing the MakerBit to send signals and initiate actions when motion is detected.
After doing these experiments, a developer should be able to identify the type of motion that each sensor can detect, and to describe differences in how the different sensors work.
Step 1: Gather the Materials
- Roger Wagner’s MakerBit by 1010 Technologies
- Ribbon cable with 12 wires (supplied with the MakerBit)
- LED (supplied with the MakerBit)
- Three jumper wires with female connectors on both ends (supplied with the MakerBit)
- micro:bit controller
- USB cable to connect micro:bit with your computer
- Elegoo Tilt Switch module
- Elegoo Tap Switch module
- Elegoo Shock Switch module
- Optional: 6 to 12 volt external power supply with round pin to fit MakerBit (for example, the battery compartment supplied with the MakerBit)
Step 2: Connect a Sensor
We will start with the Elegoo Shock module, because it worked for us more consistently compared to the other two modules in the 37 Sensor kit.
Snap the micro:bit into the MakerBit with its built-in LED display is on the top side.
Locate the tri-color header on the MakerBit that is labeled for pins P5 through P16. Connect the three jumper wires to the black (ground), red (5v power), and white (signal) posts in the group labeled “P8”.
Hold the Shock module so the pins point down and the prominent, black cylinder-shaped object is on top, facing you. Notice the pin on the left has a letter “S” beside it. This is the signal pin. Connect the signal pin to the jumper wire that goes to the white pin (P8) on the MakerBit. Connect the middle pin to the red (power) pin on the MakerBit. Connect the right-hand pin to the black (ground) pin.
All three of the Elegoo motion-switch type of sensor modules mentioned in this article attach the same way.
Step 3: Connect the LED
Examine the 12-wire ribbon cable. Hold it so the six, small connectors point up and the brown wire is on the outside, on the left side. This brown wire and the orange one next to it will get connected to pin P16. Push an LED into the small connector for this wire pair. Important: line up the SHORT post on the LED with the small triangle on the connector. Plug the 12-wire ribbon cable into the black header labeled “LEDs 11-16” on the MakerBit.
Why Use an External LED When the micro:bit Has Them Built in?
Maybe you want to hide the controller inside a housing or behind a poster? An LED can connect directly to the MakerBit ribbon cable without the need for a resistor. This way, the light can be mounted at a distance from the controller, wherever you choose to display it.
Step 4: Power Up
Connect the external power supply to the MakerBit’s round barrel connector. Connect the USB cable to the micro:bit and to the computer being used to program it.
It is OK to connect both the external power source and the USB cable. The MakerBit uses the USB only for data communication in this configuration, while power for the pins and the motor controller comes from the external supply.
Step 5: Get the Motion Switch Code
This Instructable will use the online, browser-based editor for micro:bit called Microsoft MakeCode. We make code available to you several different ways.
You can open the Microsoft MakeCode editor in a browser window with the code loaded ready to run, by clicking this link: https://makecode.microbit.org/_VCcasuRms1pb
Compile the code and upload it to the MakerBit. Generic instructions for doing this are available from the official micro:bit web site at this link: https://makecode.microbit.org/device/usb
Step 6: Test the Motion Switch Modules
Shake the wire that holds the Shock sensor module. If all goes well, the LED will flash in response to your shakes. You might have to shake it rather hard.
Try it with the other two modules also: the Tap module and the Tilt (or Roll) module. Simply unplug one and plug in another. Tap, or tilt, as the case may be. We hope the modules in your kit work better than the ones that we found in our kit.
The three motion-detection switch sensors in our Elegoo 37-Sensor kit were not as sensitive as we would like. We tried them with several other, different microcontrollers, including Arduinos. The switches operated only when the motion was extremely large. These three switches would not be our first choice for sensing relatively small degrees of motion.
Happily, we have another way to sense motion with a MakerBit.
Step 7: Micro:bit to the Rescue
Fortunately, MakerBit interfaces with a micro:bit, which comes with a rich set of very sensitive motion detectors built-in. The micro:bit can detect many kinds of motion very precisely. It reports motion through a set of blocks found in the “Input” group of the Microsoft MakeCode online editor. Official documentation for the Input group is available at this link: https://makecode.microbit.org/reference/input
We will explore a couple of the ways micro:bit can detect motion.
Disconnect the Elegoo sensor from the MakerBit. Leave the ribbon cable connected.
Code Example #1 – Shake
This link will open the MakeCode editor in a browser and load the example code:
Shake the MakerBit vigorously. If all goes well, the LED attached to the ribbon cable will light up briefly. Then it will go dark, until you shake the MakerBit again.
This example takes advantage of an intermediate coding concept called an Event Handler. In the Blocks view of the MakeCode editor, examine the block that starts with “on shake”. Notice that we do not have write code for detecting a shake. The micro:bit automatically notifies the program when a shake event occurs. All we need to do is put code inside the shake event handler telling it how to respond when shaken.
Step 8: A Coding Challenge: Change of Position
The micro:bit contains an accelerometer, which can detect more than just motion. It can sense gravity, which lets it know how the device is tilted even if it is sitting still. It senses both pitch (forward/backward angle) and roll (left/right angle).
The code example lights the LED after the MakerBit changes pitch or roll by more than 10 degrees compared to the average over the prior ½ second.
This link will open the MakeCode editor in a browser and load the example code.
This example uses intermediate coding concepts including:
- Arrays used to hold and update the five most recent position readings
- A named function that does most of the work:
- Reading the accelerometer
- Updating the recent average position of the MakerBit
- Coding students: study how this function updates an average by replacing only one number in an array. Can you think of another way to do this?
The main loop simply compares the current position to the recent average position. If there is difference of more than 10 degrees in either pitch or roll, the LED lights up for ½ of a second.
Step 9: Some Questions for Investigators to Discuss and to Explore Experimentally
What ways can you think of to use a sensor like this in the real world?
Can you tilt the MakerBit slowly enough to change its position without turning on the LED?
How fast does the pitch or roll have to change to make the light turn on?
What else can you find out about the code just by tilting the MakerBit and watching what the light does?
Can you explain the behavior you observe, of the LED, based on the code?
How could you change the way the light behaves by changing the code slightly? Experiment! Predict a change in how it functions, then change the code, then see if the function changes the way you predicted.