需要准备的东西
1.树莓派3b+;
2.噪声传感器,我用的是串口/TTL款的,5V供电;
硬件接线
## 实物接线图
树莓派串口配置
此节参考添加链接描述
(1)修改cmdline.txt文件
编辑cmdline.txt文件
$ sudo nano /boot/cmdline.txt
原来cmdline.txt文件的内容如下:
console=serial0,115200 console=tty1 root=PARTUUID=a05c3c8f-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
将有关console的内容全部删掉,修改后的cmdline.txt文件内容如下:
root=PARTUUID=a05c3c8f-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles
(2)关闭板载蓝牙
禁用蓝牙功能
$ sudo systemctl disable hciuart
编辑config.txt文件
$ sudo nano /boot/config.txt
增加如下一行:
dtoverlay=pi3-disable-bt
重启树莓派!!!
(先重启再进行下一步)
(3)禁用串口的控制台功能
编辑config.txt文件,增加如下一行:
enable_uart=1
python代码
import time
import serial
import sys
import RPi.GPIO as GPIO
port="/dev/ttyAMA0"
usart=serial.Serial(port,9600,timeout=None)
usart.flushInput()
sendbuf = bytearray.fromhex("02 03 00 00 00 01 84 39")## 标题
while True:
usart.write(sendbuf)
recvbuf = bytearray(usart.read(7))
b1 = int(recvbuf[3])
b0 = int(recvbuf[4])
result = (b1<<8) | b0
print('noise=',(result-200.0)/10.0,'dB')
time.sleep(.5)
GPIO.cleanup()
运行结果
实验室环境噪声50左右,喊话后可以到70多分贝。