0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

INA226 with Raspberry Pi 3B+ と INA226 (2024/bookwarm)

Posted at

INA226 with Raspberry Pi 3B+ と INA226 (2024/bookwarm)

Raspbery Pi OSのインストール

とりあえずRaspberry Pi OSをSDカードに焼いてセットしてあるものとします。

  • Raspberry Pi 3B+
  • Raspbery Pi OS Lite (64bit)
  • 前回使用したJRC7805A

接続構成

ina226.png

raspi-config

sudo raspi-config

3.Interface Options

  • I1 SSH

    • YES
      • この設定をした時点で誰でもアクセスできるようになるので、SSHの設定が終わるまでLANケーブルを抜いておいてもいい
  • I5 I2C

    • Would you like the ARM I2C interfaace be enabled ?
      • YES

5.Localisation Options

  • L2 Timezone
    • Asia
      • Tokyo

6 Advanced Options

  • A1 Expanded Filesystem

Finish

  • 再起動する?と聞いてくるのでYes

I2Cのテスト

sudo apt-get install i2c-tools

I2Cアドレスの確認

i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: 40 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

I2Cデバイスのレジスタ読み込み

i2cget -y 1 0x40 0x00 w
0x2741
i2cget -y 1 0x40 0x01 w
0x1100
i2cget -y 1 0x40 0x02 w
0xc60f

Python3で読み込む

PIPのインストール

sudo apt-get install python3-pip

SMBusのインストール

sudo pip3 install smbus2 --break-system-packages

サンプルコード

touch ina226.py
chmod 777 ina226.py
nano ina226.py
#!/usr/bin/python3

from smbus2 import SMBus

def main():
    # Variables
    Volt = 0
    Ampere = 0
    tmp_V = 0
    tmp_A = 0
    shant_reg = 0.002
    # Read from i2C bus(1) address(0x40)
    with SMBus(1) as bus:
        # Read Data
        data01h = bus.read_i2c_block_data(0x40, 1, 2)
        data02h = bus.read_i2c_block_data(0x40, 2, 2)
        # Volt Calcrate
        tmp_V = data02h[0]*256 + data02h[1]
        ## V = ReadValue * LSB(1.25mv) /1000 (mV -> V)
        Volt = tmp_V * 1.25 / 1000
        # Ampere Calcurate
        tmp_A = data01h[0]*256 + data01h[1]
        ## V=IR -> I=V/R -> mI=mV/mR
        ## mV = ReadValue * LSB(2.5μV) / 1000 (μV -> mV)
        ## mR = shant_reg(0.002Ω = 2mΩ)
        ## mI = (ReadValue * LSB(2.5μV) / 1000 (μV -> mV))/ shant_reg
        Ampere = (tmp_A * 2.5 / 1000)/shant_reg
        # Print out
        print(str(Volt) + "V")
        print(str(Ampere) + "mA")

if __name__ == '__main__':
    main()
$ python3 ina226.py
5.045V
20.0mA
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?