from machine import Pin
import time
# Set up input pins for number A (2-bit)
a0 = Pin(6, Pin.IN, Pin.PULL_DOWN)
a1 = Pin(7, Pin.IN, Pin.PULL_DOWN)
# Set up input pins for number B (2-bit)
b0 = Pin(8, Pin.IN, Pin.PULL_DOWN)
b1 = Pin(9, Pin.IN, Pin.PULL_DOWN)
# Set up output pins for LEDs (result)
leds = [
Pin(2, Pin.OUT), # R0 (lowest bit)
Pin(3, Pin.OUT), # R1
Pin(4, Pin.OUT), # R2
Pin(5, Pin.OUT) # R3 (highest bit)
]
# Function to show result on LEDs
def show_binary(value):
for i in range(4):
leds[i].value((value >> i) & 1)
while True:
# Read 2-bit numbers A and B
a = a0.value() + (a1.value() << 1)
b = b0.value() + (b1.value() << 1)
result = a + b # Add the numbers
show_binary(result) # Show result on LEDs
time.sleep(0.1) # Small delay to stabilize