Microbit Intruder Alarm With Special Key

by karthik5768 in Circuits > Microcontrollers

512 Views, 2 Favorites, 0 Comments

Microbit Intruder Alarm With Special Key

FSUED7EKU9T4UK9.png

In this Instructable, I made a laser security alarm with microbit( its called the Microbit Intruder Alarm) which can be accessed with a special key. I previously made a laser security alarm with a special key using a Q-box board from STEM kit and later an arduino version of it. My microbit and some(I mean many) sensors were laying in the table for around a year and I thought using it. Unfortunately, my ir sensors were damaged when i tried making an intruder alarm, so I had to use a laser, but this time, I decided to add something new to it, like a special key which can be used to enter without making the alarm go off. In this tutorial I used a microbit with simple components and a simple block code to make the same with a special access key. This can be customised in any way, by adding servos for opening door, using a key with a hidden magnet inside to access the security mode with magnetic sensors, or use a RFID card to access it. In this tutorial, i decided to keep it simple with a slide switch.

Supplies

microbit2.png
micro3.png

List of Materials needed:

1x bbc Microbit (v1 or v2 both works)

https://tinyurl.com/Microbitv1 (v1) 1668 inr

https://tinyurl.com/micobit (v2) 2513 inr

1x piezo buzzer (15 to 100 inr)

1x green LED(good ones will cost around 20-50inr)

1x Laser(19-100inr)

1x sensor of any type (magnetic, RFID, ir sensors, slide switch and anything you want.)

(price based on the sensors. The sensors mentioned shouldn't cost you more than 200inr)

I was able to make this in less than 1600inr(I used a microbit which comes without the package which cost me 1499inr from amazon)

NOTE: ALL THE PRICES ARE BASED ON THE TIME OF MAKING THIS INSTRUCTABLE AND MAY VARY

The Circuit

microbit wiring.png

Step 1: The circuit was made in Tinkerad. You can refer to this website to see it.

Note: The red LED is to be replaced by a laser which is pointing towards the microbit's LEDs on the front side. The switch can be replaced with any kind of sensor with the same wiring diagrams. To keep it simple, i have added a slide switch. You can also add several features such as opening a door with a small servo motor.

The Diagram: The red wires are the wires which are connected to the positive terminal. The black wires are the wires which are connected to the negative terminal and the green wires recieve inputs or outputs(eg: The piezo's negative terminal is connected to pin 1 of the microbit which is used to produce a beep sound when the signal is passed).

The Code

microcode1.png

First, make a new variable called Locked, which we will use later to tell the microbit if the key is inserted and is in security mode(You can pass through the lasers without activating the alarm) or in protection mode(The piezo buzzer starts to beep when you brake the laser light passing).

Coding the Start

microcode2.png

Now, when we start our code, we set the analog pitch to pin 1 where we connected our piezo buzzer which can be used to make a beep sound and alert us. Then, we set the variable Locked to 0 (Indicating that it is in security mode and not protection mode) the show leds in the next line isn't nessasary, you can either avoid that step or make a boot up image type. And then we wait for a second and clear the display. But make sure you don't avoid the first two codes (set anolog pitch to pin 1 and the set Locked to 0)

Security Mode and Protection Mode

microcode3.png

Now, we will define our variables inside a forever loop. We add an if else block and inside 'if', we check the input from pin 2. If the reading is 1, meaning that the switch or the sensor is on, we set the Locked variable into protection mode(You can not pass through the lasers) And set digital pin 0 (The laser) to high, meaning it will glow brightly. And inside else block(if the user inserted the key) we set the laser to off and we set Locked to 1.

The Main

microcode4.png

Now, we will make the main code in side a forever loop. We add an if else block. inside the if block, We use the inbuilt light sensor on microbit(The 25LEDs itself are the used as the light sensors) to see if the light is below 5 and locked is set to 0. And if its true, the microbit will display a cross sign and start beeping(using piezo) until you press a. If the light is braked, you will have to insert the key and then press a to stop the beep sound. The reason we added the until block is that we do not want to stop the alarm after the light is broken. In the else block, when the light level is above 5, or the light level is below 5 and the Locked is 1(security mode) the microbit shows a tick sign.

Code in Python

This is the python code; in case you want to copy it.

# Python code
#
Locked = 0
pins.analog_set_pitch_pin(AnalogPin.P1)
Locked = 0
basic.show_leds("""
  # # # # #
  # # # # #
  # # # # #
  # # # # #
  # # # # #
  """)
basic.pause(1000)
basic.clear_screen()


def on_forever():
  global Locked
  if pins.digital_read_pin(DigitalPin.P2) == 1:
    Locked = 0
    pins.digital_write_pin(DigitalPin.P0, 1)
  else:
    Locked = 1
    pins.digital_write_pin(DigitalPin.P0, 0)
  if input.light_level() < 5 and Locked == 0:
    while True:
      basic.show_leds("""
        # . . . #
        . # . # .
        . . # . .
        . # . # .
        # . . . #
        """)
      pins.analog_set_pitch_volume(255)
      pins.analog_pitch(225, 500)
      if input.button_is_pressed(Button.A):
        break
  else:
    basic.show_leds("""
      . . . . #
      . . . # .
      # . # . .
      . # . . .
      . . . . .
      """)
basic.forever(on_forever)


Result

Microbit intruder alarm with special key access

Here is a video about how it works

Please Note that the red led will be used as a laser pointing towards the microbit.