임베디드 보드/아두이노

아두이노 전류량/ 전압 체크

ZEROWIN.TECH 2020. 8. 12. 12:37
728x90

 

https://github.com/adafruit/Adafruit_INA219

 

 

adafruit/Adafruit_INA219

INA219 Current Sensor. Contribute to adafruit/Adafruit_INA219 development by creating an account on GitHub.

github.com

Adafruit INA219 하이 사이드 DC 전류 센서 보드 용 라이브러리입니다.

 

https://www.adafruit.com/product/904

 

INA219 High Side DC Current Sensor Breakout - 26V ±3.2A Max

This breakout board will solve all your power-monitoring problems. Instead of struggling with two multimeters, you can just use the handy INA219B chip on this breakout to both measure both ...

www.adafruit.com

튜토리얼 및 배선 다이어그램은 위의 링크를 확인하십시오. 이 칩은 I2C를 사용하여 통신합니다.

설치하려면 Arduino Library Manager를 사용하고 'Adafruit INA219'를 검색하여 라이브러리를 설치하십시오.

 

  • INA219B 칩을 사용하여 I2C에서 높은 측 전압과 DC 전류 소비를 모두 1 % 정밀도로 측정 할 수 있습니다.

전류 패널 미터와 같은 대부분의 전류 측정 장치는 로우 사이드 측정에만 적합합니다. 즉, 배터리를 사용하지 않으려면 대상 접지와 실제 접지 사이에 측정 저항을 붙여야합니다. 이는 접지 기준이 변경되고 다양한 전류 소비로 이동할 때 전자 장치가 좋아하지 않는 경향이 있기 때문에 회로에 문제를 일으킬 수 있습니다. 이 칩은 훨씬 더 스마트합니다. 3V 또는 5V로 전원이 공급 되더라도 최대 + 26VDC까지 하이 사이드 전류 측정을 처리 할 수 ​​있습니다. 또한 배터리 수명이나 태양 전지판을 추적하는 데 좋은 높은 측 전압을보고합니다.

정밀 증폭기는 0.1ohm, 1 % 감지 저항의 전압을 측정합니다. 증폭기의 최대 입력 차이는 ± 320mV이므로 최대 ± 3.2A를 측정 할 수 있습니다. 내부 12 비트 ADC를 사용할 경우 ± 3.2A 범위의 분해능은 0.8mA입니다. 내부 이득이 최소 div8로 설정된 경우 최대 전류는 ± 400mA이고 분해능은 0.1mA입니다. 고급 해커는 0.1ohm 전류 감지 저항을 제거하고 자체 저항으로 교체하여 범위를 변경할 수 있습니다 (예 : 8mA 분해능으로 32A를 측정하려면 0.01ohm)

6 핀 헤더 (이 센서를 브레드 보드에 쉽게 부착 할 수 있음)와 3.5mm 터미널 플러그가 포함되어있어 부하를 쉽게 부착 및 분리 할 수 ​​있습니다. 사용법은 간단합니다. 3 ~ 5VDC로 센서 자체에 전원을 공급하고 2 개의 I2C 핀을 마이크로 컨트롤러에 연결합니다. 그런 다음 대상 전원 공급 장치를 VIN +에 연결하고 부하를 접지에 VIN-에 연결합니다. 모든 게인, 범위 및 수학을 수행하는 자세한 자습서가 있습니다. Arduino 또는 CircuitPython을 사용하기 만하면됩니다!

 

https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/downloads

 

Adafruit INA219 Current Sensor Breakout

The INA219B breakout board will solve all your power-monitoring problems. Instead of struggling with two multimeters, you can use this breakout to measure both the high side voltage and DC current draw over I2C with 1% precision.

learn.adafruit.com

  •  
  • 0.1 ohm 1% 2W current sense resistor
  • Up to +26V target voltage
  • Up to ±3.2A current measurement, with ±0.8mA resolution
  • 0.9" x 0.8" PCB
  • PCB (no header or terminal block): 1.9g
  • Note: The terminal block included with your product may be blue or black.
  • This board/chip uses I2C 7-bit addresses 0x40, 0x41, 0x44, 0x45, selectable with jumpers
 

Adafruit INA219 Current Sensor Breakout

The INA219B breakout board will solve all your power-monitoring problems. Instead of struggling with two multimeters, you can use this breakout to measure both the high side voltage and DC current draw over I2C with 1% precision.

learn.adafruit.com

https://learn.adafruit.com/adafruit-ina219-current-sensor-breakout/downloads

 

프로그래밍

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;


void setup(void) 
{
  Serial.begin(115200);
  while (!Serial) {
      // will pause Zero, Leonardo, etc until serial console opens
      delay(1);
  }

  uint32_t currentFrequency;
    
  Serial.println("Hello!");
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  if (! ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1) { delay(10); }
  }
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  //ina219.setCalibration_16V_400mA();

  Serial.println("Measuring voltage and current with INA219 ...");
}

