随着智能家居和物联网(IoT)的迅速发展,树莓派因其强大的扩展性和较低的成本而成为构建这些系统的不二之选。树莓派传感器是连接物理世界与数字世界的桥梁,它们可以帮助你监测环境、控制设备,并实现各种智能应用。以下是20款流行的树莓派传感器,以及如何使用它们来入门智能家居与物联网世界。
1. 温湿度传感器 - DHT11/DHT22
概述:DHT11/DHT22是常见的温湿度传感器,能够提供室内温度和相对湿度的实时数据。
使用场景:环境监测、智能加湿器、温度控制器。
代码示例:
import Adafruit_DHT
sensor = Adafruit_DHT.DHT22(4) # 传感器连接到树莓派的GPIO 4号引脚
temp, hum = Adafruit_DHT.read_retry(sensor)
if temp is not None and hum is not None:
print("Temperature: {:.1f} C".format(temp))
print("Humidity: {:.1f} %".format(hum))
else:
print("Failed to get reading. Try again!")
2. 光敏传感器 - LDR
概述:光敏传感器可以根据光照强度变化输出模拟信号。
使用场景:自动灯光控制、夜间模式触发。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.IN)
while True:
if GPIO.input(27) == GPIO.LOW:
GPIO.output(17, GPIO.HIGH)
else:
GPIO.output(17, GPIO.LOW)
time.sleep(0.1)
3. 红外传感器 - HC-SR501
概述:HC-SR501是一种人体红外传感器,常用于自动开关灯。
使用场景:自动门、智能照明。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN)
while True:
if GPIO.input(23) == GPIO.HIGH:
GPIO.output(18, GPIO.HIGH)
else:
GPIO.output(18, GPIO.LOW)
time.sleep(0.1)
4. 红外遥控接收器 - IRF收发器
概述:用于接收红外遥控信号,可以用来控制电视、空调等设备。
使用场景:构建红外遥控控制智能家居设备。
代码示例:
import irrecv
import time
receiver = irrecv.IRrecv(17)
while True:
if receiver.decode():
print(receiver.value)
time.sleep(0.1)
5. 振动传感器 - MQ-2
概述:MQ-2是一种通用气体传感器,可以检测多种气体,包括酒精、烟雾、天然气等。
使用场景:烟雾报警器、气体泄漏检测。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.IN)
while True:
if GPIO.input(25) == GPIO.HIGH:
print("Detected gas")
else:
print("No gas detected")
time.sleep(0.1)
6. 金属探测传感器 - GP2Y0A02YK0F
概述:GP2Y0A02YK0F是一种接近传感器,可以检测金属物体的接近。
使用场景:自动化装配线、机器人。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(27, GPIO.IN)
while True:
if GPIO.input(27) == GPIO.LOW:
print("Metal detected")
else:
print("No metal detected")
time.sleep(0.1)
7. 霍尔传感器 - ACS712
概述:ACS712是一种霍尔效应电流传感器,可以检测电流的大小。
使用场景:电流监测、电力管理。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(26, GPIO.IN)
while True:
if GPIO.input(26) == GPIO.LOW:
print("Current detected")
else:
print("No current detected")
time.sleep(0.1)
8. 陀螺仪 - MPU-6050
概述:MPU-6050是一种集成加速度计和陀螺仪的传感器,可以用于测量角度和运动。
使用场景:游戏开发、机器人控制。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x68 # MPU-6050 I2C 地址
while True:
bus.write_byte_data(address, 0x3B, 0x00) # 配置加速度计和陀螺仪
ax, ay, az = bus.read_i2c_block_data(address, 0x3B, 6)
gx, gy, gz = bus.read_i2c_block_data(address, 0x43, 6)
print("Accelerometer: x={}, y={}, z={}".format(ax, ay, az))
print("Gyroscope: x={}, y={}, z={}".format(gx, gy, gz))
time.sleep(0.1)
9. 压力传感器 - BMP180
概述:BMP180是一种压力传感器,可以用于测量大气压力。
使用场景:天气预报、海拔测量。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x77 # BMP180 I2C 地址
while True:
bus.write_byte_data(address, 0xF4, 0x34) # 配置测量模式
bus.write_byte_data(address, 0xF5, 0x74) # 配置 oversampling ratio
data = bus.read_i2c_block_data(address, 0xF6, 3) # 读取数据
pressure = (data[0] << 16) + (data[1] << 8) + data[2]
print("Pressure: {:.2f} hPa".format(pressure / 256))
time.sleep(0.1)
10. 距离传感器 - VL53L0X
概述:VL53L0X是一种高精度距离传感器,可以测量物体与传感器之间的距离。
使用场景:自动门、机器人避障。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x29 # VL53L0X I2C 地址
while True:
bus.write_byte_data(address, 0x80, 0x01) # 设置测量模式
bus.write_byte_data(address, 0x81, 0x09) # 设置测量速率
distance = bus.read_byte_data(address, 0x94) # 读取距离
print("Distance: {:.2f} cm".format(distance))
time.sleep(0.1)
11. 霍尔传感器 - HMC5883L
概述:HMC5883L是一种三轴磁力计,可以用于测量磁场方向。
使用场景:指南针、位置定位。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x1E # HMC5883L I2C 地址
while True:
bus.write_byte_data(address, 0x02, 0x00) # 配置测量模式
bus.write_byte_data(address, 0x01, 0x80) # 开启测量
x, y, z = bus.read_i2c_block_data(address, 0x03, 6)
print("Heading: X={}, Y={}, Z={}".format(x, y, z))
time.sleep(0.1)
12. 水平传感器 - MPU-9150
概述:MPU-9150是一种集成了陀螺仪、加速度计和磁力计的传感器。
使用场景:机器人平衡、运动检测。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x68 # MPU-9150 I2C 地址
while True:
bus.write_byte_data(address, 0x3B, 0x00) # 配置传感器
ax, ay, az = bus.read_i2c_block_data(address, 0x3B, 6)
gx, gy, gz = bus.read_i2c_block_data(address, 0x43, 6)
mx, my, mz = bus.read_i2c_block_data(address, 0x01, 6)
print("Accelerometer: x={}, y={}, z={}".format(ax, ay, az))
print("Gyroscope: x={}, y={}, z={}".format(gx, gy, gz))
print("Magnetometer: x={}, y={}, z={}".format(mx, my, mz))
time.sleep(0.1)
13. 气压计 - BMP280
概述:BMP280是一种高精度压力传感器,可以用于测量大气压力和温度。
使用场景:气象站、无人机高度控制。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x76 # BMP280 I2C 地址
while True:
bus.write_byte_data(address, 0xF4, 0x27) # 配置测量模式
bus.write_byte_data(address, 0xF5, 0x73) # 设置 oversampling ratio
data = bus.read_i2c_block_data(address, 0xF7, 8) # 读取数据
pressure = (data[0] << 16) + (data[1] << 8) + data[2]
temperature = (data[3] << 16) + (data[4] << 8) + data[5]
print("Pressure: {:.2f} hPa".format(pressure / 256))
print("Temperature: {:.2f} C".format(temperature / 256))
time.sleep(0.1)
14. 红外遥控发射器 - IRLED
概述:IRLED是一种用于发送红外信号的组件,可以与红外遥控接收器配合使用。
使用场景:自定义红外遥控、智能家居控制。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
# 发送红外信号
def send_ir_signal(code):
GPIO.output(22, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(22, GPIO.LOW)
time.sleep(0.001)
for bit in code:
GPIO.output(22, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(22, GPIO.LOW)
time.sleep(0.001)
if bit:
time.sleep(0.01)
else:
time.sleep(0.0005)
# 发送特定的红外代码
send_ir_signal([1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0])
15. 红外遥控接收器 - IRF发射器
概述:IRF发射器是一种用于发送红外信号的模块,可以与红外接收器配合使用。
使用场景:自定义红外遥控、智能家居控制。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
# 发送红外信号
def send_ir_signal(code):
GPIO.output(18, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(18, GPIO.LOW)
time.sleep(0.001)
for bit in code:
GPIO.output(18, GPIO.HIGH)
time.sleep(0.001)
GPIO.output(18, GPIO.LOW)
time.sleep(0.001)
if bit:
time.sleep(0.01)
else:
time.sleep(0.0005)
# 发送特定的红外代码
send_ir_signal([1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0])
16. 声波测距传感器 - HC-SR04
概述:HC-SR04是一种超声波测距传感器,可以测量物体与传感器之间的距离。
使用场景:自动门、机器人避障。
代码示例:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.IN)
while True:
GPIO.output(18, GPIO.LOW)
time.sleep(0.002)
GPIO.output(18, GPIO.HIGH)
time.sleep(0.00001)
GPIO.output(18, GPIO.LOW)
while GPIO.input(23) == GPIO.LOW:
pulse_start = time.time()
while GPIO.input(23) == GPIO.HIGH:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
print("Distance: {:.2f} cm".format(distance))
time.sleep(0.1)
17. 霍尔传感器 - LPS331AP
概述:LPS331AP是一种压力传感器,可以用于测量大气压力和温度。
使用场景:气象站、无人机高度控制。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x5C # LPS331AP I2C 地址
while True:
bus.write_byte_data(address, 0x00, 0x34) # 配置测量模式
bus.write_byte_data(address, 0x01, 0x78) # 设置 oversampling ratio
data = bus.read_i2c_block_data(address, 0x00, 8) # 读取数据
pressure = (data[0] << 16) + (data[1] << 8) + data[2]
temperature = (data[3] << 16) + (data[4] << 8) + data[5]
print("Pressure: {:.2f} hPa".format(pressure / 256))
print("Temperature: {:.2f} C".format(temperature / 256))
time.sleep(0.1)
18. 温湿度传感器 - SHT31
概述:SHT31是一种高精度的温湿度传感器。
使用场景:环境监测、智能加湿器。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x44 # SHT31 I2C 地址
while True:
bus.write_byte_data(address, 0x24, 0x06) # 配置测量模式
time.sleep(0.5)
data = bus.read_i2c_block_data(address, 0x00, 6) # 读取数据
temp = ((data[0] << 8) + data[1]) * 0.01
hum = ((data[2] << 8) + data[3]) * 0.01
print("Temperature: {:.2f} C".format(temp))
print("Humidity: {:.2f} %".format(hum))
time.sleep(2)
19. 陀螺仪 - BNO055
概述:BNO055是一种集成了陀螺仪、加速度计和磁力计的传感器。
使用场景:机器人平衡、运动检测。
代码示例:
import smbus
import time
bus = smbus.SMBus(1)
address = 0x28 # BNO055 I2C 地址
while True:
bus.write_byte_data(address, 0x3C, 0x01) # 设置模式为基本配置
bus.write_byte_data(address, 0x3B, 0x08) # 设置陀螺仪配置
bus.write_byte_data(address, 0x3A, 0x00) # 设置加速度计配置
data = bus.read_i2c_block_data(address, 0x08, 6)
gx = ((data[0] << 8) + data[1]) * 16.4
gy = ((data[2] << 8) + data[3]) * 16.4
gz = ((data[4] << 8) + data[5]) * 16.4
print("Gyroscope: X={}, Y={}, Z={}".format(gx, gy, gz))
time.sleep(0.1)
20. 霍尔传感器 - BMP280
概述:BMP280是一种高精度压力传感器,可以用于测量大气压力和温度。
使用场景:气象站、无人机高度控制。
代码示例: “`python import smbus import time
bus = smbus.SMBus(1) address = 0x76 # BMP280 I2C 地址
while True:
bus.write_byte_data(address, 0xF4, 0x27) # 配
