Artificial Intelligence often feels like "magic" happening inside a powerful computer. But at its core, a neural network is just a series of mathematical decisions. In my previous posts, we looked at how to simulate these decisions in Python. Today, we are taking that "brain" out of the computer and building it in the real world using three BBC micro:bits.
In this project, each micro:bit acts as a single neuron. By wiring them together, we create a physical network capable of logic and decision-making.
The Math: How a Neuron "Thinks"
Every neuron in our network follows a simple linear formula to decide whether or not to "fire" (send a signal):
| Figure 1 - the maths |
If the result Net >=0 the neuron fires (Output = 1). If it’s less than 0, it stays at Output = 0.
Step 1: The Logic Gate "Cheat Sheet"
Before we flash the code, we need to decide what we want our neurons to do. By changing the weights and bias, we can turn a micro:bit into different types of logic gates. Use this table to program your "Input Neurons":
| Desired Logic | Bias (w0) | Weight 1 (w1) | Weight 2 (w2) | Behavior |
| AND | -1.5 | 1 | 1 | Only fires if BOTH inputs are 1. |
| OR | -0.5 | 1 | 1 | Fires if AT LEAST one input is 1. |
| NOT | 0.5 | -1 | 0 | Inverts the input (1 becomes 0). |
Step 2: The Hardware Setup
To build the network, you will need three micro:bits. We will use two as "Input Neurons" and one as the "Output Neuron."
| Figure 2 |
The Wiring (Referencing Figure 2):
Input Neurons: Connect your sensors (or simple switches) to Pin 0 and Pin 1. Their result will be sent out via Pin 2.
Output Neuron: Connect Pin 2 of the first two micro:bits to Pin 0 and Pin 1 of the third micro:bit.
The Golden Rule: You must connect the GND (Ground) pins of all three micro:bits together. Without a common ground, the digital signals will be "floating" and the network will behave randomly!
Step 3: The Code (MicroPython)
Flash this code onto your micro:bits. You only need to change the W array (the weights) based on the cheat sheet above.
The Inputs neuronsNeuron 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)
Output Neuron.
Feeding the inputs from Neuron 1 and Neuron 2 as inputs
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)Taking it Further
What we’ve built here is a Pre-trained Inference Network. We did the "learning" on a PC to find the right weights, and then hard-coded them into the micro:bits.
The "Smart Gate" Challenge:
Instead of just showing "T" or "F" on the screen, try connecting a servo motor to the Output Neuron. Can you configure the weights so that the gate only opens when two specific safety sensors (connected to the input neurons) are triggered simultaneously?
The Next Frontier: Machine Learning
This network is currently "static"—it doesn't learn from its mistakes. A great follow-up project would be to write code that allows the micro:bit to adjust its own weights. For example, if you press Button A, the weights increase; if you press Button B, they decrease. This would move us from simple logic gates to a truly learning physical system!
Comments
Post a Comment