2019年7月20日土曜日

Raspberry Pi 3A+ にI2Cポートを追加

I2Cポート不足又は、SDA1, SCL1 を他の用途に使ってる時の対応です。

/boot/config.txtに以下を追加

GPIO 0をSDA、GPIO 1をSCL、クロックディレイを2μsec、busを3とする場合の設定、

dtoverlay=i2c-gpio,i2c_gpio_sda=0,i2c_gpio_scl=1,i2c_gpio_delay_us=2,bus=3

reboot後、デバイス確認
# i2cdetect -y 3
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- 4c -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

認識してますね。

温度センサーLM73での動作確認

確認 python script (かなり手抜き)

[LM73lib.py]
LM73_ADDRESS             = 0x4c
I2C_BUSNUM               = 3   #<== normal 1?!
LM73_TEMP_REGISTER       = 0

class LM73(object):
        def __init__(self, address=LM73_ADDRESS, busnum=I2C_BUSNUM):
                self._address = address
                self._bus = smbus.SMBus(busnum)

        def regdata2float (self, regdata):
                return (regdata / 16.0)

        def getTemp(self):
#               set 13bit resolution
#               p2,p1,p0-->pointer B'100'
#               TO_DIS,RES1,RES0,'0' ==>B'0100'
                self._bus.write_i2c_block_data(self._address, 0x04, [0x40])
                """Reads the temp from the LM73 sensor"""
                raw = self._bus.read_word_data(self._address, LM73_TEMP_REGISTER)
                print "raw: ",
                print raw
#               low byte, high byte ==> high byte, low byte
                raw = ((raw << 8) & 0xFF00) + (raw >> 8)
                raw = raw >> 3
                return self.regdata2float(raw)
             
[LM73_test.py]             
#!/usr/bin/env python
# -*- coding:utf-8 -*-

import LM73lib
sensor = LM73lib.LM73(busnum=3)
print sensor.getTemp()

#  ./LM73_test.py
raw:  10254
28.3125
とりあえず、それなりの結果かな..

0 件のコメント: