積算電流計のためのスケッチ(プログラム)です。 正確な値を得るために、出来る限りのスピードで繰り返し電流値を読み取り、 1秒間積算し、その都度平均値を求めています。

// Stand-alone Current Integrator by Toda Yorzu Kenkyujyo
#include < LiquidCrystal.h >  // include LCD driver
#define ResetPin 9  // To define Digital 9 pin as input
#define analogPin 3 // To define analog 3 pin as input

// lcd(RS,RW,E,DB4,DB5,DB6,DB7)  Arduno board numbers for LCD SD1602VBWB-XA
LiquidCrystal lcd(7,6,5,10,11,12,13);

 unsigned long time1=0;  // time in msec unit
 unsigned long time2=0; // time2 in msec unit
 unsigned long timeForAverage=1000; // nearly=1000 msec, time for average Current 
 unsigned long SecNow=0;  // time in second unit
 unsigned long MeasN=0; // number of measurement times in 1 second
 unsigned long CurrentNow=0;  // Current value
 unsigned long SumCurrent=0;  // Sum of Current
 
 float CurrentAve=0;  // Current average for 1 sec
 double IntCurrent=0;  // Integrated Current
 float Coeff=0.488281;  // 10 bit=1024 / 5 Volts / 10 OHM *1000 ,Series resistance (10 OHM), unit mA

void setup(){
  pinMode(ResetPin, INPUT);  // for reset button
  Serial.begin(9600);  // Serial communication begins at a speed of 9600
  lcd.begin(16,2);  // This LCD is 16*2
  lcd.clear();  // Clear the display
  lcd.setCursor(1,0); // Cursor at the original position
  lcd.print("Current");
  lcd.setCursor(1,1); // Cursor at the 2nd column
  lcd.print("Integrator");
  delay(300);
  CurrentNow=0;
  IntCurrent=0;
  SumCurrent=0;
  MeasN=0;
  time2=millis();
}

void loop(){
  
  MeasN=0;  //reset the measurement times
  SumCurrent=0; //rest the sum of current
  
  time1=millis();  //check time now
  while(millis()<time1+1000){   // repeat for 1000 msec=1 second
  CurrentNow=analogRead(analogPin); //Direct measurement of analog 3 pin
  SumCurrent=SumCurrent+CurrentNow;  // Sum of the current
  MeasN++;  // number of measurement times
 // delay(300);
  }

  SecNow=time1/1000;  // time in the unit of second
  CurrentAve=Coeff*(float)SumCurrent/(float)MeasN;  // average current for 1 sec

  timeForAverage=millis()-time2;
  IntCurrent=IntCurrent+(double)timeForAverage*(double)CurrentAve/1000.0; // Integrated current
  time2=millis();
    
  lcd.clear();  // LCD clear
  lcd.setCursor(0,0);  //In the 1st column, original position
  lcd.print("Sum=");  //Integrated Current=
  lcd.setCursor(4,0);  // Set the Position
  lcd.print(IntCurrent); // Display Integrated Current  unit mA*sec
  lcd.setCursor(0,1);  // In the 2nd column
  lcd.print("I=");  // Current=
  lcd.setCursor(2,1);  // Set the Cursor position to (2,1)
  lcd.print(CurrentAve); // Display averaged current
  lcd.setCursor(9,1);  //Set the Cursor position to (7,1)
  lcd.print(SecNow); // Display averaged current
  lcd.setCursor(15,1);  // In the 2nd column
  lcd.print("s"); // Unit of second
  
  // Output to PC throgh USB/serial
  Serial.print("MeasN/SumCurrent/CurrentAve/IntegratedCurrent");  
  Serial.print(MeasN); Serial.print("/");
  Serial.print(SumCurrent);Serial.print("/");
  Serial.print(CurrentAve);Serial.print("/");
  Serial.println(IntCurrent);
  
  if(digitalRead(ResetPin)==HIGH){IntCurrent=0;} // reset the Integrated Current when the button pushed
    }

戸田よろず研究所トップページへ

ここで公開するアイデア/装置は安全性を保障しておりません。 用途に応じた設計を行い、十分な安全検査を行ってからご利用ください。 本サイトの情報の営利目的での利用はご遠慮ください。 本サイトの内容の無断転載を禁じます。© 2011 TYK