通过MCP3002 ADC使用RPi读取麦克风输入值

我正在尝试使用一个拍板开关来打开和关闭Raspberry Pi 3B +上的LED。 我使用的是MAX4466麦克风(不是自己买的,所以不知道确切的型号,但是它是3输出的麦克风-Vcc,GND,Out,看起来与上述型号非常相似)和MCP3002 ADC。

这是我用于RPi的代码(在网上找到): '''

import time
import spidev
import RPi.GPIO as GPIO
import ploting

spi_ch = 0

# Enable SPI
spi = spidev.SpiDev(0,spi_ch)
spi.max_speed_hz = 1200000

def read_adc(adc_ch,vref = 3.3):

# Make sure ADC channel is 0 or 1
if adc_ch != 0:
    adc_ch = 1

# Construct SPI message
#  First bit (Start): Logic high (1)
#  Second bit (SGL/DIFF): 1 to select single mode
#  Third bit (ODD/SIGN): Select channel (0 or 1)
#  Fourth bit (MSFB): 0 for LSB first
#  Next 12 bits: 0 (don't care)
msg = 0b11
msg = ((msg << 1) + adc_ch) << 5
msg = [msg,0b00000000]
reply = spi.xfer2(msg)

# Construct single integer out of the reply (2 bytes)
adc = 0
for n in reply:
    adc = (adc << 8) + n

# Last bit (0) is not part of ADC value,shift to remove it
adc = adc >> 1

# Calculate voltage form ADC value
# voltage = (vref * adc) / 1024
voltage = adc

return voltage


# Report the channel 0 and channel 1 voltages to the terminal
try:
while True:
    adc_0 = read_adc(0)
    adc_1 = read_adc(1)
    print("Ch 0:",round(adc_0,2),"V Ch 1:",round(adc_1,"V")
    time.sleep(0.2)

finally:
GPIO.cleanup()

当我将标准电位计连接到CH0 / 1时,它会读取应该在0到1023之间的值,但是当连接麦克风的输出时,它始终会读取大约相同的值,有时会随着一些随机值(与任何声音检测无关):

Ch 0: 478 V Ch 1: 1018 V
Ch 0: 485 V Ch 1: 1017 V
Ch 0: 511 V Ch 1: 1017 V
Ch 0: 484 V Ch 1: 1016 V
Ch 0: 484 V Ch 1: 1019 V
Ch 0: 486 V Ch 1: 1020 V
Ch 0: 64 V Ch 1: 1016 V
Ch 0: 1020 V Ch 1: 1016 V
Ch 0: 444 V Ch 1: 1017 V
Ch 0: 470 V Ch 1: 1016 V
Ch 0: 463 V Ch 1: 1016 V
Ch 0: 1023 V Ch 1: 1016 V
Ch 0: 463 V Ch 1: 1019 V
Ch 0: 444 V Ch 1: 1017 V
Ch 0: 432 V Ch 1: 1016 V
Ch 0: 487 V Ch 1: 1022 V
Ch 0: 268 V Ch 1: 1020 V
Ch 0: 433 V Ch 1: 1016 V
Ch 0: 455 V Ch 1: 1016 V
Ch 0: 464 V Ch 1: 1022 V
Ch 0: 1023 V Ch 1: 1019 V
Ch 0: 432 V Ch 1: 1020 V
Ch 0: 412 V Ch 1: 1017 V

还尝试通过NE5534放大器连接麦克风,尽管它已经带有内置放大器。 此外,我尝试将麦克风连接到Arduino Uno进行测试,并且效果很好。

有什么想法为什么RPi无法管理读取声音检测?

b0539323789 回答:通过MCP3002 ADC使用RPi读取麦克风输入值

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3152291.html

大家都在问