In this tutorial, I’ll guide you through taking your initial steps into programming with the dynamic and thrilling BBC micro:bit! This compact computer was designed to introduce coding to children through hands-on exploration of hardware and software. Therefore, this straightforward tutorial concentrates on the basic task of turning an LED on and off.
Parts Required
- BBC micro:bit (includes micro:bit board, USB cable and battery holder);
- BBC micro:bit edge connector;
- 1 green LED;
- 1 1kΩ resistor;
- Male-to-male jumper wires;
- 1 Breadboard.
Note: It is strongly advised to use the BBC micro:bit edge connector, though it’s not mandatory. Alternatively, one can employ crocodile clips or 4mm banana plugs for the connection.
BBC micro:bit
The BBC micro:bit (Figure 1) is a miniature computer created by the BBC in 2016. It introduces children to the synergy between software and hardware and facilitates their coding learning journey through web or mobile applications. Packed with diverse input and output functionalities, this diminutive computer enables children to engage in numerous games and activities, all while refining their skills.
The micro:bit features 25 external connections (pins) located on the edge connector of the board. This edge connector is the gold-colored area situated on the right side of the board, as illustrated in the figure below.
There are 5 large pins, each corresponding to labeled holes on the board: 0, 1, 2, 3V, and GND. Additionally, along the same edge, there are 20 smaller pins intended for use when connecting the micro:bit to an edge connector.
The initial three large pins, labeled 0, 1, and 2, are versatile and can serve various purposes, often referred to as “general-purpose input and output” (GPIO). Additionally, these pins possess the capability to measure analog voltages through an analog-to-digital converter (ADC). They share the same functionality. The two remaining large pins, 3V and GND, are power pins. The 3V pin serves as a 3 Volt power output when the micro:bit is powered by USB or a battery, or as a power input otherwise. Conversely, the GND pin connects to the ground to complete a circuit.
The 20 small pins are numbered sequentially from 3 to 22. While the three large pins are exclusively designated for external connections, some of the small pins are shared with other components on the micro:bit board. For instance, pin 3 is shared with certain LEDs on the micro:bit’s screen. Consequently, if you’re using the screen to display scrolling messages, this pin cannot be used simultaneously. Function assignments for the small pins may vary across different versions of the micro:bit.
For additional information, microbit.pinout.xyz is an excellent resource regarding the micro:bit pins and their utilization with popular accessories.
The micro:bit is also packed with an array of input and output sensors, including LED lights, buttons, a speaker, microphone, accelerometer, compass, temperature sensor, radio, Bluetooth transmitter, and more. These sensors can be programmed to conduct experiments, develop games, and engage in various activities, all using just one board. When compared to Arduino boards like Uno, the micro:bit appears simpler but possesses enhanced power and functionality. The integration of these functions eliminates the need for additional extensions, allowing for the creation of exciting experiments without hassle.
Moreover, the micro:bit offers coding options in Python or JavaScript-based block coding, ideal for young children. In contrast, Arduino programs are written in C++ using the Arduino IDE. Despite Arduino’s utilization of a simplified version of C++, its interface lacks the interactive nature of a block-based platform [2].
Circuit
The circuit required in this tutorial is very simple, consisting of a power supply (the BBC micro:bit), an LED and a resistor to limit the current that can flow through the circuit.Simply connect Pin 0 to one terminal of the resistor, and the remaining terminal to the LED’s anode. Then, link the LED’s cathode to the GND pin of the micro:bit. Ultimately, your setup should resemble the circuit depicted in Figure 3.
For further details on the schematics of the BBC micro:bit, refer to this page.
Code
To facilitate our code development, we can use Microsoft MakeCode for micro:bit. This framework is designed to craft interactive and captivating programming experiences, particularly tailored for beginners in the programming world.
Once in the MakeCode platform, click on “New Project”.
Name your project and click on “Create”. I named mine as “LED”.
The default code development window will show as follows. Start by eliminating the two existing blocks from the development window.
Then, access the “Input” category in the Toolbox and drag the “on button A pressed” block to the development window.
Repeat the previous step and select button “B” from the dropdown bar.
Now, access the “Pins” category in the Toolbox and drag two “digital write pin P0 to 0” blocks to the previous blocks.
Insert “1” so that the LED turns on when button A is pressed and off when button B is pressed. After that, click on the three dots next to “Download”, and select “Connect Device”.
The following window will appear. As indicated, connect the micro:bit to your computer using the USB cable and click “Next”.
A window should appear at the top of your browser prompting you to pair your device. Select the micro:bit device and click connect. Then, click “Pair”.
After successfully pairing, the code will be downloaded to your micro:bit. The end result shall be as follows:
Blocks:
JavaScript:
input.onButtonPressed(Button.A, function () { pins.digitalWritePin(DigitalPin.P0, 1) }) input.onButtonPressed(Button.B, function () { pins.digitalWritePin(DigitalPin.P0, 0) })
Python:
def on_button_pressed_a():
pins.digital_write_pin(DigitalPin.P0, 1)
input.on_button_pressed(Button.A, on_button_pressed_a)
def on_button_pressed_b():
pins.digital_write_pin(DigitalPin.P0, 0)
input.on_button_pressed(Button.B, on_button_pressed_b)
That’s it! If you enjoyed this tutorial, you can visit our YouTube channel and watch our many tutorials. Thanks for following us and be sure to rate, comment and share our content.
References
[1] https://tech.microbit.org/hardware/edgeconnector/
[2] https://www.codeadvantage.org/coding-for-kids-blog/micro-bit-vs-arduino
Towards the Future !!! 😉