How to Detect Motion With Roger Wagner's MakerBit and the Elegoo 37 Sensors Kit

Intro: How to Detect Motion With Roger Wagner's MakerBit and the Elegoo 37 Sensors Kit

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

We store the JavaScript version of the same code in a publicly available repository at github. The code listing appears below, with a link to the relevant file at github.com. If you know what you are doing, you can copy the code and paste it into the JavaScript side of the MakeCode editor.

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

Motion_Switches

// ****************************************************************
// Motion Switches | MakerBit Example Code
// To demonstrate using a micro:bit controller with the
// Tilt, Shock, and Tap modules in the 37 Sensors Kit V 2.0
// from Elegoo
//
// Copy and paste into the JavaScript side of the Microsoft
// MakeCode browser-based editor for micro:bit.
//
// by David Sparks June 14, 2018
// ****************************************************************
//
// Turn off the LED at the Start
pins.digitalWritePin(DigitalPin.P16, 0)
// The main loop checks the switch connected to pin P8
// and operates the LED.
basic.forever(() => {
if (pins.digitalReadPin(DigitalPin.P8) >=1)
{
// the switch closes when the module detects motion
// turn on the LED
pins.digitalWritePin(DigitalPin.P16, 1)
basic.pause(200)
}
else
{
// the switch opens when the module is not sensing motion
// turn off the LED
pins.digitalWritePin(DigitalPin.P16, 0)
basic.pause(200)
}
})

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:

https://makecode.microbit.org/_avmK1mME49cf

The JavaScript code is available on github, and the listing appears below. Compile the code and upload it to the MakerBit.

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.

Shake Event Example Code

// ********************************************
// MakerBit Example | Detect Shake Event
//
// Lights an LED connected to Pin P16
// for 1/2 of a second
// after micro:bit raises a Shake event.
//
// Paste into the JavaScript area of the
// Microsoft MakeCode online blocks editor.
//
// by David Sparks June 14, 2018
// ********************************************
//
// This code detects and handles the Shake event
input.onGesture(Gesture.Shake, () => {
pins.digitalWritePin(DigitalPin.P16, 1)
basic.pause(500)
pins.digitalWritePin(DigitalPin.P16, 0)
})
//
// This code will be in the Start block
pins.digitalWritePin(DigitalPin.P16, 0)
//
// The main loop actually does nothing
// because the action all takes place
// in the event handler, above.
basic.forever(() => {
})
view rawShake_Event.ts hosted with ❤ by GitHub

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.

https://makecode.microbit.org/_gYyfjzHDaLD3

The JavaScript code is available on github, and the listing appears below. You can copy and paste it into a new project in the MakeCode editor. Compile the code and upload it to the MakerBit.

This example uses intermediate coding concepts including:

  • A start-up loop to establish the initial position of the MakerBit. Coding students: notice that the JavaScript for this loop starts with “for” and the corresponding block starts with “repeat”. Study both formats until you are satisfied that they both state the same instructions.
  • 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.

Position_Change.ts

// ****************************************************************
// Position Change | MakerBit Code Example
//
// Track the average value of pitch or roll obtained from the
// built-in micro:bit accelerometer over the prior 1/2 second.
// Turn on LED for 1/2 second after detecting difference of
// more than 10 degrees in either axis compared to the average.
//
// Copy and paste this code into the JavaScript side of the
// Microsoft MakeCode browser-based editor for micro:bit.
//
// by David Sparks June 14, 2018
// ****************************************************************
//
// Declare variables used in the program
let averageRoll =0
let averagePitch =0
let arrayIndex =0// used to select specific values in arrays
let rolls:number[] = []
let pitches:number[] = []
// Declare named function
function readRotation() {
// update average pitch
averagePitch=averagePitch*5-pitches[arrayIndex]
pitches[arrayIndex] =input.rotation(Rotation.Pitch)
averagePitch= (averagePitch+pitches[arrayIndex]) /5
// update average roll
averageRoll=averageRoll*5-rolls[arrayIndex]
rolls[arrayIndex] =input.rotation(Rotation.Roll)
averageRoll= (averageRoll+rolls[arrayIndex]) /5
// update index into the pitch and roll arrays
if (arrayIndex>=4) {
arrayIndex=0
} else {
arrayIndex=arrayIndex+1
basic.pause(100)
}
}
// This code appears in the Start block
let counter =0
arrayIndex=0
rolls= [0, 0, 0, 0, 0]
pitches= [0, 0, 0, 0, 0]
for (counter=0; counter<5; counter++) {
readRotation()
}
// The main loop compares the current pitch and roll to the averages
// and switches the LED on or off based on the amount of difference.
// Then it calls the named function to update the averages.
basic.forever(() => {
if (Math.abs(averagePitch-input.rotation(Rotation.Pitch)) >10
||Math.abs(averageRoll-input.rotation(Rotation.Roll)) >10)
{
pins.digitalWritePin(DigitalPin.P16, 1)
basic.pause(500)
}
else
{
pins.digitalWritePin(DigitalPin.P16, 0)
}
readRotation()
})

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.

Share

    Recommendations

    • Fix It! Contest

      Fix It! Contest
    • Audio Contest 2018

      Audio Contest 2018
    • Metalworking Contest

      Metalworking Contest

    Discussions