Laser Head Sensor with micro:bit


A laser head sensor module is one of many useful external components that you can connect to your micro:bit!

In this guide, you will learn to connect the micro:bit with a laser head sensor module and create your own tripwire alarm system. With the addition of a light dependent resistor, the alarm will start to sound when the laser is broken.

By finishing this guide, you will have created a simple tripwire alarm system.

Parts Used in This Guide


Step 1  The Module

Let's take a closer look at the laser head sensor module.

It has three pins:

  • GND: Though it is labelled '-' on the module, this is the the 'GND' pin. In electronics, we define a point in a circuit to be a kind of zero volts or 0V reference point, on which to base all other voltage measurements. This point is called ground or GND.
    • Note: Voltage is the difference in electric potential between two points. As it is difficult to talk about voltage without a reference point, we need another point to compare it to.
  • Middle Pin: No connection required here
  • Signal: This pin is the signal pin, which is the input to control the module 

Step 2  Connect module to breadboard

Step 3  Connect P2 to S

Step 4  Connect GND to -

Step 5  Connect + to VCC (Buzzer module)

Step 6  Connect GND to GND (Light-dependent resistor module)

Step 7  Connect P1 to AO

Step 8  Connect + to VCC

Step 9  Connect P0 to I/O

Step 10  Connect GND to GND (Buzzer module)

Step 11  Connect + to 3.3V

Step 12  Code for button A and B!

Now that we have connected the laser head module to the micro:bit, we will program it! We will use the two push buttons on the micro-bit to turn the laser on and off. 

  • Open up MakeCode editor
  • Click on the 'Projects' button then click on 'New Project ...'
  • From the 'Input' tab, grab two 'on button A pressed' blocks.
  • Then click on the 'Pins' tab to grab two 'digital write pin' blocks.
  • Arrange them so that:
    • When button A is pressed, pin 2  is set to a value of 0. 
    • When button B is pressed, pin 2 is set to a value of 1. 

Upload this code to the micro:bit and press button A and B to see what it does!

 Note: These are the two buttons found on the micro:bit.

Step 13  Add the code for the buzzer!

We have also added a light dependent resistor module and a buzzer module to the circuit. Let's use them!

  • Pin 2 has been used to connect to signal (S) of the laser head module, while pin 0 is connected to input or output signal (I/O) of the buzzer module.
  • If sensorVal is more than 600, the micro:bit will display the value and then sound the alarm by playing a Middle C tone for 2 beats each time.
  • Else, the micro:bit will display the sensorVal when the laser is not broken.

Step 14  Add Some Visuals!

Let's change the code some more to add more visuals. The changes made:

  • When the trip wire alarm system goes off now, it will display an angry face using the micro:bit's LEDs. 
  • Otherwise, if all is well, it will display a smiley face on the LEDs.

Step 15  Send a message to a second micro:bit!

If you have another micro:bit laying around, use the following MakeCode, else skip to the next step!

  • We will use another micro:bit to receive a message when the trip wire alarm system goes off. 
  • Now when the laser is broken and the sensorVal goes above 600, the second micro:bit will display the string, "Intruder alert!".
  • This is done using "radio send string" and "on radio received". The two micro:bits can communicate with one another via radio.

Step 16  Upload the code to micro:bit

It's time to upload the code to the micro:bit!

  1. Connect the micro:bit to your computer by using a microUSB cable
  2. In MakeCode editor, click on the Download button 
  3. Find the hex file in your Downloads folder or where you have saved it to
  4.  Open up Finder on the MacOS or Explorer on Windows, and drag the hex file into MICROBIT under 'Devices' on the macOS.
  5. The micro:bit will flash for a few seconds and the trip wire alarm will be all set!