1

Тема: INA3221

Основная статья - http://rcl-radio.ru/?p=132614

http://forum.rcl-radio.ru/uploads/images/2024/09/249a346749df0888926c4d00c92464de.png


#include <Wire.h>
#include <INA3221.h>

#define SERIAL_SPEED     9600  // serial baud rate
#define PRINT_DEC_POINTS 3       // decimal points to print

// Set I2C address to 0x41 (A0 pin -> VCC)
INA3221 ina3221(INA3221_ADDR40_GND);

void setup() {
    Serial.begin(SERIAL_SPEED);

    while (!Serial) {
        delay(1);
    }

    ina3221.begin();
    ina3221.reset();

    // Set shunt resistors to 10 mOhm for all channels
    ina3221.setShuntRes(100, 100, 100);

    // Set series filter resistors to 10 Ohm for all channels.
    // Series filter resistors introduce error to the current measurement.
    // The error can be estimated and depends on the resitor values and the bus
    // voltage.
    ina3221.setFilterRes(100, 100, 100);
}

void loop() {
    float current[3];
    float current_compensated[3];
    float voltage[3];

    current[0]             = ina3221.getCurrent(INA3221_CH1);
    current_compensated[0] = ina3221.getCurrentCompensated(INA3221_CH1);
    voltage[0]             = ina3221.getVoltage(INA3221_CH1);

    current[1]             = ina3221.getCurrent(INA3221_CH2);
    current_compensated[1] = ina3221.getCurrentCompensated(INA3221_CH2);
    voltage[1]             = ina3221.getVoltage(INA3221_CH2);

    current[2]             = ina3221.getCurrent(INA3221_CH3);
    current_compensated[2] = ina3221.getCurrentCompensated(INA3221_CH3);
    voltage[2]             = ina3221.getVoltage(INA3221_CH3);

    Serial.print("Channel 1: \n Current: ");
    Serial.print(current[0], PRINT_DEC_POINTS);
    Serial.print("A\n Compensated current: ");
    Serial.print(current_compensated[0], PRINT_DEC_POINTS);
    Serial.print("\n Voltage: ");
    Serial.print(voltage[0], PRINT_DEC_POINTS);
    Serial.println("V");

    Serial.print("Channel 2: \n Current: ");
    Serial.print(current[1], PRINT_DEC_POINTS);
    Serial.print("A\n Compensated current: ");
    Serial.print(current_compensated[1], PRINT_DEC_POINTS);
    Serial.print("\n Voltage: ");
    Serial.print(voltage[1], PRINT_DEC_POINTS);
    Serial.println("V");

    Serial.print("Channel 3: \n Current: ");
    Serial.print(current[2], PRINT_DEC_POINTS);
    Serial.print("A\n Compensated current: ");
    Serial.print(current_compensated[2], PRINT_DEC_POINTS);
    Serial.print("\n Voltage: ");
    Serial.print(voltage[2], PRINT_DEC_POINTS);
    Serial.println("V\n");

    delay(1000);
}

2

Re: INA3221

http://forum.rcl-radio.ru/uploads/images/2024/09/39f4332c8b6ca2d8993481486a34b8be.png

#define ADDR 0x40 // I2C address
#define UR   0.1    // R (Om)
#define AVG  0B100
#define VBUS 0B111
#define CH   0B111
#define VSH  0B111
#define MODE 0B111

#include <Wire.h>

void setup() {
    Wire.begin();
    Serial.begin(9600);
    delay(100);
    I2C_write(0x00, 0x00|(CH<<12)|(AVG<<9)|(VBUS<<6)|(VSH<<3)|MODE); // Configuration Register
    Serial.print("Manufacturer ID ");Serial.println(I2C_read(0xFE),HEX);
    Serial.print("Die ID ");Serial.println(I2C_read(0xFF),HEX);
}

void loop() {
    int ur1=(I2C_read(0x01)>>3);
    int u1=(I2C_read(0x02)>>3);
    int ur2=(I2C_read(0x03)>>3);
    int u2=(I2C_read(0x04)>>3);
    int ur3=(I2C_read(0x05)>>3);
    int u3=(I2C_read(0x06)>>3);
    
    Serial.print("Ur_1 ");Serial.print(ur1*0.04,3);Serial.println(" mV");
    Serial.print("I_1 ");Serial.print(ur1*0.04/UR,1);Serial.println(" mA");
    Serial.print("U_1 ");Serial.print(u1*0.008);Serial.println(" V");
    Serial.println();
    Serial.print("Ur_2 ");Serial.print(ur2*0.04,3);Serial.println(" mV");
    Serial.print("I_2 ");Serial.print(ur2*0.04/UR,1);Serial.println(" mA");
    Serial.print("U_2 ");Serial.print(u2*0.008);Serial.println(" V");
    Serial.println();
    Serial.print("Ur_3 ");Serial.print(ur3*0.04,3);Serial.println(" mV");
    Serial.print("I_3 ");Serial.print(ur3*0.04/UR,1);Serial.println(" mA");
    Serial.print("U_3 ");Serial.print(u3*0.008);Serial.println(" V");
    Serial.println();
    delay(2000);
}


unsigned int I2C_read(byte reg){
  Wire.beginTransmission(ADDR);
  Wire.write(reg);
  Wire.endTransmission();
  Wire.requestFrom(ADDR,2);
  while(Wire.available()<1);
  unsigned int value = (Wire.read()<<8) | Wire.read();
  return value;
  } 

