micro:bit door sensor

micro:bit door sensor

At Lancaster University I've been working on an energy project for schools.
Here's one of the experiments the students can reproduce using a micro:bit and a magnet.

https://www.youtube.com/watch?v=pBIzG7zMCUo

Equipment

1 x micro:bit
1 x battery pack with batteries
1 x magnet
Cellotape
A door

Code

Code the micro:bit before sticking everything to the door.

Find your range
The micro:bit can measure magnetic force strength. We need to know

  • the magnetic force strength when the magnet is next to the micro:bit?
  • the magnetic force strength when the magnet is as far away as the door opened
  1. Use this code and put the magnet next to the micro:bit.
  2. From Basic drag out "show number 0" and place it under forever
  3. From Input > More drag out "magnet force x" and place it on top of 0
  4. Change x to stength

range

When you download the code, the micro:bit will calibrate itself by asking you to draw a circle. Tilt the micro:bit around until you've drawn a circle using the lights on the front.

  1. Write down the number. Wait and check a few times until the number is consistent.
  2. Move the magnet away from the micro:bit.
  3. Record the number. Wait and check a few times until the number is consistent.

When the magnet was away from my micro:bit, the value was 40. Sometimes it was 39, 38. So here's my code to record the number of times a door is opened:

magnet

Instructions on how to recreate the code:

  1. From logic drag out "if else"
  2. From logic drag out "0 < 0" and drop it onto the if statement
  3. From input > more drag out "magnet force x" and drop it onto the first 0
  4. Change x to stength
  5. Change the second 0 to your magnet number
  6. From basic drag out a icon to indicate the door has been opened
  7. In the Variable menu create a new variable called doorOpened
  8. From Variables drag out "change item by 1"
  9. Change item to "doorOpened"
  10. From Basic drag out "show number 0"
  11. From Variables drag out "doorOpened" and drop it on the 0

I paused the micro:bit for 5 seconds (5000 milliseconds) to allow the door to close before recording another door open. How long does it take for your door to close? How often do people keep the door open for?

  1. From basic drag out "pause (ms) 100"
  2. Change 100 to the number of milliseconds it takes your door to close

Build

  1. Find a door that when opened decreases the temperature of the room
  2. Cellotape the magnet to the edge of the door where it opens
  3. Cellotape the micro:bit to the door frame
  4. Plug in the battery
  5. Open the door to test it's working

magnet_close_up

This experiment was used with others to try and help schools save money. A student would record the number of times the door was opened each day and reset it to start recording the next day. They would display the information each day and try to change behaviours so the door wouldn't be opened as much.

Related Article