Raspberry Pi I2C驱动 (Python)
本文参考 http://www.instructables.com/id/Raspberry-Pi-I2C-Python/all/?lang=zh 作者 AntMan232

I2C is a communication bus designed by Philips, for chips to communicate with each other on a PCB. It is commonly used, however, for connecting sensors, such as the two examples later in this instructable and port expanders, because you can have multiple devices on the same two pins.
步骤1: Install R-Pi Image

http://www.raspberrypi.org/downloads
There is an easy setup guide on the wiki, just follow it through.
When you have got it installed, run the config tool, and get everything going nicely. In my case, I am running it headless via SSH, which is enabled as default, at pi@192.168.0.X (check on your router to find the IP).
步骤2: Enable I2C

Original:
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
blacklist i2c-bcm2708
Convert to this:
# blacklist spi and i2c by default (many users don't need them)
blacklist spi-bcm2708
#blacklist i2c-bcm2708
步骤3: Enable kernel I2C Module

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
This should have the line i2c-dev added to the end.
Final file:
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.
snd-bcm2835
i2c-dev
步骤4: Install Necessary Packages

To configure the software, we will add the Pi user to the I2C access group, by running the command sudo adduser pi i2c.
Now run sudo reboot to reboot, and test the new software.
To test the software, run the command i2cdetect -y 0 to see if there is anything connected. On my setup, it returned this output, because there was nothing connected:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
步骤5: Example 1: CMPS03 Compass Module

To use the CMPS03 compass module, connect the power to V+ and 0V, from the Pi. I used the 5V line, which they recommend not doing because it might damage your pi, It worked for me, and has caused now damage, but I am not responsible if your's fries.
Then, connect the SDA and SCL lines to the Pi SDA and SCL, and you are ready to roll. The wiring diagram is shown at http://www.robot-electronics.co.uk/htm/cmps3tech.htm.
When you have connected it, run the command "i2cdetect -y 0". In my case, this returned:
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
This shows that the module is on address 0x60. You then need the following python file:
import smbus
import time
bus = smbus.SMBus(0)
address = 0x60
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)
This program should be saved as anything, but add ".py" on the end. Then, run the command with sudo python whateveryoucalledit.p and you should get values written to your screen in a long list.
步骤6: SRF08 Range Sensor

