Raspberry Pi I2C (Python)

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

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值