void loop(void) 
{
  float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  Serial.println("");

  delay(2000);
}

아두이노 시리얼모니터를 이용하여 보드에서 사용하는 전류량을 표시합니다.

 

전류량을 LCD 디스플레이합니다. 

시리얼플로터를 이용하여 전류량을 그래프로 표시합니다.

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD I2C 주소를설정한다. 16칸2줄LCD 사용
// 0x27 대신 스캐닝 된 주소를 넣는다.

void setup(void) 
{
  Serial.begin(115200);
  while (!Serial) {
      // will pause Zero, Leonardo, etc until serial console opens
      delay(1);
  }

  uint32_t currentFrequency;
    
  Serial.println("Hello!");
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  if (! ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1) { delay(10); }
  }
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  //ina219.setCalibration_16V_400mA();

  Serial.println("Measuring voltage and current with INA219 ...");

  lcd.init();
  lcd.backlight();                // 백라이트를켠다. (lcd.noBacklight() 는 백라이트 끈다.)
  lcd.write(12);
}

#define MAX_NUM_CURRENT 100
float average_current[MAX_NUM_CURRENT] = { 0, };

int loop_count = 0;
void loop(void) 
{

  loop_count += 1;

  lcd.clear(); // 화면을 지운다.
  
  //float shuntvoltage = 0;
  //float busvoltage = 0;
  float current_mA = 0;
  //float loadvoltage = 0;
  //float power_mW = 0;

  //shuntvoltage = ina219.getShuntVoltage_mV();
  //busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  //power_mW = ina219.getPower_mW();
  //loadvoltage = busvoltage + (shuntvoltage / 1000);
  
  // Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  // Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  // Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); // Serial.println(" mA");
  // Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  // Serial.println("");
  
  average_current[loop_count % MAX_NUM_CURRENT] = current_mA;

  double total = 0;
  for(int i = 0; i < MAX_NUM_CURRENT; i++)
  {
     total += average_current[i];
  }

  
  double average = total / MAX_NUM_CURRENT;
  if(loop_count < MAX_NUM_CURRENT)average = total / loop_count;

  Serial.print(","); Serial.print(average);
  Serial.println();
  

    char temp[32] = { 0x00, };
  sprintf(temp, "Current: %d.%d V", 
    (int)average, (int)(average*10) % 10);


  lcd.setCursor(0, 0);
  lcd.print(temp);        // 읽은 문자를 LCD에 써라.

  delay(100);
}

시리얼 플로터를 이용하여 평균 전류량을 표시합니다.

전류 / 전압 표시

#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD I2C 주소를설정한다. 16칸2줄LCD 사용
// 0x27 대신 스캐닝 된 주소를 넣는다.

void setup(void) 
{
  Serial.begin(115200);
  while (!Serial) {
      // will pause Zero, Leonardo, etc until serial console opens
      delay(1);
  }

  uint32_t currentFrequency;
    
  Serial.println("Hello!");
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  if (! ina219.begin()) {
    Serial.println("Failed to find INA219 chip");
    while (1) { delay(10); }
  }
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  //ina219.setCalibration_16V_400mA();

  Serial.println("Measuring voltage and current with INA219 ...");

  lcd.init();
  lcd.backlight();                // 백라이트를켠다. (lcd.noBacklight() 는 백라이트 끈다.)
  lcd.write(12);
}

#define MAX_NUM_CURRENT 100
float average_current[MAX_NUM_CURRENT] = { 0, };

int loop_count = 0;
void loop(void) 
{

  loop_count += 1;

  lcd.clear(); // 화면을 지운다.
  
  float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  
  // Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  // Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA);  // Serial.println(" mA");
  // Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  // Serial.println("");
  
  average_current[loop_count % MAX_NUM_CURRENT] = current_mA;

  double total = 0;
  for(int i = 0; i < MAX_NUM_CURRENT; i++)
  {
     total += average_current[i];
  }

  
  double average = total / MAX_NUM_CURRENT;
  if(loop_count < MAX_NUM_CURRENT)average = total / loop_count;

  Serial.print(","); Serial.print(average);
  Serial.println(" mA");
  

  char temp[32] = { 0x00, };
  sprintf(temp, "Current: %d.%dmA", 
    (int)average, (int)(average*10) % 10);
  lcd.setCursor(0, 0);
  lcd.print(temp);        // 읽은 문자를 LCD에 써라.

    //char temp[32] = { 0x00, };
    memset(temp, 0x00, 32);
  sprintf(temp, "Voltage: %d.%d V", 
    (int)loadvoltage, (int)(loadvoltage*10) % 10);
  lcd.setCursor(0, 1);
  lcd.print(temp);        // 읽은 문자를 LCD에 써라.


  delay(100);
}

테스트영상