Halloween STEM Project: A Motion-Sensing Light-Up Pumpkin with micro:bit
A great Halloween STEM project for kids is a pumpkin that wakes up when someone touches it. Put a micro:bit inside a plastic or foam pumpkin, and its built-in motion sensor (the accelerometer) notices when the pumpkin is picked up, bumped or tilted. The code then flashes a surprised face, plays a spooky sound and makes a strip of orange LEDs flicker like a candle. It takes about an hour, it needs no soldering, and kids end up learning sensors, thresholds and loops without noticing they're doing "real" programming.
Here's everything you need, the full MicroPython code, and ideas for making it your child's own.
What you need
A micro:bit V2 (the V2 has the built-in speaker this project uses)
A battery pack with 2 AA batteries and a USB cable for loading the code
A short NeoPixel (WS2812) LED strip or ring, about 8 LEDs, with alligator clips or a micro:bit edge-connector breakout
A plastic or foam craft pumpkin (not a real one; it keeps the electronics dry and is easy to cut)
Tape or a small zip-top bag to hold the parts in place
No LED strip? Skip it. The micro:bit's own 5x5 LED display and speaker are enough for a fun first version. If you're still choosing a board, our coaches compare options in Best Robotics Kits for Kids at Home and mBot2 vs micro:bit vs LEGO CS & AI.
How the motion sensing works
The accelerometer measures pull in three directions (x, y and z) in milli-g. When the pumpkin sits still, those numbers barely change. When someone lifts or bumps it, they jump. Every 50 milliseconds the program compares the new reading to the last one and adds up how much each direction changed. If the total is bigger than a threshold (we start at 300), that counts as motion and the pumpkin lights up.
That one idea, "read a sensor, compare it to a threshold, react," is the same pattern kids later use for line followers, obstacle-avoiding robots and FLL robot runs.
Wiring
NeoPixel GND to micro:bit GND
NeoPixel power (5V/VCC) to micro:bit 3V
NeoPixel data in (DIN) to micro:bit pin 1
Keep the strip short (around 8 LEDs). The micro:bit's 3V pin can't power a long strip, and a short one is plenty bright inside a pumpkin. If your LEDs flicker oddly or the micro:bit resets, use fewer LEDs or a dimmer orange.
The code (MicroPython)
Open the micro:bit Python Editor, paste this in, and click Send to micro:bit. We tested the logic before publishing; values like the threshold may need tuning for your pumpkin.
# Motion-sensing light-up pumpkin for micro:bit V2 (MicroPython)
from microbit import *
import neopixel
NUM_LEDS = 8 # LEDs on your NeoPixel strip or ring
THRESHOLD = 300 # how much change (in milli-g) counts as motion
GLOW_TIME = 4000 # how long the pumpkin stays lit (ms)
strip = neopixel.NeoPixel(pin1, NUM_LEDS)
ORANGE = (255, 80, 0)
def movement(old, new):
# add up how much each axis changed since the last reading
return abs(new[0] - old[0]) + abs(new[1] - old[1]) + abs(new[2] - old[2])
def light_up():
display.show(Image.SURPRISED)
audio.play(Sound.MYSTERIOUS, wait=False)
start = running_time()
step = 0
while running_time() - start < GLOW_TIME:
for i in range(NUM_LEDS):
# every other LED flickers so it looks like a candle
if (i + step) % 2 == 0:
strip[i] = ORANGE
else:
strip[i] = (120, 30, 0)
strip.show()
step += 1
sleep(150)
strip.clear()
display.show(Image.ASLEEP)
display.show(Image.ASLEEP)
last = accelerometer.get_values()
while True:
now = accelerometer.get_values()
if movement(last, now) > THRESHOLD or button_a.was_pressed():
light_up()
now = accelerometer.get_values() # fresh reading after the glow
last = now
sleep(50)What each part does:
movement() adds up the change on all three axes, so lifting, tilting and bumping all count.
light_up() shows a surprised face, starts a sound without waiting for it to finish, and flickers the LEDs for 4 seconds by swapping bright and dim orange.
Button A also triggers the effect, which is handy for testing at the table.
The main loop takes a fresh reading after each glow so the pumpkin doesn't keep re-triggering itself.
Make it your own
Tune the sensitivity: lower THRESHOLD to 150 to react to a light tap, raise it to 600 so only a real pick-up counts.
Change the mood: try Image.GHOST, Image.SKULL or Sound.GIGGLE.
Colors: a purple-and-green "witch" pumpkin is just a change to the RGB numbers.
Count the scares: add a variable that goes up by one each time and press button B to show the total.
Prefer blocks? The same idea works in MakeCode with the "on shake" block, a great first step before Python.
Safety notes
Use LEDs only, never a candle, with electronics inside.
Let an adult do any cutting of the pumpkin.
Unplug the battery pack when the pumpkin is put away.
Common questions
What age is this project for? Kids around 8 and up can build it with a little help; younger kids can do the MakeCode "on shake" version. Teens can extend it with extra sensors and their own animations.
Will it work on a micro:bit V1? The motion sensing and LEDs will. The V1 has no built-in speaker or Sound images, so delete the audio.play line.
Can my child learn more micro:bit projects like this? Yes. Our Robotics Explorers micro:bit course starts from the basics, and Advanced micro:bit moves into bigger projects.
Keep building after Halloween
If your child enjoyed this, there are easy next steps. Start with our free self-paced courses, like Scratch, or browse the full course catalog. All-Access is $39/month with a 7-day free trial, and Remote 1-on-1 live lessons are $149/month (text (619) 786-5731 to set one up).
In San Diego? Join our weekend group sessions in Scripps Ranch: 1.5 hours a week, ages 6–18, max 10 kids, and every child works on their own project at their own pace. Your first session is free. Text (619) 786-5731 to book it.



Comments