๐Ÿ”ฆ Level 2 โ€“ Sensors & IR Control

Project 2.4: "Musical Touch Sensor"

Project 2.4 โ€“ Musical Touch Sensor

ย 

๐ŸŽฏ What youโ€™ll learn

  • Detect touch input using the TTP223 sensor.
  • Play notes on a buzzer.
  • Map touches to different sounds or patterns.
  • Build a simple musical instrument.

๐Ÿงฑ Blocks glossary used

  • pinX = machine.Pin(X, machine.Pin.IN) โ†’ Setup touch input.
  • pinX.value() โ†’ Read touch state (0 or 1).
  • midi = music.MIDI(pin) โ†’ Setup buzzer output.
  • midi.pitch_time(freq, ms) โ†’ Play a note at frequency for duration.
  • time.sleep() โ†’ Delay.
  • while True: โ†’ Infinite loop.

๐Ÿงฐ What you need

  • D1 R32 board.
  • TTP223 touch sensor (Pin 2).
  • Buzzer (Pin 26).

โœ… Before you start

print("Ready!")  # Confirm serial monitor works

๐ŸŽฎ Microprojects


๐ŸŽฎ Microproject 2.4.1 โ€“ Simple contact detection

Idea: Detect touch and print state.
Pins: Touch=2.

# Simple contact detection

import machine, time                          # Import pin control and time

pin2 = machine.Pin(2, machine.Pin.IN)         # Touch sensor input on pin 2

print("Touch detection ready")                # Serial start message

while True:                                   # Infinite loop
    state = pin2.value()                      # Read sensor state (0 or 1)
    print("Touch state:", state)              # Print state
    time.sleep(0.2)                           # Delay for stability
  • Reflection: You can see the sensor change from 0 to 1 when touched.
  • Challenge: Add an LED on pin 5 that turns ON when touched.

๐ŸŽฎ Microproject 2.4.2 โ€“ Different notes per tap

Idea: Touch plays a single note.
Pins: Touch=2, Buzzer=26.

# Different notes per tap

import machine, time, music                   # Import pin control, time, and music

pin2 = machine.Pin(2, machine.Pin.IN)         # Touch sensor input
midi = music.MIDI(26)                         # Buzzer output on pin 26

print("Touch notes ready")

while True:
    if pin2.value() == 1:                     # If touch detected
        midi.pitch_time(440, 200)             # Play A4 (440 Hz) for 200 ms
        print("Note A4 played")               # Serial confirmation
        time.sleep(0.3)                       # Prevent multiple triggers
    time.sleep(0.05)                          # Fast scan delay
  • Reflection: A single touch produces a clear note.
  • Challenge: Add button logic: first touch plays A4, second touch plays C5.

๐ŸŽฎ Microproject 2.4.3 โ€“ Simple piano 4 notes

Idea: Touch plays a short scale (C, D, E, F).
Pins: Touch=2, Buzzer=26.

# Simple piano 4 notes

import machine, time, music

pin2 = machine.Pin(2, machine.Pin.IN)         # Touch sensor input
midi = music.MIDI(26)                         # Buzzer output

print("4-note piano ready")

while True:
    if pin2.value() == 1:                     # If touch detected
        midi.pitch_time(262, 200)             # C4
        time.sleep(0.05)
        midi.pitch_time(294, 200)             # D4
        time.sleep(0.05)
        midi.pitch_time(330, 200)             # E4
        time.sleep(0.05)
        midi.pitch_time(349, 200)             # F4
        print("4-note sequence played")
        time.sleep(0.3)                       # Prevent repeat
    time.sleep(0.05)
  • Reflection: A touch triggers a mini piano scale.
  • Challenge: Add G4 to extend the scale.

๐ŸŽฎ Microproject 2.4.4 โ€“ Tactile rhythmic pattern

Idea: Touch plays a rhythm (taโ€‘taโ€‘taa).
Pins: Touch=2, Buzzer=26.

# Tactile rhythmic pattern

import machine, time, music

pin2 = machine.Pin(2, machine.Pin.IN)         # Touch sensor
midi = music.MIDI(26)                         # Buzzer

print("Rhythm pattern ready")

while True:
    if pin2.value() == 1:                     # If touch detected
        midi.pitch_time(440, 150)             # Short note
        time.sleep(0.1)
        midi.pitch_time(440, 150)             # Short note
        time.sleep(0.1)
        midi.pitch_time(440, 400)             # Long note
        print("Pattern ta-ta-taa")
        time.sleep(0.3)                       # Prevent repeat
    time.sleep(0.05)
  • Reflection: A single touch plays a rhythm with short and long notes.
  • Challenge: Add a second rhythm pattern triggered by a second touch.

๐ŸŽฎ Microproject 2.4.5 โ€“ Complete musical instrument

Idea: Touch alternates between note mode and rhythm mode.
Pins: Touch=2, Buzzer=26.

# Complete musical instrument

