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

In this instructable, I will explain how to use I2C on the Pi, with the examples of the CMPS03 compass module and SRF08 Ultrasonic range, using python. I will explain right through installing the OS, to ensure that the dependencies and everything is installed.

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

Go to the Raspberry Pi website, and download the latest Raspbian image and follow the instructions burn it to the SD card.

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

On the Pi, I2C is disabled by default. The first thing to do, is run the commandsudo nano /etc/modprobe.d/raspi-blacklist.conf . In this file, there is a comment, and two lines. Add a hash before the I2C line, to comment it out.

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

The next thing to do is add the I2C module to the kernel. Run the command sudo nano /etc/modules .You should see the following 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

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

There are a few packages that will need installing to use I2C. The first command to run is sudo apt-get install i2c-tools. If this fails, try running sudo apt-get updateand try again, else run crying to your nearest nerd. The other package needed can be installed by running sudo apt-get install python-smbus.

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

We now have everything ready to start using I2C!

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

The second example is the SRF08 range sensor, with built in light 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

I hope you have found this instructable useful, as it should provide you with the code you need to get I2C working nicely. I spent a long time trying to fathom the Adafruit I2C Library out, before realising that these simple commands are all that I need. The basic read and write commands are functions in my provided code, so that should see you through. 

Raspberry Pi I2C驱动 (Python)的更多相关文章

  1. 常用Raspberry Pi周边传感器的使用教程

    在Raspberry Pi 的使用和开发过程中,你可能时常需要一些硬件和传感器等来支持你的开发工作,例如,加入一个超声波测距的模块来让你的Raspberry Pi具备测距功能,加入一个测温模块以实现测 ...

  2. 常用Raspberry Pi周边传感器的使用教程(转)

    转:http://bbs.xiaomi.cn/thread-7797152-1-1.html 在Raspberry Pi 的使用和开发过程中,你可能时常需要一些硬件和传感器等来支持你的开发工作,例如, ...

  3. Raspberry pi 使用python+pySerial实现串口通信(转)

    Raspberry pi 使用python+pySerial实现串口通信 转:http://blog.csdn.net/homeway999/article/details/8642353   目录( ...

  4. (RaspBerry Pi) Python GPIO 基本操作

    目前打算由潛入深慢慢學習RaspBerry Pi, 所以先由最容易下手的Python進入樹莓派的世界 首先要使用 GPIO 需要利用RPI.GPIO package想當然爾必須先安裝 所以先執行下列命 ...

  5. 树莓派 -- 输入设备驱动 (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 ...

  6. Raspberry PI 2上的802.11ac网卡驱动编译

    Raspberry PI 2上的802.11ac网卡驱动编译 最近在树莓派2上折腾视频,用来做FPV,但是发现2.4G的控会严重干扰2.4G WIFI,在开控的时候我的台式机+外置USB网卡都频频掉线 ...

  7. RASPBERRY PI 外设学习资源

    参考: http://www.siongboon.com/projects/2013-07-08_raspberry_pi/index.html Raspberry Pi         Get st ...

  8. 转:Raspberry Pi(树莓派)试用小记

    近期入手一树莓派卡片机,体验了一下它的强大,写篇报告,推广一下哈! 机器截图: 基础参数: CPU:700 MHz, ARM11 内存:512M(还有一种是256M的) 支持GPU加速(高清视频无压力 ...

  9. 用树莓派Raspberry Pi和Micro:bit做一个自拍器

    在这个项目中,我们将使用Python来构建一个由Micro:bit触发树莓派Raspberry Pi和相机模块的自拍器.这是开始使用硬件和简单文本编程的好方法. 我们将学习: 如何设置Raspberr ...

随机推荐

  1. c++初步实现的一个LRU

    #include<iostream>#include<map> using namespace std; typedef struct Node{ int data; stru ...

  2. 防止SQL注入的方法

    方法1⃣️addslashes(); $username=addslashes($_POST['username']); 方法2⃣️mysql_escape_string(); 方法3⃣️开启魔术引号 ...

  3. jxl_1

    import java.io.File; import jxl.CellView; import jxl.Workbook; import jxl.format.Alignment; import j ...

  4. codevs1409 拦截导弹2

    [问题描述]一场战争正在 A 国与 B 国之间如火如荼的展开.B 国凭借其强大的经济实力开发出了无数的远程攻击导弹,B 国的领导人希望,通过这些导弹直接毁灭 A 国的指挥部,从而取得战斗的胜利!当然, ...

  5. js 求点到直线的距离(由2点确定的直线,求到第三点的距离)

    需要用到2个数学公式 1,已知2点求其直线方程 2,点到直线的距离 1,Y=kX+b 分别将两点带入以上方程,求出k 和b 例如: p0={x:?,y:?}, p1={x:?,y:?} 可解得方程: ...

  6. Ubuntu 修改 ssh 登录后的欢迎信息

    /etc/update-motd.d/90-updates-available cd /etc/update-motd.d/ vi 10-help-text 修改或者注释掉里面的内容 vi 90-up ...

  7. Asp.NET Visible属性与HTML display:none

    作为Asp.NET 程序猿,我们经常会遇到客户端HTML 元素与 WebPage Server端控件互操作的情景.如果没有很好的掌握两种技术的细节就很容易发生一些“莫名其妙的错误”. 我最近犯的一个错 ...

  8. django框架代码基础

    urls.py 导入相对应的模块from django.conf.urls import url,includefrom django.contrib import adminfrom son1.vi ...

  9. nginx rewrite 实现二级域名跳转

    当访问http://cbs.test.com跳转到http://www.test.com/test/cbs/方法一: (这种方法浏览器地址会变www.test.com/test/cbs)server ...

  10. 转:Eclipse插件开发之TreeViewer

    http://www.tuicool.com/articles/e6fmE3R contentprovider在插件开发和RCP(Rich Client Platform)开发中常常被用到,譬如你要创 ...