माइक्रोफ़ोन सेंसर और मेलोडी

माइक्रोफ़ोन सेंसर

माइक्रो पाइथन कोड का एक उदाहरण Raspberry Pi Pico 2W माइक्रोकंट्रोलर पर चल रहा है ब्रेडबोर्ड पर माइक्रोफोन साउंड सेंसर मॉड्यूल, पैसिव पीजो बजर और OLED डिस्प्ले मॉड्यूल (SSD1306) से जुड़ा हुआ है। बज़र पर लगे माइक्रोफ़ोन द्वारा मापी गई ध्वनि आवृत्तियों को पुनः बजाकर डिस्प्ले पर प्रदर्शित करना।

एक माइक्रोफोन ध्वनि सेंसर अंतर्निर्मित माइक्रोफोन और ऑनबोर्ड प्रोसेसिंग सर्किट का उपयोग करके पर्यावरणीय ध्वनि तरंगों को विद्युत संकेतों में परिवर्तित करता है।. ध्वनि तरंगें हवा में फैलती हैं और मॉड्यूल के माइक्रोफोन के अंदर मौजूद एक छोटे, लचीले डायाफ्राम से टकराती हैं।. डायाफ्राम में होने वाले उतार-चढ़ाव से ऐसे विद्युत संकेत उत्पन्न होते हैं जो ध्वनि की आवृत्ति और तीव्रता से मेल खाते हैं।. इस स्क्रिप्ट को चलाएं और साथ ही किसी दूसरे डिवाइस पर melody.py को भी चलाएं।. दोनों उपकरणों को एक दूसरे के करीब लाएं और माइक्रोफ़ोन को सीधे उस उपकरण के बजर के ऊपर रखें जो धुन बजा रहा है।. अगर कमरे में ज्यादा शोर नहीं है, तो कम से कम कुछ सुर तो सुनाई देने ही चाहिए।.

संकेत देना

किसी अन्य डिवाइस पर नया कोड अपलोड किए बिना melody.py को तुरंत चलाने के लिए, 'शुरुआत करना ' अनुभाग में वर्णित वैकल्पिक विधि का उपयोग करें।.

Thonny में microphone_sensor.py नाम से एक नई फ़ाइल बनाएँ।. नीचे दिए गए कोड को कॉपी करके अपनी बनाई गई फ़ाइल में पेस्ट करें।.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
print("\nMicrophone Sensor")

# DESCRIPTION

# A microphone sound sensor converts environmental sound waves into electrical 
# signals using a built-in microphone and an onboard processing circuit. Sound waves 
# move through the air and hit a tiny flexible diaphragm inside the module's microphone.
# The fluctuations in the diaphragm create electrical signals that match the sound 
# frequency and volume. Run this script, and simultaneously run melody.py on a different 
# device. Bring the devices close to each other and place the microphone directly on top of
# the buzzer of the device playing the melody. If the room is not very noisy, it should pick
# up at least some of the notes.

# CONNECTIONS REQUIRED

# Connect Microphone Sensor (KY-037) module 
# (it includes a cylindrical mic with black circular pad on top)
#   Pins: GND => GND, VCC => VCC, S => GP28

from time import sleep
from machine import Pin, ADC
from oled import OLED
from buzzer import Buzzer
from value_filter import ValueFilter
from helper import interpolate

mic_sensor = ADC(Pin(28))
value_filter = ValueFilter(filter="SMA", window_size=5)
oled = OLED(scl_pin=15, sda_pin=14)
buzzer = Buzzer(pwm_pin=16)

sensor_min = 8000
sensor_max = 13000
frequency = 0

try:
    while True:
        # Use Simple Moving Average filter to smooth out readings
        sensor_value = value_filter.update(mic_sensor.read_u16())

        # Convert sensor_value to frequency when reading is greater than sensor_min
        # and buzzer is not playing (frequency == 0).
        # If buzzer is already playing, don't change frequency.
        # If sensor_value drops below 75% of sensor_min, reset frequency to stop buzzer
        if sensor_value < sensor_min * 0.75: frequency = 0
        elif sensor_value >= sensor_min and frequency == 0:
            frequency = interpolate(sensor_value, sensor_min, sensor_max, 200, 600)

        oled.print(f"Raw: {sensor_value:.0f}", f"Freq: {frequency}")
        buzzer.play_tone(frequency, 0)
        sleep(0.02)

except KeyboardInterrupt:
    print("\nInterrupted by user!")

finally:
    oled.reset()
    buzzer.reset()

किसी अन्य डिवाइस पर melody.py फ़ाइल चलाकर परीक्षण करने के लिए, ऊपर दिए गए राग प्रयोग से melody.py फ़ाइल को दूसरे डिवाइस से जुड़े कंप्यूटर पर चलाएँ।.


शीर्ष पर वापस जाएं

शिक्षकों और छात्रों के लिए व्यावहारिक आविष्कार को अधिक सुलभ बनाना.
© 2026 Inventor Pi, Inc.
Inventor Pi, Inc. एक पंजीकृत 501(सी)(3) संगठन है।