<< Back to activities

Binary's UFO (micro:bit) kit - Monitoring for night

This activity shows how to use Microsoft Make code - drag and drop blocks to detect light levels when button a is pressed.

Start >>

Step 1 - Wiring up Binary's UFO

Firstly wire up Binary's UFO to the BBC micro:bit as shown below:

Next >>

Step 2 - Access the BBC micro:bit editor to be able to "code" on the micro:bit

When you are coding using the BBC micro:bit, you need to do it through their online editor.

Click on this link http://microbit.org/ to access the BBC micro:bit website.

When it loads, click on "Let's Code" shown in the image below at the top op the page.:

 

 

 

Next >>

Step 3 - Start a new project using the 'JavaScript Blocks Editor (PXT)'

There are lots of different types of editors you can use to program the BBC micro:bit. We are going to use the 'Makecode Editor' for this project. Click on the 'Let's Code' button shown below.

 

 

Next >>

Step 4 - Set up your project

To begin setting up your new project, click on the 'New Project' button

 

 

 

When the new project loads you will see two blocks on the page, an 'on start' and 'forever' block. Delete these blocks by dragging them over the tabs to the left

 

  →  

 

Next we need to change the name of the script, click in the 'Untitled' box located next to the download button at the bottom of the page. This will allow you to change the name to something meaningful i.e. UFO Monitoring for night

 

  →  

 

 

Next >>

Step 5 - Start to Code

  • Click on the 'Basic' tab and select the 'forever' loop


The 'forever' loop will always repeat any statements and/or blocks located inside of it, unless it is interrupted.

  • Drag the 'forever' loop over onto the blank page


Click on the ‘Variables’ tab, select the ‘Make a Variable’ option and name the variable as ‘ brightness’

 




The variable you have named has now been created. Select the ‘set brightness to 0’ block and drag it in-between the ‘forever’ block

 → 


Next click on the ‘advanced’ tab, then the pins tab. Select the ‘analog read pin P0’ block, drag and snap it onto the right of the ‘set brightness’ block

 → 


Change the pin being read by clicking on the 'P0' dropdown menu and selecting the pin you have connected the temperature sensor to

   

Next >>

Step 6 - Add an 'If' block to display different things depending on Light or Dark

This stage introduces a loop that goes on forever. Within the loop, we will be using an 'if' statement coupled with a variable called 'brightness' from the previous step - this registers the value being read by the sensor.

  •  Click on the 'Logic' tab, select an 'if' statement and drag it underneath the 'set brightness to' block

  


  • Again, click on the 'Logic' tab, select the '0 < 0' block, drag and snap it to the right of the 'if' statement

   


  •  Click on the 'Variables' tab, select the 'brightness' variable, drag and snap it to where the first '0' is in the '0 < 0' statement

   


  • Next click on the '<' symbol and change it to a '≤' symbol, also you will need to input a variable where the '0' is located in the 'brightness < 0' block - this works best if the variable is quite low, e.g. 25 or under




  • Click on the 'Basic' tab, select the 'show string' block and drag it into the 'do' section of the 'if' statement

  → 


  •  Click on the 'Hello!' located in the 'show string' block and change it to 'BLINDING!'


  •  Click on the 'Basic' tab, select the 'show number' block and drag it underneath the 'show string' block

   


  •  Click on the 'Variables' tab, select the 'brightness' variable, drag and snap it over the '0' in the 'show number' statement




  •  Next click on the 'Logic' tab, select the 'if' statement and drag it underneath the first 'if' statement

  → 


  •  Click on the 'Logic' tab, select the 'and' statement, drag and snap it into place to the right of the second 'if' statement

  → 


  •  Click on the 'Logic' tab and select the '0 > 0' statement, drag two of these to replace the blank spaces in the 'and' statement

  → 


  •  Next change the '=' symbol to '≤' on the second '0 > 0' statement




  • Click on the 'Variables' tab, select two 'brightness' variables, drag and replace the first '0' on both the '0 = 0' and '0 ≤ 0' statements




  • Next you will need to change the variables in the 'and' statement to your desired numbers - this works better if one of the numbers is below 200


  • Next click on the 'Basic' tab, select the 'show string' block, drag it into the 'then' section of the second 'if' statement




  •  Click on the 'Hello!' located in the 'show string' block and change it to 'NICE!'


  • Click on the 'Basic' tab, select the 'show number' block and drag it underneath the 'show string' block in the second 'if' statement

   


  •  Click on the 'Variables' tab, select the 'brightness' variable, drag and snap it over the '0' in the 'show number' statement




  • Click on the 'Logic' tab and select the 'if' statement and drag it underneath the second 'if' statement




  •  Click on the 'Logic' tab, select the '0 = 0' statement, drag the statement to the right of the third 'if' statement

  → 


  •  Click on the '=' symbol and change it to '≥'




  • Click on the 'Variables' tab, drag a 'brightness' variable and snap it over the first '0' of the '0 ≥ 0' statement




  • Change the value of the second '0' to the next number up from your previous value in the 'brightness ≤ 'value'' statement


  •  Next click on the 'Basic' tab, select the 'show string' block and drag it into the 'then' section underneath the third 'if' statement




  •  Click on the 'Hello!' located in the 'show string' block and change it to 'DARK'


  • Click on the 'Basic' tab, select the 'show number' block and drag it underneath the 'show string' block in the second 'if' statement

   


  •  Click on the 'Variables' tab, select the 'brightness' variable, drag and snap it over the '0' in the 'show number' statement



Next >>

Step 7 - Adding Sound

This activity follows on from the previous step

 Click on the 'Music' tab, select the 'play tone' block, drag and drop the block underneath the 'show number brightness' block in the first 'if' statement 

   


Repeat the previous stage for each of the 'if' statements


Next change the 'play tone' and 'beat' section for all the 'play tone' blocks - these should all be different

Next >>

Step 8 - Ready to test!

You are ready to test your code !! You can do this before you put it on Binary's UFO. On the left side is a 'Virtual' micro:bit - its a version of the real thing that you can use to test. 

This will automatically update it self. 

 

 

Next >>

Step 9 - Does it work?

To test the code properly, you will need to compile and download the hex file to the micro:bit (see stages 11 - 14)

 When you run the code on the virtual micro:bit, it will only display 'BLINDING' and play the tone associated with that reading - this is due to the virtual micro:bit not having a virtual light sensor

Next >>

Step 10 - Download the code ready for Binary's UFO

 

It is now time to see it work on Binary's UFO. 

 

On the bottom left of the editor is a button that says 'Download' - shown below in a red box

 

Next >>

Step 11 - Find your downloaded code file

Click on the downloaded file at the bottom of the browser - select 'Show in Folder'

This will open the folder containing all your downloaded files - this will include a file called something like 'microbit-UFO-monitoring-for-night'

Right click on the file and choose to copy it (this is ready for the next step)

 

 

Next >>

Step 12 - Plug in the micro:bit

Now you need to plug the micro:bit on Binary's UFO, into your computer - using the USB cable that came with the micro:bit.

When you plug it in, a window should pop up on screen that looks a bit like this:

 

 

Next >>

Step 13 - Paste the code file to the micro:bit

Right click on a blank space within the micro:bit folder and choose 'Paste' - it will display a window a bit like the below as it copies the file to the micro:bit.

 

 

Next >>

Step 14 - See what Binary's UFO thinks to the Dark!

Next >>