Blinking an LED

header-lesson-03

Blinking an LED#

Now that you know about breadboards and resistors and have built a basic circuit with an LED and the micro:bit, it is time to make the LED blink.

kitronik-blink-one-led-04

Open MakeCode and create a new project.

makecode-blink-one-led-00

Select the Advanced drawer in the Toolbox.

makecode-blink-one-led-01

Select the digital write block.

makecode-blink-one-led-03

Insert the digital write block into the forever block. You can change the value by typing a value or using the slider. A value of 0 means the digital signal will be off, and a value of 1 means the digital signal will be on. This is what will make the LED blink. The pin is set to P0 and does not need to be changed, because the LED is connected to 0 on the Kitronik prototyping plate.

makecode-blink-one-led-04

Change the value to 1 to turn the LED on.

makecode-blink-one-led-05

Next, select the Basic drawer in the Toolbox.

makecode-blink-one-led-05

Select the pause block.

makecode-blink-one-led-07

Place the pause block in the forever block below the digital write block.

makecode-blink-one-led-08

Duplicate the digital write block.

makecode-blink-one-led-09

The duplicated block appaears over the original blocks. .Select the duplicated digital write block.

makecode-blink-one-led-10

Place it below the pause block in the forever block.

makecode-blink-one-led-11

Change the value from 1 to 0.

makecode-blink-one-led-12

Duplicate the pause block.

makecode-blink-one-led-13

Select the duplicated pause block.

makecode-blink-one-led-14

Place the duplicated pause block below the second digital write block.

makecode-blink-one-led-15

Notice that the default value is 100. MakeCode measures time in milliseconds (ms), and 1000 ms is equal to one second. The default value of 100 is 0.10 (one-tenth) of a second, which will cause the LED to blink 5 times (on and off) in a second. Let’s change this value to change the blinking rate.

makecode-blink-one-led-16

To make the LED blink once (on and off) in a second, change the value from 100 to 500.

makecode-blink-one-led-17

Change the value in the other pause block from 100 to 500.

makecode-blink-one-led-18

Now both values are 500 code will turn the LED on for half a second and turn the LED off for half a second.

makecode-blink-one-led-19

The program is now complete. The LED should be turning on and off every second.

Try experimenting with different blink rates by changing the value in the delay block.

The micro:bit is now ready to be flashed with code. Press the download button to transfer the code to the micro:bit.