software resources for science and technology education
Mr Bit Experiments with Sensors and Devices
When your Mr Bit Starter Kit is ready, you can experiment with connecting sensors to your micro:bit and controlling devices.
To get you started, here are some sample experiments. Instructions for a full set of 18 experiments, coding examples and a list of components are available for free download.
Coding
The examples here show three equivalent systems:
Making connections
ALARM TEST
If you have a smoke alarm, from time to time you need to test it to make sure that it still works and the battery is not flat.
Create a program to sound continuous beeps after a button gets pressed. The beeps should stop when you press the button for a second time.
from microbit import *
status = False
while True:
if button_a.was_pressed():
status = not status
if status == True:
pin0.write_digital(1)
sleep(1000)
pin0.write_digital(0)
sleep(1000)
When button A gets pressed, pulse the buzzer until button A gets pressed again.
PERFECT LIGHT
A perfect photograph depends upon allowing the right amount of light into the camera – if there is too much light, the picture is overexposed – too little light and the picture is too dark. As well as the light sensor, this experiment uses a yellow LED to show when the light is just right but a red LED at other times.
Create a program which lights up a Yellow LED when the light level is between 50% and 60%, but lights up a Red LED for all other light values.
from microbit import *
while True:
light_level = pin0.read_analog() #sensor
if light_level < 500 or light_level > 600:
pin1.write_digital(1) #Red LED
pin2.write_digital(0) #Yellow LED
else:
pin1.write_digital(0)
pin2.write_digital(1)
Switch on the Red LED until it is lighter than 50 and it is darker than 60.
Switch on the Yellow LED until it is darker than 50 or it is lighter than 60.
EXTREME ALERT
With a temperature sensor and a buzzer connected to the micro:bit pins: Create a program to sound the buzzer when the temperature gets too cold or too hot.
from microbit import *
while True:
reading = pin0.read_analog() #sensor
temperature = reading / 10
if temperature < 15 or temperature > 25:
pin1.write_digital(1) #red LED
sleep(500)
pin1.write_digital(0)
sleep(2000)
When it is cooler than 15 or warmer than 25, switch on the buzzer for 0.5 seconds.
Wait for 2 seconds.
© 2024 Insight Resources