void I2C_write(byte reg, unsigned int data){  
  Wire.beginTransmission(ADDR);
  Wire.write(reg);
  Wire.write((data&0xFF00)>>8);
  Wire.write(data&0x00FF);
  Wire.endTransmission();
  }  

3

Re: INA3221

#define ADDR 0x40 // I2C address
#define UR   0.1    // R (Om)
#define AVG  0B001
#define VBUS 0B111
#define CH   0B111
#define VSH  0B111
#define MODE 0B111

#define I_ogr 0.6 // A

#include <Wire.h>
#include <U8glib.h>            // https://github.com/olikraus/u8glib/

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_FAST);

int ogr_i;

void setup() {
    Wire.begin();
    Serial.begin(9600);
    delay(100);
    I2C_write(0x00, 0x00|(CH<<12)|(AVG<<9)|(VBUS<<6)|(VSH<<3)|MODE); // Configuration Register
    I2C_write(0x0F, 0b111<<12);
    ogr_i = (int)(I_ogr/0.0004)<<1;
    I2C_write(0x0E, ogr_i); // CRI | REG 0X0F BIT 6 | LED3
    Serial.print("Manufacturer ID ");Serial.println(I2C_read(0xFE),HEX);
    Serial.print("Die ID ");Serial.println(I2C_read(0xFF),HEX);
}

void loop() {
    int ur1=(I2C_read(0x01)>>3);
    int u1=(I2C_read(0x02)>>3);
    int ur2=(I2C_read(0x03)>>3);
    int u2=(I2C_read(0x04)>>3);
    int ur3=(I2C_read(0x05)>>3);
    int u3=(I2C_read(0x06)>>3);
    int ur_s=(I2C_read(0x0D)>>1);
    bool err = (I2C_read(0x0f)>>6)&1;
  
    Serial.print("Ur_1 ");Serial.print(ur1,BIN);Serial.println(" mV");
    Serial.print("I_1 ");Serial.print(ur1*0.04/UR,1);Serial.println(" mA");
    Serial.print("U_1 ");Serial.print(u1*0.008);Serial.println(" V");
    Serial.println();
    Serial.print("Ur_2 ");Serial.print(ur2*0.04,3);Serial.println(" mV");
    Serial.print("I_2 ");Serial.print(ur2*0.04/UR,1);Serial.println(" mA");
    Serial.print("U_2 ");Serial.print(u2*0.008);Serial.println(" V");
    Serial.println();
    Serial.print("Ur_3 ");Serial.print(ur3*0.04,3);Serial.println(" mV");
    Serial.print("I_3 ");Serial.print(ur3*0.04/UR,1);Serial.println(" mA");
    Serial.print("U_3 ");Serial.print(u3*0.008);Serial.println(" V");
    
    u8g.firstPage();  
    do {
    u8g.setFont(u8g_font_profont15r);
    u8g.drawLine(0, 52, 128, 52);
    u8g.setPrintPos(0, 12);u8g.print(u1*0.008);u8g.drawStr(45,12,"V");
    u8g.setPrintPos(70, 12);u8g.print(ur1*0.00004/UR,3);u8g.drawStr(115,12,"A");

    u8g.setPrintPos(0, 30);u8g.print(u2*0.008);u8g.drawStr(45,30,"V");
    u8g.setPrintPos(70, 30);u8g.print(ur2*0.00004/UR,3);u8g.drawStr(115,30,"A");

    u8g.setPrintPos(0, 48);u8g.print(u3*0.008);u8g.drawStr(45,48,"V");
    u8g.setPrintPos(70, 48);u8g.print(ur3*0.00004/UR,3);u8g.drawStr(115,48,"A");

    u8g.setPrintPos(70, 64);u8g.print(ur_s*0.00004/UR,3);u8g.drawStr(115,64,"A");
    if(err==1){u8g.drawStr(0,64,"ERROR");}
    } while( u8g.nextPage() );
    delay(1000);
}


unsigned int I2C_read(byte reg){
  Wire.beginTransmission(ADDR);
  Wire.write(reg);
  Wire.endTransmission();
  Wire.requestFrom(ADDR,2);
  while(Wire.available()<1);
  unsigned int value = (Wire.read()<<8) | Wire.read();
  return value;
  } 

void I2C_write(byte reg, unsigned int data){  
  Wire.beginTransmission(ADDR);
  Wire.write(reg);
  Wire.write((data&0xFF00)>>8);
  Wire.write(data&0x00FF);
  Wire.endTransmission();
  }  

4

Re: INA3221

http://forum.rcl-radio.ru/uploads/images/2024/10/6b4710f1053eb74d04889bf56a419d0f.png

http://forum.rcl-radio.ru/uploads/images/2024/10/0a1ee22d6d669c34c4720cad18ceb944.png

http://forum.rcl-radio.ru/uploads/images/2024/10/9e5d5da9828000aa3440fd801ed6c623.png

http://forum.rcl-radio.ru/uploads/images/2024/10/0b828dcec7ad127f2d93fcf4b66c2cd0.png

http://forum.rcl-radio.ru/uploads/images/2024/10/5df2ccf22de86f7eb325fa5de69ff28f.png

http://forum.rcl-radio.ru/uploads/images/2024/10/10f68420453233ebd8d7f56dd65177a7.png