Raspberry Pi I2C (Python)
使用i2cdetect
命令查看对应的IMU模块地址
sudo i2cdetect -y -r 1
查看得 imu 地址
:0x68、0x1e
安装smbus
模块
apt-get install python-smbus
使用python代码利用I2C
协议通信:
import smbus
import time
# 对应的Bus位置也要修改
bus = smbus.SMBus(1)
#修改对应的地址位置
address = 0x68
def bearing255():
bear = bus.read_byte_data(address, 1)
return bear
def bearing3599():
bear1 = bus.read_byte_data(address, 2)
bear2 = bus.read_byte_data(address, 3)
bear = (bear1 << 8) + bear2
bear = bear/10.0
return bear
while True:
bearing = bearing3599() #this returns the value to 1 decimal place in degrees.
bear255 = bearing255() #this returns the value as a byte between 0 and 255.
print bearing
print bear255
time.sleep(1)
保存Python文件并运行即可
参考地址:
- Raspberry Pi I2C (Python) step 5