microbit Logic Gates Practical Overview:
I wanted a hands on way to teach logic gates and truth tables to my students, so I decided to program a set of microbits to act like logic gates. These logic gates could then be connected to switches and outputs using crocodile clips.
- 3 BBC microbits
- 8 crocodile clips
- 3 two pin rocker switches
- 3 battery packs for the microbits
- 1 micro USB cable
- 1 piece of A4 card
- Roll of foil
- 10 brass hex standoff spacers (M3, male to female, 16mm)
- 10 nylon hex standoff spacers (M3, male to female, 16mm)
Getting Things Ready:
Start by covering a piece of A4 card in foil.
Next attach the brass and nylon hex spacers to 2 microbits as shown in the picture, these will become logic gates.
The upward facing brass hex spacers make it easy to connect crocodile clips without them touching the other pins. The downward facing brass hex spacer allows all the microbits to be grounded by touching the foil.
Now attach the brass and nylon hex spacers to the remaining microbit as shown in the picture, this will be the output.
Programming the Logic Gates:
Two of the microbits will be programmed to be logic gates. The program is explained below.
This section of code imports the microbit module and defines the icons that will be displayed when each logic gate is active.
from microbit import * notGate = Image("99000:95990:95599:95990:99000") andGate = Image("99990:95559:95559:95559:99990") orGate = Image("99990:09559:09559:09559:99990")
This is the function that is called when the NOT gate is active. It starts by displaying the notGate icon. It checks whether pin0 is connected to ground, if so this is taken as a 1 being inputted. This is opposite to the behaviour we would normally expect for a logic gate, however this method removes the need for the switches to be connected to a power source.
If a 1 is received the NOT gate outputs a 1 on pin2. This is interpreted as a 0 on the receiving logic gate or output.
def notGateProg(): display.show(notGate) if pin0.is_touched(): pin2.write_digital(1) else: pin2.write_digital(0)
These are the functions for the AND and OR gates.
def andGateProg(): display.show(andGate) if pin0.is_touched() and pin1.is_touched(): pin2.write_digital(0) else: pin2.write_digital(1) def orGateProg(): display.show(orGate) if pin0.is_touched() or pin1.is_touched(): pin2.write_digital(0) else: pin2.write_digital(1)
This part of the program allows the user to cycle through the logic gates by pressing the A button. The appropriate function for the current logic gate is continually called within a while loop.
gateChoice=1 while True: if gateChoice==1: notGateProg() elif gateChoice==2: andGateProg() elif gateChoice==3: orGateProg() if button_a.was_pressed(): if gateChoice < 3: gateChoice+=1 else: gateChoice=1
Programming the Output:
The remaining microbit needs to programmed to be an output.
This program checks whether pin0 is grounded, if so it displays a 1, otherwise it displays a 0. Therefore if a 1 is received this is interpreted as a 0.
from microbit import * while True: if pin0.is_touched(): display.show("1") else: display.show("0")
Creating a Logic Circuit:
Connect the components as shown in the diagram and picture. This is a simple logic circuit with 1 input, a NOT gate and 1 output.
Try it out by turning the switch on and off.
Follow the activities in the PowerPoint workbook in order to create more complex logic circuits.
- Make sure all micro bits are touching the foil.
- Try not to touch the metal contacts on the switches or crocodile clips with your hands, this could ground the inputs unintentionally.