树莓派Configuring I2C

本文介绍了如何在Raspberry Pi上配置I2C总线,并提供了几个使用I2C设备的实际项目案例。通过安装必要的软件包和支持内核模块,用户可以轻松地将各种I2C设备连接到Raspberry Pi并进行检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址::https://learn.adafruit.com/adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c



The I2C bus allows multiple devices to be connected to your Raspberry Pi, each with a unique address, that can often be set by changing jumper settings on the module. It is very useful to be able to see which devices are connected to your Pi as a way of making sure everything is working.

To do this, it is worth running the following commands in the Terminal to install the i2c-tools utility.

  
  1. sudo apt-get install -y python-smbus
  2. sudo apt-get install -y i2c-tools

Installing Kernel Support (with Raspi-Config)

Run sudo raspi-config and follow the prompts to install i2c support for the ARM core and linux kernel

learn_raspberry_pi_advancedopt.png
learn_raspberry_pi_i2c.png
learn_raspberry_pi_wouldyoukindly.png
learn_raspberry_pi_i2ckernel.png

Then reboot!

We also recommend going through the steps below to manually check everything was added by raspi-config!

Installing Kernel Support (Manually)

If you're not using a modern Raspbian or you want to do it by hand, you can! Open LXTerminal or console or ssh and enter the following command:

  
  1. sudo nano /etc/modules

and add these two lines to the end of the file:

  
  1. i2c-bcm2708 
  2. i2c-dev

like so:

learn_raspberry_pi_editing_modules_file.png

Then save the file with Control-X Y <return>

Depending on your distribution, you may also have a file called /etc/modprobe.d/raspi-blacklist.conf

If you do not have this file then there is nothing to do, however, if you do have this file, you need to edit it and comment out the lines below:

  
  1. blacklist spi-bcm2708
  2. blacklist i2c-bcm2708
.. by putting a # in front of them.

Open an editor on the file by typing:
  
  1. sudo nano /etc/modprobe.d/raspi-blacklist.conf
.. then edit the file so that it appears as below, and then save and exit the file using CTRL-x and Y.
learn_raspberry_pi_blacklist.png

If you are running a recent Raspberry Pi (3.18 kernel or higher) you will also need to update the /boot/config.txt file. Edit it with sudo nano /boot/config.txt and add the text

dtparam=i2c1=on
dtparam=i2c_arm=on

at the bottom. note that the "1" in "i2c1" is a one not an L!

learn_raspberry_pi_dtparami2c.png

Once this is all done, reboot!

sudo reboot

Testing I2C

Now when you log in you can type the following command to see all the connected devices

  
  1. sudo i2cdetect -y 1
learn_raspberry_pi_i2c-detect.png

This shows that two I2C addresses are in use – 0x40 and 0x70.

Note that if you are using one of the very first Raspberry Pis (a 256MB Raspberry Pi Model B) then you will need to change the command to:

  
  1. sudo i2cdetect -y 0

The Raspberry Pi designers swapped over I2C ports between board releases. Just remember: 512M Pi's use i2c port 1, 256M ones use i2c port 0!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值