Project 4Sensors35 min
π¦ Smart Flashlight
Make a flashlight that turns on by itself when it's dark and has special modes.
Workshop projects (4 of 6)
Let's turn your micro:bit into a super smart flashlight that turns on by itself when it gets dark and has special modes!
What are we going to do?
Our smart flashlight will have:
- π Automatic mode: Turns on when it gets dark
- πΊ Disco mode: Lights that flash to the beat
- π¦ Normal flashlight mode: Always on
What is the light sensor?
The micro:bit can "see" whether it's light or dark using its light sensor. It's like an electronic eye! The sensor gives us a number:
- 0 = Very dark (like at night)
- 255 = Very bright (like in the sun)
Step 1: Create the project
- Create a new project called "Smart Flashlight"
- We'll start with a variable to remember which mode we're using
Create variables:
- Go to "Variables"
- Make a variable called "mode"

Step 2: Getting started
In the "on start" block:
- Set mode to 1 (we start in automatic mode)
- Show string "Flashlight"
- Pause for 1 second
- Show number with the variable "mode"

Step 3: Program the automatic mode
Here comes the magic! Let's make the flashlight notice when it's dark.
For this we'll use the "forever" block
Decide whether to turn on the flashlight:
- Go to "Logic" and drag an "if... then... else" block
- Put it inside the "forever" block
- Go to "Input" and find the "light level" block
- Program this logic:
if mode = 1 then
if light level < 100 then
show leds (all on)
else
clear screen

Step 4: Change mode with button A
Let's be able to switch between modes:
- Drag "on button A pressed"
- Inside that block, program the mode change:
if mode = 3 then
set mode to 1
else
change mode by 1
show number mode
The modes will be:
- 1 = Automatic
- 2 = Disco
- 3 = Normal flashlight

Step 5: Disco mode
Now the most fun mode! Lights that change pattern quickly:
- Go to "Basic" and add a new "forever" block
- Inside that "forever" block, add this program
if mode = 2 then
show leds (pattern 1)
pause 200 ms
show leds (pattern 2)
pause 200 ms
show leds (pattern 3)
pause 200 ms

Step 6: Normal flashlight mode
The easiest one, always on:
- Go to "Basic" and add a new "forever" block
- Inside that "forever" block, add this program
if mode = 3 then
show leds (all on)

Step 7: Try your flashlight!
- Try the automatic mode: Cover the micro:bit with your hand to make it dark
- Press button A: To switch between modes
- Disco mode: It should look like a light party!
Extra improvements
Button B for disco mode
Add an "on button B pressed" that goes straight to disco mode:
set mode to 2
show icon music
Adjust the light sensitivity
If the flashlight turns on too much or too little, change the number 100 to:
- 50 so it only turns on when it's very dark
- 150 so it turns on with just a little darkness
Add sounds
Give each mode its own sound!
- Go to the "Music" category
- Add "play tone" blocks in each mode:
- Automatic mode: A soft tone when it turns on (262 Hz)
- Disco mode: A different tone with each pattern
- Pattern 1:
play tone 262 Hz for 200 ms - Pattern 2:
play tone 523 Hz for 200 ms - Pattern 3:
play tone 1047 Hz for 200 ms
- Pattern 1:
Rainbow mode
Add a fourth mode that cycles through different shape patterns!
What have you learned?
- β How to use the micro:bit's light sensor
- β Programming with several modes
- β Using "forever" loops for patterns
- β Combining sensors with conditions
- β Making light effects with timing
Common problems
π€ The flashlight doesn't turn on by itself?
- Try covering the micro:bit completely with your hands
- Change the value 100 to a higher number (like 150)
π€ Disco mode is too fast or too slow?
- Change the pause values: bigger numbers = slower
- Try different speeds: 100ms, 300ms, 500ms
π€ The mode doesn't change?
- Check that the "mode" variable is updated correctly
- Make sure the numbers in the conditions are right (1, 2, 3)
Real uses
Your smart flashlight can be really useful!
- ποΈ Camping: An automatic flashlight that turns on when it gets dark
- π Parties: Disco mode to liven things up
- π Exploring: A normal flashlight to look for things
Ready for more fun?
Great! Now that you've mastered sensors and modes, let's make music with your micro:bit in Project 5: Magic Piano.