前段时间在DFRobot的用户群里面有个用户用到了这个模块:
他想要一个中文的教程, 在LZ的努力下, 终于跑通了.
1. 安装最新的树莓派镜像, 可以参考这个帖子:
2. 然后进行连线的工作 (MCP3424上面的编码器全部拨到ON的一侧)
- MCP3424 GND >> 树莓派 Ground
- MCP3424 +5V >> 树莓派 DC Power 5V
- MCP3424 SCL >> 树莓派 GPIO03(SCL1, I2C)
- MCP3424 SDA >> 树莓派 GPIO02(SDA1, I2C)
- MCP3424 C*+ 和 C*- 接上对应需要测电压的两端.
3. 首先要启用I2C接口功能,- 打开Terminal.
- 键入
复制代码
- 用上下键选择 5 Interfacing Options , 按回车进入.
- 选择 5 I2C , 按回车确认.(需要重启)
4. 安装Python依赖库(树莓派需要联网)
5. 运行下面的Python脚本就可以了:- #!/usr/bin/env python
-
- import glob
- import logging
- import smbus
- from MCP342x import MCP342x
-
- import numpy as np
-
-
- __author__ = 'Steve Marple'
- __version__ = '0.3.3'
- __license__ = 'MIT'
-
-
- def get_smbus():
- candidates = []
- prefix = '/dev/i2c-'
- for bus in glob.glob(prefix + '*'):
- try:
- n = int(bus.replace(prefix, ''))
- candidates.append(n)
- except:
- pass
-
- if len(candidates) == 1:
- return smbus.SMBus(candidates[0])
- elif len(candidates) == 0:
- raise Exception("Could not find an I2C bus")
- else:
- raise Exception("Multiple I2C busses found")
-
-
- logging.basicConfig(level='DEBUG')
-
- logger = logging.getLogger(__name__)
-
- bus = get_smbus()
-
- # Create objects for each signal to be sampled
- addr68_ch0 = MCP342x(bus, 0x68, channel=0, resolution=18)
- addr68_ch1 = MCP342x(bus, 0x68, channel=1, resolution=18)
- addr68_ch2 = MCP342x(bus, 0x68, channel=2, resolution=18)
- addr68_ch3 = MCP342x(bus, 0x68, channel=3, resolution=16)
-
- # Create a list of all the objects. They will be sampled in this
- # order, unless any later objects can be sampled can be moved earlier
- # for simultaneous sampling.
- adcs = [addr68_ch0, addr68_ch1, addr68_ch2, addr68_ch3]
- r = MCP342x.convert_and_read_many(adcs, samples=2)
- print('return values: ')
- print(r)
-
-
- # , scale_factor=2.448579823702253
- addr68_ch0.convert()
- print(addr68_ch3.convert_and_read())
复制代码
玩得开心 :)
|