Skip to content

Activity

Sensory toy

Beginner | MakeCode, Python | Accelerometer, LED display, Speaker | Input/output, Product design, Sensors

Step 1: Make it

What is it?

Make a sensory toy that responds to movement with light and sound.

Introduction

Coding guide

What you'll learn

  • How to use the micro:bit's built-in accelerometer sensor inputs and expressive sounds as outputs to create a toy that could help learners who respond well to stimulation through touch, light and sound. Different sounds are played and pictures displayed depending on how you move it.

How it works

  • The micro:bit's built-in accelerometer sensor measures forces.
  • The micro:bit uses readings from the accelerometer to trigger events when you move it in different ways.
  • If you shake the micro:bit, it plays a giggle sound on the built-in speaker output and shows a smile on the LED display.
  • Different icon and sound outputs are triggered by different movements such as tilting the micro:bit left and right or placing it with the logo pointing up or down.

What you need

  • V2 micro:bit (or MakeCode simulator)
  • MakeCode or Python editor
  • battery pack (optional)

Step 2: Code it

1from microbit import *
2import audio
3
4while True:
5    if accelerometer.was_gesture('shake'):
6        display.show(Image.SURPRISED)
7        audio.play(Sound.GIGGLE)
8    if accelerometer.was_gesture('up'):
9        display.show(Image.HAPPY)
10        audio.play(Sound.HELLO)
11    if accelerometer.was_gesture('down'):
12        display.show(Image.ASLEEP)
13        audio.play(Sound.YAWN)
14    if accelerometer.was_gesture('left'):
15        display.show(Image.ARROW_W)
16        audio.play(Sound.SLIDE)
17    if accelerometer.was_gesture('right'):
18        display.show(Image.ARROW_E)
19        audio.play(Sound.SOARING)
20

Step 3: Improve it

  • Add different pictures and sounds when you press button A or B or touch the logo.
  • Experiment with different expressive sounds like 'giggle', 'happy' and 'twinkle'.
  • Add an animation or your own pictures to go with each movement.
  • Create a case or holder for the micro:bit and battery pack that allows the sound to come out and the LED display to shine though, perhaps diffusing the LED light slightly.
  • If you're a teacher, you may also be interested in our Sensory classroom lessons.