Playing with Raspberry Pi (20) - Noise Monitoring

Playing with Raspberry Pi (20) - Noise Monitoring

Noise pollution is a very annoying thing, and prolonged exposure to noise can not only upset people, but even affect our physical and mental health. In this section, with the help of the sound sensor, we can develop a small noise monitoring tool that will alert you when the ambient noise is too loud.

1. Sound sensor

A sound sensor module is a component that converts sound waves into electrical signals. Use it to easily obtain the sound intensity in the environment. In this experiment, the sound sensor module we used is shown in the following figure:

As shown in the figure, this sensor module has 4 external pins, among which the AO pin is the analog quote output pin, and the DO pin is the digital quote output pin. The G pin is used for grounding, and the + pin is connected to the 5V power supply.

There are also two LED indicators on the sensor module, LED1 is the power indicator, as long as the positive and negative poles of the sensor are connected normally, this indicator will be on. LED2 is the sound detection indicator. When the ambient sound intensity does not exceed a certain value, the indicator will turn off. When the ambient sound intensity exceeds a certain value, the indicator will be on. The specific threshold can be adjusted by adjusting the variable resistor in the blue part of the figure.

The sound sensor module we used in this experiment can output both digital signals and analog signals. For digital signals, we can directly connect it to the GPIO of the Raspberry Pi for reception. Since the Raspberry Pi does not have a built-in digital-to-analog conversion module, So for the analog signal, we need to use the PCF8591 digital-to-analog conversion module to read the analog signal.

2. Experiment

First, confirm the pins we want to use. The digital signal pin of the sound sensor can be directly connected to the GPIO17 (BCM encoding) of the Raspberry Pi, and the analog quote pin is connected to the AINO of the PCF8591. The following table:

PCF8591 and Raspberry Pi:

PCF8591 raspberry pie
SCL SCL
SDA SDA
GND GND
VCC +5V

Sound Sensor Module with Raspberry Pi:

sound sensor raspberry pie
G GND
+ +5V
DO GPIO17 (BCM code, corresponding to physical code 11)

Sound Sensor with PCF8591:

sound sensor PCF8591
TO THE AIN0

Write the sample code as follows:

#SMBus (System Management Bus,系统管理总线) 
import smbus   #在程序中导入“smbus”模块
import RPi.GPIO as GPIO 
import time

bus = smbus.SMBus(1)         #创建一个smbus实例

# 通过PCF8591读取模拟信号

# 声音强度的模拟数据
def readData():
    #发送一个控制字节到设备 表示要读取AIN0通道的数据
    bus.write_byte(0x48,0x40)   
    bus.read_byte(0x48)         # 空读一次,消费掉无效数据
    return bus.read_byte(0x48)  # 返回某通道输入的模拟值A/D转换后的数字值

# 通过GPIO读取数字信号

# 设置使用的引脚编码模式
GPIO.setmode(GPIO.BOARD)
# 数字输出引脚 BCM 17
P = 11

# 引脚初始化
GPIO.setup(P, GPIO.IN)

while True:
    print('--------分割线----------')
    print('环境声音强度数字信号:', GPIO.input(P))
    data = readData()
    print('环境声音强度模拟信号:', readData())
    if data < 130:
        print("噪声过大,请注意!!!!!!!!!")
    time.sleep(2)

In the above code, if the analog signal is set to be less than 130, it means that the ambient sound is too strong. It should be noted that if you run the above program and find that the value is always small, it may be caused by the high sensitivity setting. You can adjust the variable resistor to get the appropriate sensitivity.

The effect of running the code is shown in the following figure:

There is nothing particularly novel about this experiment, and the techniques used have been described in detail in previous blogs in this series.

Focus on technology, understand love, be willing to share, be a friend

QQ:316045346

{{o.name}}
{{m.name}}

Guess you like

Origin my.oschina.net/u/2340880/blog/5311091