No related product information!

Lesson 13 Make a Smart Light

2380

 In this lesson, we will carry out an interesting experiment to make a smart light with a Micro:bit.

13.1 Components to be prepared

image.png

13.2 LED display

The front LED display of Micro:bit consists of 25 5x5 lattice LEDs.

25 LEDs arranged in a 5x5 grid make up the display for showing pictures, words and numbers.

image.png


13.3 Light sensor

(1)As well as working as an output, the LEDs on your micro:bit can also work as an input device light sensor, measuring the amount of light falling on them.A light sensor is an input device that measures light levels. Your BBC micro:bit uses the LEDs to sense the levels of light and lets you program your micro:bit as a light sensor.

image.png 

In this lesson, we will make a smart light that can measure the amount of light. When your Micro:bit is in the dark, its LED screen will automatically light up. When your Micro:bit is exposed to light, it automatically turns off the LED.

More information about the light sensor is available in the following link:

https://www.microbit.org/get-started/user-guide/features-in-depth/#light-sensor


13.4 Circuit

Connect micro:bit and PC with a Micro USB cable.

image.png



13.5 MakeCode programming

  We will use an online MakeCode Editor to complete the experiment in this lesson, as shown below.

13.5.1 Start programming

(1) Log in to the website

1. You need to enter the URL in the address bar of Google Browser:

https://makecode.microbit.org/

2. After the website is successfully opened, the interface as shown below will appear:

image.png


(2) Import a project

1. In the HOME interface, click the "Import" button to import the external ".hex" file:

image.png


In the pop-up dialog box, select the "Import File", as shown in the following figure:

image.png


Click the "Choose File":

image.png


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_13\BlockCode

Select the file in ".hex" format and click the Open:


image.png


2. Notice whether the file has been loaded into the following window, and then click the "Go ahead!" button, as shown in the following figure:


image.png


3. Open the file successfully, as shown in the following figure:

image.png


13.5.2 Run the program

1. After the program is written, connect micro:bit and PC with a Micro USB cable.

2. After micro:bit is connected to the computer, you need to first "Pair device". Click the image.png button on the right of image.png in the lower left corner, and then click the image.png option, as shown in the following figure:

image.png


Then click image.png in the lower right corner:

image.png


Then the following dialog box will pop up, select image.png, and then click image.png

image.png


After the device is successfully paired, the image.png button changes to image.png

image.png


3. Start to download the program to Micro:bit, and click the image.png button. Generally, the program will be downloaded directly to the Micro:bit. After the download is completed, your Micro:bit will restart and run the program just downloaded.Observe the LED screen of the Micro:bit. If your Micro:bit is exposed to a high intensity of light, every LED will be turned off. If your Micro:bit is in the dark, the LED screen will light up, as shown in the figure below:

image.png


[Note]:

If Micro:bit doesn't respond after clicking the image.png, you need to click the image.png button on the right of the image.png, and then click the image.png , and observe the situation of the Micro:bit again, as shown in the following figure:

image.png


 If you have problems, please send us an email: support@adeept.com


13.5.3 Learn the code program

In the program, we use the following instruction blocks, which are explained as follows:


Block

Function

image.png 

This is an instruction block to detect the intensity of light.

image.png 

This is an instruction block to turn off every LED.

image.png 

This is an instruction block for conditional judgment.

image.png 

It uses selection to sense if the light falling on the micro:bit falls below a certain level – is less than (<) 80. If it is dark, it lights up the micro:bit display, else it clears the screen so the LEDs are dark.You may need to adjust the threshold number 100 depending on the lighting conditions where you are.

image.png 

This is an instruction block to control delay, and the number indicates a delay of 1s.


13.6 Python programming

13.6.1 Run the program

1.Connect micro:bit and PC with a Micro USB cable.

2. Open the Mu Editor installed on the computer, and click the button [Load] in the upper left corner to open the source code program of this lesson:

image.png 


Find the code file for this lesson:

BBC_Microbit_Sensor\Code\Lesson_13\PythonCode

Select the file in ".py" format and click the Open:

image.png 


3. Click the [Flash] button to download the program to Micro:bit, as shown in the following figure:

image.png 


   

4.After the program is downloaded, your Micro:bit will restart and run the program you just downloaded. Observe the LED screen of the Micro:bit. If your Micro:bit is exposed to a high intensity of light, every LED will be turned off. If your Micro:bit is in the dark, the LED will light up, as shown in the following figure:

image.png 

 

【Note】:

After you click the [Flash] button, if there is no change on the LED screen of Micro:bit, you need to restart the Micro:bit, and then click the [Flash] button again.

    If you have problems, please send us an email: support@adeept.com


13.6.2 Learn the code program

The source codes are as follows:


1

2

3

4

5

6

7

8

9

10

11

12

from microbit import *

while True:

    if display.read_light_level() 100:

        display.show(Image(

        "99999:"

        "99999:"

        "99999:"

        "99999:"

        "99999"))

    else:

        display.clear()

    sleep(2000)


(1) The method of the read_light_level() can read the intensity of light, and the data in 0~255 can be read.


3

if display.read_light_level() 100:

 

(2) The method of the display.clear() can turn off every LED.


11

display.clear()