Sound

🎮 Display the Digital Sound State

Goal 

Read the sound sensor and display its raw 0 or 1 value on the PC. 

Concepts 

Digital Input · HIGH/LOW · Loop · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

pin26 = machine.Pin(26, machine.Pin.IN)  # D2 reads the sound sensor

while True:  # Repeats forever

    state = pin26.value()  # Reads the sensor state

    print(state)  # Sends 0 or 1 to the PC

    time.sleep(0.1)  # Waits before reading again

🎮 Detect a Strong Sound

Goal 

Display Sound detected when a strong sound activates the sensor. 

Concepts 

Digital Input · If/Else · Sound Detection · Loop · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

pin26 = machine.Pin(26, machine.Pin.IN)  # D2 reads the sound sensor

while True:  # Repeats forever

    state = pin26.value()  # Reads the sensor state

    if state == 0:  # Checks if sound was detected
        print('Sound detected')  # Reports the sound to the PC

    else:  # Runs when no sound is detected
        print('Quiet')  # Reports the quiet state

    time.sleep(0.1)  # Waits before reading again

🎮 Reduce Repeated Sound Detections

Goal 

Register a new sound only if at least 400 milliseconds have passed since the previous detection. 

Concepts 

Digital Input · Debounce · Timing · Comparison · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

pin26 = machine.Pin(26, machine.Pin.IN)  # D2 reads the sound sensor

last_sound = 0  # Stores the time of the last sound

while True:  # Repeats forever

    state = pin26.value()  # Reads the sensor state

    now = time.ticks_ms()  # Gets the current time

    elapsed = time.ticks_diff(now, last_sound)  # Finds elapsed time

    if state == 0 and elapsed >= 400:  # Checks for a new valid sound

        print('New sound detected')  # Reports the sound to the PC

        last_sound = now  # Saves the detection time

    time.sleep(0.05)  # Waits before reading again

🎮 Count Sounds and Measure Time

Goal 

Count detected sound events and display the time since the previous event. 

Concepts 

Digital Input · Counter · Timing · Debounce · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

pin26 = machine.Pin(26, machine.Pin.IN)  # D2 reads the sound sensor

count = 0  # Stores the number of sound events

last_sound = 0  # Stores the previous sound time

while True:  # Repeats forever

    state = pin26.value()  # Reads the sensor state

    now = time.ticks_ms()  # Gets the current time

    elapsed = time.ticks_diff(now, last_sound)  # Finds elapsed time

    if state == 0 and elapsed >= 400:  # Checks for a new valid sound

        count += 1  # Adds one to the sound counter

        print('Sound:', count)  # Shows the event number

        print('Time:', elapsed, 'ms')  # Shows time since the last event

        last_sound = now  # Saves the current event time

    time.sleep(0.05)  # Waits before reading again

🎮 Sound Activity Monitor

Goal 

Create a sound-event monitor that reports the event number, time between events, and when there has been no detected sound for 5 seconds. 

Concepts 

Digital Input · Counter · Timing · Debounce · Silence Detection · Serial Output

import machine  # Controls the board hardware
import time     # Controls pauses and timing

pin26 = machine.Pin(26, machine.Pin.IN)  # D2 reads the sound sensor

count = 0  # Stores the number of sound events

last_sound = time.ticks_ms()  # Stores the last sound time

silence_reported = False  # Remembers if silence was reported

while True:  # Repeats forever

    state = pin26.value()  # Reads the sensor state

    now = time.ticks_ms()  # Gets the current time

    elapsed = time.ticks_diff(now, last_sound)  # Finds elapsed time

    if state == 0 and elapsed >= 400:  # Checks for a new valid sound

        count += 1  # Adds one to the event counter

        print('Sound event:', count)  # Shows the event number

        print('Time between:', elapsed, 'ms')  # Shows time between sounds

        last_sound = now  # Saves the current sound time

        silence_reported = False  # Allows a new silence report

    if elapsed >= 5000 and silence_reported == False:  # Checks for 5 seconds of silence

        print('Silence for 5 seconds')  # Reports the silence to the PC

        silence_reported = True  # Remembers that silence was reported

    time.sleep(0.05)  # Waits before reading again