Wire it in in exactly the same way as before, with power, SDA and SCL connected to the Pi. I found that this sensor would not work off 3.3V, but again, I bear no responsibility for you putting 5V through your Pi pins. You can even leave the compass module in as well, because I2C can handle multiple devices on one line. The wiring diagram can be seen here: http://www.robot-electronics.co.uk/htm/srf08tech.shtml .
Run i2cdetect -y 0
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
Note that I have left the compass module connected.
You will then need the following python file. It is more complex, becuase you have to write a command to the sensor to get it to begin reading.
import smbus
import time
bus = smbus.SMBus(0)
address = 0x70
#SRF08 REQUIRES 5V
def write(value):
bus.write_byte_data(address, 0, value)
return -1
def lightlevel():
light = bus.read_byte_data(address, 1)
return light
def range():
range1 = bus.read_byte_data(address, 2)
range2 = bus.read_byte_data(address, 3)
range3 = (range1 << 8) + range2
return range3
while True:
write(0x51)
time.sleep(0.7)
lightlvl = lightlevel()
rng = range()
print lightlvl
print rng
This will print the light level on the built in light sensor and the current range, in cm.
步骤7: Conclusion
Raspberry Pi I2C驱动 (Python)的更多相关文章
- 常用Raspberry Pi周边传感器的使用教程
在Raspberry Pi 的使用和开发过程中,你可能时常需要一些硬件和传感器等来支持你的开发工作,例如,加入一个超声波测距的模块来让你的Raspberry Pi具备测距功能,加入一个测温模块以实现测 ...
- 常用Raspberry Pi周边传感器的使用教程(转)
转:http://bbs.xiaomi.cn/thread-7797152-1-1.html 在Raspberry Pi 的使用和开发过程中,你可能时常需要一些硬件和传感器等来支持你的开发工作,例如, ...
- Raspberry pi 使用python+pySerial实现串口通信(转)
Raspberry pi 使用python+pySerial实现串口通信 转:http://blog.csdn.net/homeway999/article/details/8642353 目录( ...
- (RaspBerry Pi) Python GPIO 基本操作
目前打算由潛入深慢慢學習RaspBerry Pi, 所以先由最容易下手的Python進入樹莓派的世界 首先要使用 GPIO 需要利用RPI.GPIO package想當然爾必須先安裝 所以先執行下列命 ...
- 树莓派 -- 输入设备驱动 (key) 续2: 转载 Setting up a GPIO-Button “keyboard” on a Raspberry Pi
使用device-tree (DT) overlay应该是更方便的方法: http://blog.gegg.us/2017/01/setting-up-a-gpio-button-keyboard-o ...
- Raspberry PI 2上的802.11ac网卡驱动编译
Raspberry PI 2上的802.11ac网卡驱动编译 最近在树莓派2上折腾视频,用来做FPV,但是发现2.4G的控会严重干扰2.4G WIFI,在开控的时候我的台式机+外置USB网卡都频频掉线 ...
- RASPBERRY PI 外设学习资源
参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi Get st ...
- 转:Raspberry Pi(树莓派)试用小记
近期入手一树莓派卡片机,体验了一下它的强大,写篇报告,推广一下哈! 机器截图: 基础参数: CPU:700 MHz, ARM11 内存:512M(还有一种是256M的) 支持GPU加速(高清视频无压力 ...
- 用树莓派Raspberry Pi和Micro:bit做一个自拍器
在这个项目中,我们将使用Python来构建一个由Micro:bit触发树莓派Raspberry Pi和相机模块的自拍器.这是开始使用硬件和简单文本编程的好方法. 我们将学习: 如何设置Raspberr ...
随机推荐
- codeforces 748E Santa Claus and Tangerines
E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- iOS - QRCode 二维码
1.QRCode 在 iOS7 以前,在 iOS 中实现二维码和条形码扫描,我们所知的有,两大开源组件 ZBar 与 ZXing. 这两大组件我们都有用过,这里总结下各自的缺点: 1.ZBar 在扫描 ...
- js判断当前设备
最近用bootstrap做自适应,发现仍然很难很好的兼容web端和PC端的现实. 仔细观察百度,淘宝,京东等大型网站,发现这些网站都有对应不同客户端的子站. 例如: 站点 PC端url web端url ...
- dubbo & zookeeper & springMVC
一.安装准备 1.环境简介 名称 版本 OS Mac OS X 10.11.5 JDK 1.8.0_51 dubbo 2.5.4 tomcat 8.0.35 Zookeeper 3.4.8 maven ...
- linux下使用多线程编写的聊天室
自从开始学linux网络编程后就想写个聊天室,一开始原本打算用多进程的方式来写,可是发觉进程间的通信有点麻烦,而且开销也大,后来想用多线程能不能实现呢,于是便去看了一下linux里线程的用法,实际上只 ...
- 解决Mac下MX4手机无法连接adb问题之解决方案
一般的android连接mac 很方便不用安装驱动就可以啦,可是不知道为什么特殊情况下有的android手机(小米2,华为等)就是连接不上,下来就说说特殊情况下如何连接. 使用USB连接安卓手机后可以 ...
- contiki-process结构体
struct process { struct process *next; #if PROCESS_CONF_NO_PROCESS_NAMES #define PROCESS_NAME_STRING ...
- 【转】 XDC介绍
转:http://blog.sina.com.cn/s/blog_693301190100sitz.html XDC是TI公司为嵌入式实时系统可重用软件组件(在XDC里被成为packages,以下成为 ...
- H5离线存储
如何使用 首先,我们建立一个html文件,类似这样: <!DOCTYPE html> <html lang="en" manifest="manifes ...
- java中参数传递方式
在 Java 应用程序中永远不会传递对象,而只传递对象引用.因此是按引用传递对象.Java应用程序按引用传递对象这一事实并不意味着 Java 应用程序按引用传递参数.参数可以是对象引用,而 Java ...