import machine, time, music

pin2 = machine.Pin(2, machine.Pin.IN)         # Touch sensor input
midi = music.MIDI(26)                         # Buzzer output

mode = 0                                      # Mode variable (0=notes, 1=rhythm)
print("Instrument ready (mode 0)")

while True:
    if pin2.value() == 1:                     # If touch detected
        mode = 1 - mode                       # Toggle mode
        print("Mode changed:", mode)

        if mode == 0:                         # Note mode
            midi.pitch_time(262, 200)         # C4
            time.sleep(0.05)
            midi.pitch_time(330, 200)         # E4
            time.sleep(0.05)
            midi.pitch_time(392, 200)         # G4
            print("Note mode sequence")

        else:                                 # Rhythm mode
            midi.pitch_time(440, 150)         # Short note
            time.sleep(0.1)
            midi.pitch_time(440, 150)         # Short note
            time.sleep(0.1)
            midi.pitch_time(440, 400)         # Long note
            print("Rhythm mode pattern")

        time.sleep(0.3)                       # Prevent repeat
    time.sleep(0.05)
  • Reflection: The instrument now has two modes controlled by touch.
  • Challenge: Add a third mode for a longer melody.

ย 

โœจ Main Project โ€“ Musical Touch Sensor

๐Ÿ”ง Blocks steps

  • Touch input: Detect contact with pin2.value().
  • Music output: Play notes with music.MIDI and pitch_time.
  • Logic: Switch between modes (notes vs rhythm).
  • Serial: Print feedback for clarity.

๐Ÿ MicroPython code (lineโ€‘byโ€‘line comments)

# Project 2.4 โ€“ Musical Touch Sensor (Main Project)

import machine, time, music                   # Import pin control, time delays, and music library

pin2 = machine.Pin(2, machine.Pin.IN)         # Setup touch sensor input on pin 2
midi = music.MIDI(26)                         # Setup buzzer output on pin 26 using MIDI

mode = 0                                      # Mode variable: 0 = notes, 1 = rhythm
print("Musical Touch Main ready (mode 0)")    # Serial start message

while True:                                   # Infinite loop
    if pin2.value() == 1:                     # If touch detected (sensor returns 1)
        mode = 1 - mode                       # Toggle mode (switch between 0 and 1)
        print("Mode changed:", mode)          # Print current mode

        if mode == 0:                         # If mode is 0 (notes)
            midi.pitch_time(262, 200)         # Play C4 for 200 ms
            time.sleep(0.05)                  # Short pause
            midi.pitch_time(330, 200)         # Play E4 for 200 ms
            time.sleep(0.05)                  # Short pause
            midi.pitch_time(392, 200)         # Play G4 for 200 ms
            print("Note mode sequence played")# Serial confirmation

        else:                                 # If mode is 1 (rhythm)
            midi.pitch_time(440, 150)         # Short note (ta)
            time.sleep(0.1)                   # Short pause
            midi.pitch_time(440, 150)         # Short note (ta)
            time.sleep(0.1)                   # Short pause
            midi.pitch_time(440, 400)         # Long note (taa)
            print("Rhythm mode pattern played") # Serial confirmation

        time.sleep(0.3)                       # Delay to prevent multiple triggers from one touch

    time.sleep(0.05)                          # Fast scan delay
  • Reflection: The program now lets you switch between two musical modes with a single touch.
  • Challenge: Add a third mode (mode=2) that plays a longer melody, for example C4โ€“D4โ€“E4โ€“F4โ€“G4.

๐Ÿ•ต๏ธ Debugging

๐Ÿž Debugging 2.4.A โ€“ False tactile positives

  • Symptom: Touch detected even when you donโ€™t press the sensor.
  • Cause: Electrical noise or high sensitivity.
  • Fix: Add a small delay or debounce logic.
time.sleep(0.1)   # Increase delay to filter out false signals
  • Why it works: Slower scanning reduces false positives caused by noise.

๐Ÿž Debugging 2.4.B โ€“ Notes do not respond

  • Symptom: No sound when touched.
  • Cause: Wrong pin or missing ground connection.
  • Fix:
midi = music.MIDI(26)   # Confirm buzzer is wired to pin 26
# Check that sensor, buzzer, and board share the same ground
  • Why it works: Correct wiring and shared ground ensure the buzzer receives signals.

โœ… Final checklist

  • Touch input works with Pin(2, IN).
  • Notes play correctly in mode 0.
  • Rhythm pattern plays in mode 1.
  • Mode toggle works with each touch.
  • Serial feedback prints clear messages.

๐Ÿ“š Extras

  • Student tip: Try different frequencies (e.g., 523 Hz for C5) to explore higher notes.
  • Instructor tip: Show how timing changes rhythm feel.
  • Glossary: Touch input, debounce, pitch, mode toggle.
  • Mini tip: If sensor outputs inverted values, adjust logic to check for 0 instead of 1.
On this page