The snap:bit Is an electronic component for the Snap Circuits educational electronic kit. It features a socket for connecting the BBC micro:bit. This allows the Snap Circuits to be programmatically controlled by the micro:bit.
As we already know how to light an LED up with the micro:bit, it is time to try and see what we can do with that LED with the help of some programming code.
In this project we will make the LED blink, i.e. it will be continuously turning on and off by order of the micro:bit.Snap Circuits diagram
Build the circuit shown in the diagram above.Code
The code in this project uses the "forever" cycle to continuously repeat the instructions in the cycle's body. They change the digital signal written to pin P1 (where the LED is connected to) between "0" and "1" every second.
You can build the code yourself in the MakeCode Editor. You will find the "digital write pin" block under the Advanced > Pins section.
Alternatively, open the ready project here: https://makecode.microbit.org/_TCe3qu5mTLLH
Once ready, download the code to your micro:bit. Then disconnect all cables from your micro:bit. Both the USB and the battery pack must be disconnected from the micro:bit.How it works...
When you close the slide switch (S1), the Battery Holder (B1) powers the snap:bit through the 3V snap and the micro:bit turns on.
The “forever” loop starts. It first writes a digital 1 signal to pin P1. This closes the circuit between the P1 and GND pins and the LED (D1) turns on. Then it pauses for 1 second. Then it writes a digital 0 signal to pin P1. This opens the circuit between the P1 and GND pins and the LED turns off. The loop now pauses for another second. So far this made the LED be on for one second, and off for one second.
The “forever” loop executes the instructions in its body again and again until the micro:bit is turned off. Therefore, the LED will blink every second.
Try changing the duration of the pause instructions. For example:
- Change both to pause for 200 ms
- Change only the first pause to 200 ms and leave the other to pause for 1 second
How does this change the blinking of the LED?