将声音传感器和PCF8591的电源(VCC)接入2,4号引脚(5v)
将PCF8591的GND接入6号引脚,声音传感器的GND接入9号引脚
将树莓派的SDA引脚(3号),和SCL引脚(5号)与PCF8591相连
将声音传感器的AOUT与PCF8591的0号输入引脚AIN0连接
配置i2c
$ sudo raspi-config
选择3 Interfacing Options
安装i2c通信协议包
$sudo apt-get install i2c-tools
通过sudo i2cdetect -y 1查询PCF8591的地址
图示为0x48
import time
import RPi.GPIO as GPIO
import smbus
GPIO.setmode(GPIO.BCM)
address=Ox48
bus=smbus.SMBus(1)
def loop_print():
while True:
bus.write_byte(address,Ox40)
sound=bus.read_byte(address)
if sound:
print("the power of the sound is %lf"%sound)
time.sleep(0.2)
try:
loop_print()
except KeyboardInterrupt:
print("end")
finally:
GPIO.cleanup()
树莓派4B 声音传感器AO 模块演示