MI-4micro:bitBeginner

Ticklish micro:bit: A Board That Giggles When You Touch the Logo

Touch the gold logo above the display: the micro:bit giggles and its face wiggles left and right, then calms down when you take your finger away. pin_logo.is_touched senses the touch and audio.play plays the sound. No extra parts are needed.

0.0(0)
Ticklish micro:bit: A Board That Giggles When You Touch the Logo
Make a board that laughs when you touch it. You learn to read the touch logo, play a built-in sound on the onboard speaker, run an animation while the sound plays, shift an image across the display and set the volume.

Video

Circuit Diagram

Ticklish micro:bit: A Board That Giggles When You Touch the Logo - Circuit Diagram

Source Code

MicroPython
1# ─────────────────────────────────────────
2#   BlueGrays · bluegrays.com
3#   Ticklish micro:bit: A Board That Giggles When You Touch the Logo
4# ─────────────────────────────────────────
5# Touch the gold logo above the display and the micro:bit giggles while
6# its face wiggles left and right. Take your finger away and it goes back
7# to its calm face. No extra parts are needed.
8#
9# Pins:
10#   LED -> Onboard 5x5 LED display
11#   LOGO -> Touch logo
12#   SPEAKER -> Onboard speaker
13
14from microbit import *
15
16# ============ SETTINGS ============
17VOLUME = 150         # Volume, 0-255
18WIGGLES = 6          # Wiggles per tickle
19WIGGLE_MS = 120      # One wiggle step (ms)
20LOOP_MS = 50         # Loop pause (ms)
21
22set_volume(VOLUME)
23display.show(Image.SMILE)
24
25while True:
26    if pin_logo.is_touched():
27        # The sound plays in the background while the face shifts left and right.
28        audio.play(Sound.GIGGLE, wait=False)
29        for i in range(WIGGLES):
30            display.show(Image.HAPPY.shift_left(1))
31            sleep(WIGGLE_MS)
32            display.show(Image.HAPPY.shift_right(1))
33            sleep(WIGGLE_MS)
34        display.show(Image.SMILE)
35    sleep(LOOP_MS)
36

FAQ

Which other built-in sounds are there?

The micro:bit V2 has ten built-in sounds: Sound.GIGGLE, HAPPY, HELLO, MYSTERIOUS, SAD, SLIDE, SOARING, SPRING, TWINKLE and YAWN. Write Sound.YAWN instead of Sound.GIGGLE in the code, for example, and the board yawns when touched.

The sound is too loud or too quiet. How do I change it?

VOLUME goes from 0 to 255. Use a small value such as 60 in class or at night and 255 in a noisy place. set_volume applies the value to the speaker; 0 turns the sound off completely.

How do I put this code on the micro:bit?

The code is MicroPython. Open the micro:bit Python Editor (python.microbit.org) on a computer, delete the code that is there and paste this one. Connect the board with a USB cable and press Send to micro:bit. If that button does not work, use Save to download the .hex file and copy it to the drive called MICROBIT. You can rebuild the same project with blocks in MakeCode, but this code cannot be pasted there.

Does it work on the older micro:bit (V1) too?

The code is written for the micro:bit V2. Projects that only use the LED display, buttons A and B and the accelerometer also run on a V1. The speaker, the microphone and the touch logo exist only on the V2, so code that uses them gives an error on a V1. If your board has a speaker on the back and a gold logo on the front, it is a V2.