Building a Physical Microbit Neural Network

In this article we going to look at building a neural network using microbits with micropython. The figure above shows the arrangement of the connections; pin 2 is the output of each neuron, pins 0 and 1 act as inputs. The micro:bit images used in the figure were produced using the micro:bit Fritzing diagram available at https://github.com/microbit-foundation/dev-docs/issues/36 thanks to David Whale (@whalleygeek ) for this.
The two micro:bits/neurons on the left of the picture take in the two same inputs; the output from these neurons are the two inputs to the output neuron on the right. Looking at producing a network of neurons, ie. neural network using a three microbits; with each microbit acting as a neuron. To have an example we are looking to solve the problem, that a single neuron alone can’t solve, making an Exclusive OR gate (XOR)
First let us start without the microbits but building an entirely software-based Neural Network in Python first and then moving to the microbits
1 Overview and a non-microbit neuron
1.1 Overview:
The characteristics of the system will be:
- Inputs are going to be binary
- Weighted sum is bias+W1*input1+w2*input2
- If weighted sum>=0 then the output is True (T on the LEDs) or ‘1’
- If weighted sum<0 then the output is False (F on the LEDs) or ‘0
1.2. Single Neuron (without the Microbit)
Lets start without the microbit and build a single neuron in Python and useful exercise in it’s own right just to see the mechanism. A class Neuron is produced and all possible combination of a two-input binary system are feed in.
class Neuron: def __init__(self, input1, bias, w1, w2): self.input1 = input1 self.bias = bias self.w1=w1 self.w2=w2 def CalculateOutput (self): output1 = 0 net = self.bias+self.input1[0]*self.w1+self.input1[1]*self.w2 if net >= 0: output1 = 1 else: output1 = 0 return output1for x1 in range (2): for x2 in range (2): neuron1= Neuron([x1,x2],-1,1,1)print("x1="+str(x1)+"x2= "+str(x2)+" Output= " +str(neuron1.CalculateOutput()))The code above implements a simple single neuron and the weights -1,1,1 produce an OR gate and -2,1,1 produces an AND gate.
1.3 Now as a network
We can extend the code above to produce a neural network, by feeding the outputs of one or more neurons as inputs to other neurones. The code below produces an Exclusive XOR — essentially for the two input case if the two inputs are different then the output is True. The same inputs go to two neurones but they have different weight (bias, W1 and W2) but the outputs from these neurones are the inputs to a third neurone. The code is shown below (the Neuron class hasn’t changed):
class Neuron: def __init__(self, input1, bias, w1, w2): self.input1 = input1 self.bias = bias self.w1=w1 self.w2=w2 def CalculateOutput (self): output1 = 0 net = self.bias+self.input1[0]*self.w1+self.input1[1]*self.w2 if net >= 0: output1 = 1 else: output1 = 0 return output1for x1 in range (2): for x2 in range (2): neuron1= Neuron([x1,x2],-1,-1,1) neuron2= Neuron([x1,x2],-1,1,-1) neuron3= Neuron([neuron1.CalculateOutput(),neuron2.CalculateOutput()],-1,1,1)print("x1="+str(x1)+"x2= "+str(x2)+" Output 1= "+str(neuron1.CalculateOutput())+" Output 2= "+str(neuron2.CalculateOutput())+" Output overall= "+str(neuron3.CalculateOutput()))2. And finally as a Physical Network
Moving from the entirely software-based Neural Network to one with Physical Devices as the neurones is not too big a step. Using a micropython editor such as Mu (https://codewith.mu/) we can program the three microbits to be ‘neurons’. In essence doing the same thing as the purely software version seen above, but with handing physical inputs. The code is shown below.
The Inputs neurons (Neurons 1 and 2)
Neuron 1:
from microbit import *W=[-1,-1,1]while True:
x1=pin0.read_digital()
x2=pin1.read_digital()
net = W[0]+W[1]*x1+W[2]*x2
if net>=0:
display.scroll("T")
pin2.write_digital(1)
else:
display.scroll("F")pin2.write_digital(0)Neuron 2
from microbit import *W=[-1,1,-1]while True:
x1=pin0.read_digital()
x2=pin1.read_digital()
net = W[0]+W[1]*x1+W[2]*x2
if net>=0:
display.scroll("T")
pin2.write_digital(1)
else:
display.scroll("F")pin2.write_digital(0)All that is left to do is produce the code for the output neuron.
from microbit import *W=[-1,1,1]while True:
x1=pin0.read_digital()
x2=pin1.read_digital()
net = W[0]+W[1]*x1+W[2]*x2
if net>=0:
display.scroll("T")
pin2.write_digital(1)
else:
display.scroll("F")pi
pin2.write_digital(0)Next step building training of the weights into the system, any ideas?






