< Return to Video

Simple door alarm coding video

  • 0:01 - 0:05
    To make my door alarm, I've got a new MakeCode 
    project. Let's get rid of the 'on start' block
  • 0:05 - 0:10
    but keep the 'forever' block because we're going 
    to keep checking the magnetic field measured by
  • 0:10 - 0:18
    the micro:bit's magnetometer, its compass. Go to 
    'Logic' and find the 'if' block and drag that in,
  • 0:18 - 0:23
    and also in 'Logic' we'll find the comparison 
    block, so get that hexagonal block and drop
  • 0:23 - 0:30
    it on top of 'true'. Then, if we go to 'Input' 
    and click on 'more', you'll find a block called 'magnetic force'.
  • 0:30 - 0:37
    Drop it in the first hole in 
    the comparison block, and we're going to change it
  • 0:37 - 0:43
    so that it measures the overall strength of the 
    magnetic force. Your micro::bit's magnetometer can
  • 0:43 - 0:47
    measure magnetic force in different dimensions, 
    but we're just interested in just how strong it is overall.
  • 0:47 - 0:52
    And we're going to pop a number 
    in here that we might need to change later but
  • 0:52 - 0:56
    I'm just going to pick 200 for the moment. It 
    measures the strength of the magnetic field
  • 0:56 - 1:02
    in a unit called 'microTeslas' and I'm going to 
    try 200 to start with. If the magnetic field
  • 1:02 - 1:09
    falls below 200 microTeslas, I'm going to show an 
    angry face because what that will mean is that the
  • 1:09 - 1:15
    magnet has gone away from my micro:bit, somebody's 
    opened the door, and we want to know if someone's
  • 1:15 - 1:21
    been in the room so it's going to show an angry 
    face on the display. How do I know what number
  • 1:21 - 1:24
    to pick for the magnetic force? How do I know 
    how strong my magnet is? Well, we're going to
  • 1:24 - 1:30
    do something to sort that out. So, if I go 
    to 'Input', I drag in 'on button A pressed',
  • 1:31 - 1:37
    and we'll add a 'show number' block. Let's copy 
    the magnetic force block if I right-click on it,
  • 1:37 - 1:43
    duplicate, and drag it in here and drop it there. 
    What this means now is that when I press button A,
  • 1:43 - 1:48
    I can measure the amount of magnetic force 
    measured by the micro:bit. I can see it on the display.
  • 1:48 - 1:54
    So, we're ready to try this 
    out now. It doesn't work in the simulator,
  • 1:54 - 1:58
    so we're going to put this on a real micro:bit. 
    So, I'm going to call it 'door alarm',
  • 1:58 - 2:05
    and I'm going to download the hex file, and 
    transfer it to my micro:bit. I've fixed the
  • 2:05 - 2:09
    micro:bit and the battery pack to the door frame 
    using something like Blu Tak and put the magnet
  • 2:09 - 2:14
    on the door itself, very close to it. Have a look 
    on the back of the micro:bit board and see where the compass is.
  • 2:14 - 2:19
    You need to make sure the magnet 
    is close to the compass chip on the back of your micro:bit.
  • 2:19 - 2:27
    Now we can calibrate it. With the door 
    closed, press button A and get a reading of
  • 2:27 - 2:35
    the magnetic field strength. In my case, it's around 
    453 microTeslas. I'm then going to open the door,
  • 2:35 - 2:42
    press button A again and take a reading with the 
    door open. It's 129 microTeslas now.
  • 2:42 - 2:46
    It can still measure some magnetism, don't forget the Earth 
    is magnetic, and there are other things probably
  • 2:46 - 2:52
    in my house that are magnetic as well, so the 
    micro:bit is still picking up some magnetic field.
  • 2:52 - 2:58
    So my number of 200 should work pretty well. If 
    it's more than 200, it means the door is shut,
  • 2:58 - 3:04
    it sounds likely. Because with the door shut 
    that's 453 microTeslas. Below 200, that probably
  • 3:04 - 3:09
    means somebody has opened the door, in this 
    case I know when my door is open I have 129 microTeslas.
  • 3:09 - 3:14
    So that should work OK now, I should 
    be able to tell when someone has opened my door,
  • 3:14 - 3:19
    an angry face will appear on my micro:bit's 
    display, and I'll know that somebody has been in my room.
  • 3:19 - 3:23
    There are lots of things you could do 
    to this project to augment it. You could perhaps
  • 3:23 - 3:28
    have a delay to give yourself some time to get 
    out the room. Remember if you need to reset it,
  • 3:28 - 3:33
    you could press button A to show the magnetic 
    force again and that will clear the angry face
  • 3:33 - 3:38
    off the display of your micro:bit. Maybe you 
    could add an audible alarm too or
  • 3:38 - 3:45
    you could add a music block so that when somebody opens 
    your door, it makes an audible sound to warn you
  • 3:45 - 3:52
    that somebody has been inside your room. There are 
    all sorts of ways you could modify this project.
Title:
Simple door alarm coding video
Description:

more » « less
Video Language:
English
Team:
Microbit_Educational_Foundation
Project:
Make it: code it
Duration:
03:52

English, British subtitles

Revisions