# BBC Micro:bit IR-based candle detector and counter # by Ido Gendel, 2025 # Hardware setup: # 3V -> L-53P3C 940nm NPN Phototransistor -> Pin 1 and 1K resistor -> GND from microbit import * # 3x5 digits patterns, 0 to 9 and then "E" digits = ["888808808808888", "080880080080888", "888008888800888", "888008888008888", "808808888008008", "888800888008888", "888800888808888", "888008008008008", "888808888808888", "888808888008888", "888800888800888"] count = 0 # How many candles were detected currState = 0 # Are we seeing a candle now? (0/1) threshold = 3 # Range 1-5 inclusive, each step means 170 in raw values def refresh(rawValue): image = [["0","0","0","0","0"], ["0","0","0","0","0"], ["0","0","0","0","0"], ["0","0","0","0","0"], ["0","0","0","0","0"]] # Draw current reading and threshold image[5 - threshold][1] = "3" value = int((rawValue / 1024) * 46) for n in range(5): temp = min(value, 9) image[4 - n][0] = str(temp) value = value - temp # Draw counter digit for i in range(15): image[i // 3][2 + (i % 3)] = digits[count][i] # Display completed image s = "" for line in image: s = s + "".join(line) + ":" display.show(Image(s[:-1])) display.clear() while True: rawValue = pin1.read_analog() if currState == 0: if (rawValue >= threshold * 170 + 85) and (count < 10): currState = 1 count = count + 1 else: if rawValue < threshold * 170 - 85: currState = 0 refresh(rawValue) if button_a.was_pressed(): threshold = threshold + 1 if threshold > 5: threshold = 1 if button_b.was_pressed(): count = 0 display.clear() sleep(200)