Software apps and online services
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.
This project demonstrates how to control the 3V Lamp (L1) of Snap Circuits with the BBC micro:bit.
The P0, P1, and P2 pins of the micro:bit can drive just a few mA of current, which is enough to power low-current devices like LEDs. However, most other devices consume a larger current. The 3V Lamp (L1) can consume up to 200 mA of current, which cannot be delivered by the micro:bit pins.
Therefore, the lamp must be powered directly from the batteries, yet connected to the micro:bit to control its on and off state. We will use the NPN Transistor (Q2) as a switch to turn the lamp on and off by applying a small amount of current from the micro:bit to the transistor's base.Snap Circuits diagram
Build the circuit shown in the diagram above.Code
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/_WxeEUFM3biiD
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.
At this moment no current flows from pin P2 of the micro:bit to the base of the NPN transistor (Q2). Therefore, no current flows between the collector and the emitter of the transistor, so that part of the circuit is open and the Lamp (L1) is off.
Press and release button A on the micro:bit. The "on button A pressed" event triggers and the micro:bit writes a digital 1 signal to pin P2. This closes the circuit between the P2 and GND pins and a small current starts flowing to the base of the NPN transistor, through the transistor's emitter, the GND pin of the snap:bit, and back to the negative terminal of the Battery Holder (B1).
The 1kΩ Resistor (R2) limits the current flowing to the base of the transistor to a very small amount - around 1 mA. The transistor would be damaged if a large current flows through it. The resistor protects the transistor from this risk.
The small amount flowing through the base of the transistor is enough to trigger the current flow from the collector to the emitter. A large amount of current (up to 200 mA) starts flowing from the positive terminal of the Battery Holder (B1), through the Lamp (L1), the collector and emitter of the transistor, the GND pin of the snap:bit, and back to the negative terminal of Battery Holder (B1). The part of the circuit connected to the lamp is now closed and the lamp turns on.
Press and release button B on the micro:bit. The "on button B pressed" event triggers and the micro:bit writes a digital 0 signal to pin P2. This opens the circuit between the P2 and GND pins and the current stops flowing to the base of the transistor. This stops the current flow from the collector to the emitter of the transistor and the lamp turns off.