Introduction: Diwali Lights With Micro:bit
I wanted to create a flickering electronic Diwali candle light, very large, for a school cafeteria.
I was given a micro:bit by the school librarian to see if I could do something with it. You have to download Mu to run it. Mu is a very beginner IDE for python and extremely easy to use.
I used the simple code provided by Google AI.
Supplies
- A computer set up with Mu, a kid friendly python IDE I've only seenon Raspberry Pi OS, but whih runs on Windows also. You can download at codewith.mu. Mu is no longer maintained so you will probably have to use win 10 OS.
- a micro:bit with battery case and AAA batteries and approproate cable to attached the battery to the micro:bit
Step 1: Programming the Micro:bit
- Connect the micro:bit to the computer, with appropriate USB cable, having downloaded Mu on the computer.
- Open Mu.
- Drag the following code to the editor of Mu.
'''
+------------------------------------------------
| fire.py - flickering fire on the micro:bit
| by Google AI!
+------------------------------------------------
'''
from microbit import *
import random
while True:
# Create an empty 5x5 image
fire_image = Image("00000:00000:00000:00000:00000")
# Fill the bottom row of pixels with a random brightness (0-9)
for x in range(5):
fire_image.set_pixel(x, 4, random.randint(0, 9))
# Gradually fade and shift the image upwards
for y in range(4, 0, -1):
for x in range(5):
current_brightness = fire_image.get_pixel(x, y)
# Shift the brightness up to the next row
fire_image.set_pixel(x, y - 1, current_brightness)
# Reduce the brightness to simulate the flame fading
new_brightness = int(current_brightness * 0.9)
fire_image.set_pixel(x, y, new_brightness)
# Display the new image on the screen
display.show(fire_image)
# Pause for a short, random time to create a flickering effect
sleep(random.randint(50, 150))
Step 2: Flash the File
Flash the file to the micro:bit and turn on the micro:bit.





