Raspberry Pi 3b+ serial port/TTL acquisition noise sensor PG-760/ZY-C python program

Things to prepare
1. Raspberry Pi 3b+;
2. Noise sensor, I use serial/TTL type, 5V power supply;

hardware wiring

insert image description here## Physical wiring diagram

insert image description here

Raspberry Pi serial port configuration

This section refers to adding a link description

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 code

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()

operation result

insert image description here
The ambient noise in the laboratory is about 50, and it can reach more than 70 decibels after shouting.

Guess you like

Origin blog.csdn.net/jinanhezhuang/article/details/115170738