Every onboard sensor with specs, MakeCode blocks, MicroPython code, classroom activity ideas, and NGSS alignment. All code is tested in both the MakeCode online simulator and on real hardware. In Workshop 3 we pair any of these sensors with the Aggregator Node pattern to build a two-device IoT system — sender on one simulated micro:bit, receiver on a second — with no hardware required.
basic.forever(function () {
basic.showNumber(input.temperature())
basic.pause(2000)
})
MakeCode blocks: forever → show number → temperature (°C) → pause 2000ms
from microbit import *
while True:
display.scroll(temperature())
sleep(2000)
basic.forever(function () {
led.plotBarGraph(input.lightLevel(), 255)
basic.pause(500)
})
MakeCode blocks: forever → plot bar graph of → light level → up to 255 → pause 500ms
from microbit import *
while True:
light = display.read_light_level()
display.scroll(light)
sleep(1000)
// Show acceleration strength on LED bar
basic.forever(function () {
let strength = input.acceleration(Dimension.Strength)
led.plotBarGraph(strength, 2048)
basic.pause(100)
})
from microbit import *
import math
while True:
x = accelerometer.get_x()
y = accelerometer.get_y()
z = accelerometer.get_z()
strength = math.sqrt(x**2 + y**2 + z**2)
display.scroll(int(strength))
sleep(500)
basic.forever(function () {
let heading = input.compassHeading()
if (heading < 45 || heading > 315) {
basic.showString("N")
} else if (heading < 135) {
basic.showString("E")
} else if (heading < 225) {
basic.showString("S")
} else {
basic.showString("W")
}
})
from microbit import *
compass.calibrate()
while True:
heading = compass.heading()
if heading < 45 or heading > 315:
display.show("N")
elif heading < 135:
display.show("E")
elif heading < 225:
display.show("S")
else:
display.show("W")
sleep(200)
// Sound level meter
basic.forever(function () {
led.plotBarGraph(input.soundLevel(), 255)
basic.pause(100)
})
from microbit import *
while True:
level = microphone.sound_level()
display.scroll(level)
sleep(500)
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
basic.showIcon(IconNames.Heart)
})
input.onLogoEvent(TouchButtonEvent.Released, function () {
basic.clearScreen()
})
from microbit import *
while True:
if pin_logo.is_touched():
display.show(Image.HEART)
else:
display.clear()
sleep(100)
Two physical buttons plus the ability to detect A+B pressed simultaneously. Useful for user input, mode switching, and data logging triggers.
// Simple counter
let count = 0
input.onButtonPressed(Button.A, function () {
count += 1
basic.showNumber(count)
})
input.onButtonPressed(Button.B, function () {
count = 0
basic.showNumber(count)
})
from microbit import *
count = 0
while True:
if button_a.was_pressed():
count += 1
display.scroll(count)
if button_b.was_pressed():
count = 0
display.scroll(count)
sleep(100)
| Activity | Sensors | NGSS Standard | Grade Band | CTE Pathway |
|---|---|---|---|---|
| Temperature data logging (sun vs shade) | Temperature | MS-ESS2-6: Weather & climate data | 6-8 | STEM, Environmental Tech |
| Light intensity vs distance investigation | Light | MS-PS4-2: Light wave properties | 6-8 | STEM |
| Shake detector / motion alarm | Accelerometer | MS-PS2-2: Force and motion | 6-8 | Engineering, Manufacturing |
| Classroom noise monitor | Microphone | MS-PS4-1: Wave amplitude & energy | 6-8 | STEM, Health Science |
| Plant growth light monitor | Light + Temperature | MS-LS1-6: Photosynthesis | 6-8 | Agriculture, Environmental |
| Ramp acceleration experiment | Accelerometer | HS-PS2-1: Newton's Second Law | 9-12 | Engineering, Physics |
| Navigation compass challenge | Compass | 5-ESS1-2: Earth's position | 3-5 | STEM |
| Earthquake shake table model | Accelerometer | MS-ESS3-2: Natural hazards | 6-8 | Engineering, Geoscience |
| Multi-sensor weather station | Temp + Light + Sound | MS-ESS2-5: Weather data collection | 6-8 | Environmental Tech, Data Science |
| Comfort index (temp + light + noise) | Temp + Light + Microphone | MS-ETS1-4: Design optimization | 6-8 | Architecture, Building Tech |
Alert when temperature is above 30°C AND light level is below 50 (door closed, overheating risk).
basic.forever(function () {
let temp = input.temperature()
let light = input.lightLevel()
if (temp > 30 && light < 50) {
basic.showIcon(IconNames.Sad)
music.playTone(880, 500)
} else {
basic.showIcon(IconNames.Happy)
}
basic.pause(1000)
})
from microbit import *
import music
while True:
temp = temperature()
light = display.read_light_level()
if temp > 30 and light < 50:
display.show(Image.SAD)
music.play(["A5:500"])
else:
display.show(Image.HAPPY)
sleep(1000)
Show compass direction only when the device is moving (shake detected). Stationary = show "—".
let moving = false
input.onGesture(Gesture.Shake, function () {
moving = true
})
basic.forever(function () {
if (moving) {
let heading = input.compassHeading()
if (heading < 45 || heading > 315) { basic.showString("N") }
else if (heading < 135) { basic.showString("E") }
else if (heading < 225) { basic.showString("S") }
else { basic.showString("W") }
moving = false
} else {
basic.showString("-")
}
basic.pause(500)
})
from microbit import *
compass.calibrate()
while True:
if accelerometer.was_gesture("shake"):
heading = compass.heading()
if heading < 45 or heading > 315:
display.show("N")
elif heading < 135:
display.show("E")
elif heading < 225:
display.show("S")
else:
display.show("W")
else:
display.show("-")
sleep(300)
Score 0-9 based on: temperature (20-25°C ideal), light level (100-200 ideal), noise level (below 80 ideal). Display score on LED.
from microbit import *
def comfort_score():
score = 0
# Temperature: ideal 20-25C
t = temperature()
if 20 <= t <= 25: score += 3
elif 18 <= t <= 28: score += 2
else: score += 0
# Light: ideal 100-200
light = display.read_light_level()
if 100 <= light <= 200: score += 3
elif 50 <= light <= 250: score += 2
else: score += 0
# Sound: ideal below 80
sound = microphone.sound_level()
if sound < 80: score += 3
elif sound < 150: score += 2
else: score += 0
return score
while True:
s = comfort_score()
display.show(str(s))
sleep(2000)
CRAFT PD Series · UCF DRACO Lab & School of Teacher Education