Introduction

点亮一盏灯的场景图

LEDs are small, powerful lights that are used in many different applications.Here we will use an led module to make a small star twinkling in the night sky.

That’s right — it’s as simple as turning a light on and off. It might not seem like much, but establishing this important baseline will give you a solid foundation as we work toward more complex experiments.

Parts Needed

You will need the following parts:

1x micro:bit
1x Micro B USB Cable
1x micro:bit Breakout (with Headers)
1x Jumper Wire
1x LED Module

所有器件图片

Hardware Hookup

Ready to start hooking everything up? Check out the wiring diagram and hookup table below to see how everything is connected.

LED Module Micro Bit Breakout
GND/- GND
VCC/+ 3.3V
S P0

Insert micro:bit into the edge connector of the expansion board.
Note:micro:bit will always stay attached to the expansion board in the following sessions.
Connect the LED module to the P0 port of the expansion board.

图片(实物图/fritzing图)

Program

Task 1: Turn on the LED

Open online programming interface of MakeCode(https://makecode.microbit.org/).

makecode首页图 1

STEP1: Start a new project.

点击+号生成新项目 图片2

You will see the edit page as below:

makecode 编辑页面图片 3

STEP2: The external LED is used in the hardware connection. During the process of programming, the turning-on and turning-off of the light is controlled by setting the pin status of micro: bit. LEDs in this experiment belong to the digital output.

Click on “Advanced” and there will be “Pins”. The procedure for calling the instruction: Pins (advance) —-digital write pin p0 to (0).

图片4

STEP3: Drag the module into “forever” and upload the code. When the code is successfully uploaded to the micro: bit, the external LED connected to the P0 pin lights up.

You can also clik the below part and download the code to your BBC board directly.

led被点亮时的实物图

Task 2: Flashing LED

Goal: After learning how to turn on a LED, we should learn how to make it blink.

STEP1: Start a new project.

STEP2: Set the pin status of micro: bit to either turn on and off the LED. LEDs in this experiment belong to the digital output. Click the “Advance”->”Pins”, set digital write pin p0 to (0). The pin value can only be 0 or 1. When it is 1, the light stays on. When it is 0, the light stays off.

图片5

Set the value of Pin P0 to high (1) and the LED will light

图片6

Set the value of Pin P0 to low (0) and the LED will turn off

STEP3: You will also need to keep the external LED on for 1 second and off for another second in a sequence. To do this, go to Basic and select Pause (ms) (100). The “Pause” command keep the same status for a certain period of time.

tupi

STEP4: The final code will show as below after dragging the “Pause” command to the “forever” and adjusting the numbers. In this picture: set “pin P0” as 1 (LED on) and “pause(ms)” as 1000, pin P0 as 0 (off) and “pause(ms)” as 1000, and the program inside the “forever” loop will be running repeatedly in a sequence.

Click ‘Download’ and transfer your script onto your micro:bit.

图片7
You can also clik the below part and download the code to your BBC board directly.

Running Result

Turn out the lights in the room and connect the expansion board to the power supply. You will see a small star shining continuously.

实验结果动态图/图片