Make a Motion Detector

Don’t like people sneaking up on you? Not a fan of humidity? Enjoy sounds? What a happy coincidence, because boy do we have an oddly specific and mildly contrived micro:bit project for you! In this project, we’ll learn how to use the motion sensor, the moisture sensor, and the buzzer.

This tutorial was contributed by Luo Beiyu from the National University of Singapore.

Goals

  • Get to know the PIR Sensor Brick and moisture sensor
  • Make something with a PIR Sensor Brick
  • Make something with a moisture sensor

Materials

1 x BBC micro:bit
1 x Micro USB cable
1 x Breakout board
1 x Mini buzzer1 x Octopus PIR sensor Brick
1 x Moisture sensor
2 x Female-Female jumper wires

Step 1

  • After connecting one end of the USB cable to your computer, connect the other end to the micro:bit as shown in the picture
  • Connect the side of the micro:bit where the pins are located to the breakout board
  • Connect 2 wires to the buzzer
  • The pin under the ‘+‘ sign on the buzzer (usually the longer one) will connect to the yellow pin on the break out board, while the other pin (usually the shorter one) connects to the black.

Step 2

  • Plug in the buzzer to Pin 0 (the pins beside the number ‘0’ on the breakout board) as shown on the left.
  • Plug in the moisture sensor to Pin 3as shown on the left
  • Plug in the motion sensor to Pin 1 as shown on the right
  • Make sure the colour of the wire of the buzzer and the ADKeyboard follows the colour of the pins on the breakout board

Step 3 – Pre-coding

We’ll need to add a package of code to be able to use our kit components. Click on Advanced in the Code drawer to see more code sections and look at the bottom of the Code Drawer for Add Package.
This will open up a dialog box. Do a search for tinkercademy or just tinker. Click on the search icon  or press enter, then click on tinkercademy-tinker-kit.

Note: If you get a warning telling you some packages will be removed because of incompatibility issues, either follow the prompts or create a new project in the Projects file menu.

Click on Tinkercademy or OLED inside the Code Drawer to find our custom blocks for the various components in your kit

Step 4

      • In this step, we will code the Micro:bit with Block Editor
      • We begin by coding a starting screen, by placing the Show Icon block under the On Start block as shown in the picture on the right.
      • This causes the icon to appear on the screen whenever the micro:bit is powered on.

Step 5

      • Next, let’s create some music using the moisture sensor values
      • Select the Play Tone block under the code section Music and place the Value of Moisture Sensor code block in it
      • The pitch can be adjusted by multiplying the sensor values by different numbers, as shown in the image on the left.

Step 6

      • Finally, make the buzzer produce sound when the motion sensor detects movements. The micro:bit will only show an icon on the screen if there is no movement.
      • This can be done by using a conditional (if-then-else) statement and inserting the relevant blocks in the appropriate spots, as shown in the picture on the right.
Success! You now have your very own Micro:bit motion detector! 🙂