Pulse – Flashing LED using micro:bit
In this project we connect the micro:bit to an LED and make it pulse at various speeds.
- A micro:bit
- A USB cable to link your micro:bit to the computer
- A battery pack
- Two crocodile clip connectors
- An LED
Connect your micro:bit
Using your crocodile clips, carefully connect:
- the Ground (GND) pin to the smaller of the two legs of the LED
- Data pin 0 (0) to the other, larger leg of the LED
It should look like this:
Make sure that the two legs and connectors are not touching each other.
We’ve created an electronic circuit that uses power from the micro:bit to turn on the LED, but we can turn this on and off by changing the state of pin 0.
Load the Mu editor and type in the code to the right:
As before, we first we need to load the micro:bit libraries so that we can access all the micro:bit functions.
Now we can light the LED by setting the state pin 0 to high (1). By doing this we are turning pin 0 on and completing the electric circuit.
Try it: download to the micro:bit and check the LED is lit. If not, check your connections.
To create a pulse, we need to turn the LED off, which we do by adding a line to set pin 0 to low (0), breaking the circuit.
We need a pause in between otherwise it would be too fast to see. We are using the sleep command with 500 milliseconds so the LED should stay lit for half a second
Finally, we put this in a forever loop. Remember to be careful to tab out the code inside the loop.
Try it: the LED should now be flashing
To control the speed of the pulse, we need to store the sleep time in a variable, which we can then change using the buttons.
Try changing the value of pulse, download it and see the effect.
To change the pulse using the buttons we look for button press events, which looks this this:
Add this to your code, and try it. Pressing A increase the pulse by 0.1 seconds making the delay longer. Pressing B does the opposite.
Finally, add some images so that we get some extra feedback when we press a button: