亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Manufacturing Technology >> 制造工艺

树莓派 DS18B20 温度传感器测试与 HC-SR04 超声波测距实战

树莓派 DS18B20 温度传感器测试

  1. sudo modprobe w1-gpio
  2. sudo modprobe w1-therm
  3. cd /sys/bus/w1/devices
  4. ls
  5. cd 28-xxxx(替换为出现的序列号)
  6. cat w1_slave

1-Wire 接口可能有点脆弱,但能可靠报告温度值是否可用。因为传感器以简单文本文件暴露数据,只需读取文件即可。

第一行以 YES 或 NO 结尾。若为 YES,第二行包含千分之一度 Celsius 的温度。示例中传感器报告 20.687°C 然后 26.125°C。

连接多个传感器时,每个将作为单独的 28-xxxx 目录出现。逐个插入,记录生成的目录,给传感器贴标签以备后续参考。

超声波传感器应用

HC‑SR04 超声波传感器从树莓派 +5 V 引脚(引脚 2)取电。触发引脚(GPIO 23)发起测量,回波引脚返回高电平脉冲,脉宽对应往返距离。因 GPIO 仅耐 3.3 V,需分压电阻保护回波线。

示例代码

#!/usr/bin/python
# Import required Python libraries
import time
import RPi.GPIO as GPIO

# Use BCM GPIO references instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24

print("Ultrasonic Measurement")

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO, GPIO.IN)      # Echo

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

# Send 10us pulse to trigger
GPIO.output(GPIO_TRIGGER, True)
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)

start = time.time()
while GPIO.input(GPIO_ECHO) == 0:
    start = time.time()
while GPIO.input(GPIO_ECHO) == 1:
    stop = time.time()

# Calculate pulse length
elapsed = stop - start
# Distance pulse travelled in that time is time multiplied by the speed of sound (cm/s)
distance = elapsed * 34000
# That was the distance there and back so halve the value
distance = distance / 2
print("Distance : %.1f" % distance)

# Reset GPIO settings
GPIO.cleanup()

超声波触发火箭发射器

以下脚本利用超声波传感器在物体接近用户定义距离时触发火箭发射。同时拍照并点亮 LED 作为视觉指示。

#!/usr/bin/python
# Author : Julian and Kyle Milligan
# Date : 09/01/2013
# Import required Python libraries
import time
import RPi.GPIO as GPIO
from subprocess import call

# Use BCM GPIO references instead of physical pin numbers
GPIO.setmode(GPIO.BCM)

# Define GPIO to use on Pi
GPIO_TRIGGER = 23
GPIO_ECHO = 24
GPIO_FIRE = 4

print("Ultrasonic Measurement")

# Set pins as output and input
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)  # Trigger
GPIO.setup(GPIO_ECHO, GPIO.IN)      # Echo
GPIO.setup(GPIO_FIRE, GPIO.OUT)     # Fire

# Set trigger to False (Low)
GPIO.output(GPIO_TRIGGER, False)

# Allow module to settle
time.sleep(0.5)

# Set distance to trigger
setdistance = input('Please enter a value to trigger the camera:')

while True:
    time.sleep(0.1)
    GPIO.output(GPIO_TRIGGER, True)
    time.sleep(0.00001)
    GPIO.output(GPIO_TRIGGER, False)
    start = time.time()
    s = start
    quit = 0
    while quit == 0:
        quit = GPIO.input(GPIO_ECHO)
        start = time.time()
        if start - s > 0.1:
            quit = 1
        if start - s < 0.1:
            while GPIO.input(GPIO_ECHO) == 1:
                stop = time.time()
            elapsed = stop - start
            distance = elapsed * 34300
            distance = distance / 2
            print("Distance : %.1f" % distance)
            if distance < setdistance:
                call(["raspistill -o image.jpg"], shell=True)  # take a picture with Pi Camera
                print("Fire")
                GPIO.output(GPIO_FIRE, True)  # Turn on GPIO pin 7 fire the rockets

# Reset GPIO settings
GPIO.cleanup()

树莓派 USB 摄像头流媒体

从 USB 摄像头流式传输视频,确保树莓派联网。连接近期摄像头并按官方指南设置 MJPEG 流媒体。深度解析请见 DS18B20 传感器测试文档。

制造工艺

  1. 树莓派多路DS18B20 1-Wire温度监测:接线与编程全指南
  2. 树莓派 GPIO 直连 DS1820 单总线温度传感器指南
  3. 树莓派 3 接 HC‑SR04 超声波测距传感器完全指南
  4. 树莓派接 KY‑033 循迹/门状态传感器:简易 Python 实现
  5. 树莓派接入 DS18B20 单总线数字温度计:分步实战指南
  6. 树莓派 + MCP9808 + InfluxDB + Grafana:家庭温度监测系统
  7. 掌握树莓派 GPIO:在 B+/Model 2 上接入 PIR 运动传感器
  8. 树莓派家庭安防系统:PIR 运动检测 + 摄像头邮件报警
  9. HC‑SR501 PIR 运动传感器与树莓派接线、校准与 Python 编程全指南
  10. 低成本情绪传感器与 EEG:实时情绪检测硬件方案
  11. 数字光照传感器驱动 LED:Windows 10 IoT Core 树莓派实战
  12. Turck TB3-CP80:感应/磁性/电容/光电传感器精准测试盒