1、安装必要的软件包:
sudo apt-get update
sudo apt-get install python3-smbus python3-dev i2c-tools
sudo apt-get install python3-smbus
2、确认I2C接口已经启用:
运行 sudo raspi-config
命令打开Raspberry Pi配置工具。
在菜单中选择 "5 Interfacing Options",然后 "P5 I2C"。
确保已经启用了I2C接口。
3、确认传感器连接正确:
请确认MPU6050的VCC引脚已正确连接到树莓派的3.3V电源引脚。
请确认MPU6050的GND引脚已正确连接到树莓派的地引脚(GND)。
请确认MPU6050的SCL引脚(时钟)已正确连接到树莓派的GPIO3引脚(BCM标号为2)。
请确认MPU6050的SDA引脚(数据)已正确连接到树莓派的GPIO2引脚(BCM标号为3)。
4、进入存放代码的目录
cd /home/pi/my_code_directory
5、新建py文件
nano mpu6050_qt.py
6、输入代码
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget, QPushButton, QLabel, QMessageBox
from PyQt5.QtCore import QTimer
import RPi.GPIO as GPIO
import smbus
class DataCollectionApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Data Collection App")
# 创建按钮和标签
self.start_button = QPushButton("开始采集")
self.stop_button = QPushButton("停止采集")
self.accel_label = QLabel("等待数据采集...")
self.gyro_label = QLabel("等待数据采集...")
# 设置布局
layout = QVBoxLayout()
layout.addWidget(self.start_button)
layout.addWidget(self.stop_button)
layout.addWidget(self.accel_label)
layout.addWidget(self.gyro_label)
central_widget = QWidget()
central_widget.setLayout(layout)
self.setCentralWidget(central_widget)
# 连接信号和槽函数
self.start_button.clicked.connect(self.start_collection)
self.stop_button.clicked.connect(self.stop_collection)
# 初始化状态
self.is_collecting = False
self.timer = QTimer()
self.timer.timeout.connect(self.update_data)
def start_collection(self):
if not self.is_collecting:
self.is_collecting = True
self.timer.start(2000) # 每2秒更新一次数据
else:
QMessageBox.warning(self, "警告", "采集已经开始!")
def stop_collection(self):
if self.is_collecting:
self.is_collecting = False
self.timer.stop()
else:
QMessageBox.warning(self, "警告", "采集尚未开始!")
def read_data(self):
bus = smbus.SMBus(1)
device_address = 0x68 # MPU6050设备地址
# 启动加速度计和陀螺仪
bus.write_byte_data(device_address, 0x6B, 0x00)
bus.write_byte_data(device_address, 0x1B, 0x08)
# 读取加速度值
accel_x = self.read_word_2c(bus, device_address, 0x3B)
accel_y = self.read_word_2c(bus, device_address, 0x3D)
accel_z = self.read_word_2c(bus, device_address, 0x3F)
# 读取角速度值
gyro_x = self.read_word_2c(bus, device_address, 0x43)
gyro_y = self.read_word_2c(bus, device_address, 0x45)
gyro_z = self.read_word_2c(bus, device_address, 0x47)
return (accel_x, accel_y, accel_z), (gyro_x, gyro_y, gyro_z)
def read_word_2c(self, bus, address, register):
high = bus.read_byte_data(address, register)
low = bus.read_byte_data(address, register + 1)
value = (high << 8) + low
if value >= 0x8000:
return -((65535 - value) + 1)
else:
return value
def update_data(self):
acceleration, gyro = self.read_data()
accel_data = "加速度:{},{},{}".format(acceleration[0], acceleration[1], acceleration[2])
gyro_data = "角速度:{},{},{}".format(gyro[0], gyro[1], gyro[2])
self.accel_label.setText(accel_data)
self.gyro_label.setText(gyro_data)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = DataCollectionApp()
window.show()
sys.exit(app.exec_())
7、执行代码
python3 mpu6050_qt.py