Working With The Light Level Of BBC Micro - Bit


Today, I am writing about the light level of micro:bit. The micro:bit uses its LED array to detect the light level. In shadow, it reads the light level as 0 and whenever it senses light, it reads more than zero. Basically, the light level is used to detect how bright or dark the place is where our micro:bit is placed or where we are. And, the light level ranges from 0 to 255, where 0 means darkness and more than 0 to 255 means brightness. It uses LED arrays to detect the level of the light.
So let us see how we can access the light level using our micro:bit.
Tool You Need
  1. Micro:Bit(1 pcs)
  2. USB (1 pcs)
  3. Battery Box( 1pcs)
  4. AA battery (2 pcs) 
In this tutorial, there is no connection part since we're not working with any sensor or anything, so we just need a USB cable to connect micro:bit to the computer to upload the program. You need to go to makecode website as always and create a new project and follow the steps.
Go to the input block and then grab the light level.
Actually, light level is used to read the light level from the micro:bit and it returns the number from 0 to 255.
Now, go to variables and choose "set item to".
This function/block is used to set the value of a variable to some value. And if you want, then rename the item variable to level or whatever you want. I have renamed it as level and attached the light level to it. In this step, we're going to read the light level and map it to a variable so that we can do whatever we want to do with it. I have used on start block so it will display the light level only once -- if you want you can use forever block.
Now go to basic and choose show number block and attach the light level to it.
Now, download this code and upload it to your micro:bit. You will be able to see the light level.    
That's all you need to do. You can see a working demo here.
Now, what I am going to do is to plot the graph so that we can see the light level whenever our micro:bit reads we can see a graph on it.
So for that just go to LED block and grab plot bar graph.
Now attach the light level variable to the first 0 and we know that the range is 255, so the chances are the second will be 255. 
The final code is:
You can see a demo here.
Get the code here.
Next, I am going to create a burglar alarm system by combining this and this article. So, please go through this article once and stay tuned.
Javascript code
  1. let level = 0  
  2. input.onButtonPressed(Button.A, () => {  
  3.     led.plotBarGraph(  
  4.     input.lightLevel(),  
  5.     255  
  6.     )  
  7. })  
  8. level = input.lightLevel()  
  9. basic.showNumber(input.lightLevel())