--> Table17 --> value = "0"DIO0 is used for Voltage zero crossing
--> Table18 --> value = "0"DIO1 for Over Current.
Please quote correctly. It's 1/2^15with a step size of 1/215
Let me guess:If I try to read only the voltage and current - the data are not constant.
/* ACS71020 Arduino Programming Code
* This code will work for Arduino UNO
* Supply Voltage is 3.3V, ACS71020KMABTR-030B3-I2C
* SCL Pin = A4 Analog Pin
* SDA Pin = A5 Analog Pin
* By using this code it is possible to read AC Voltage, AC Current, Active Power, Reactive Power, Power Factor and Frequency.
*
*/
#include <Wire.h>
#define kNOERROR 0
#define kREADERROR 1
#define kWRITEERROR 2
#define ADDRESS 0x60 // Slave Address = 96 (DIO_0 = DIO_1 = 0 (GND)), When DIO_0 = DIO_1 = 1 (connected to VCC through 10K pull up )Slave Address = 127
const uint16_t LEDPin = 13;
const uint32_t WRITE = 0x00;
const uint32_t READ = 0x80;
const uint32_t COMMAND_MASK = 0x80;
const uint32_t ADDRESS_MASK = 0x7F;
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
double AmpsRMS1 = 0;
unsigned long nextTime;
bool ledOn = false;
bool UseI2C = true;
// Setup the demo board.
void setup()
{
pinMode(LEDPin, OUTPUT);
digitalWrite(LEDPin, LOW);
// Turn on the pullup so the determination of communication protocol can be made.
// pinMode(ProtocolSelectPin, INPUT_PULLUP);
if (UseI2C)
{
// Initialize I2C
Wire.begin();
Wire.setClock(100000);
}
Write(0x2F, 0x4F70656E); // Unlock device
// Write(0x1E, 0x00); // Bypass_enable
Write(0x25, 0x00); // Numptsout
Write(0x1C, 0x32); // rms_avg, AC Voltage averaging done by writing the 50 decimal value in the rms_avg_1 register (0x1C)
Write (0x1B, 0x0019C000);
// Initialize serial
Serial.begin(115200);
// If the Arduino has built in USB, keep the next line
// in to wait for the Serial to initialize
while (!Serial);
nextTime = millis();
}
/*
* Every 500 milliseconds, read the ACS71020 and print out the values
*/
void loop()
{
uint32_t vrms_irms;
uint32_t vrms;
uint32_t irms;
uint32_t pactive;
uint32_t paparent;
uint32_t pimag;
uint32_t pfactor;
uint32_t numptsout;
uint32_t vrmsavgonesec_irmsavgonesec;
uint32_t vrmsavgonesec;
uint32_t irmsavgonesec;
uint32_t vrmsavgonemin_irmsavgonemin;
uint32_t vrmsavgonemin;
uint32_t irmsavgonemin;
uint32_t pactavgonesec;
uint32_t pactavgonemin;
uint32_t vcodes;
uint32_t icodes;
uint32_t pinstant;
uint32_t flags;
float AC_Voltage;
float factor_v;
float AC_Current;
float factor_i;
float Power_Factor;
float Power_Factor1;
float div1 = 32767.00;
float div2 = 32767.00;
float pf,pf1,pact1,pact2,pact3,pact4,Active_Power,Active_Power_avg,Aparent_Power,papa1,pima1,Reactive_Power;
int Frequency;
// Every 1/2 second, toggle the state of the LED and read the ACS71020
if (nextTime < millis())
{
Read(0x20, vrms_irms);
Read(0x21, pactive);
Read(0x22, paparent);
Read(0x23, pimag);
Read(0x24, pfactor);
Read(0x25, numptsout);
Read(0x26, vrmsavgonesec_irmsavgonesec);
Read(0x27, vrmsavgonemin_irmsavgonemin);
Read(0x28, pactavgonesec);
Read(0x29, pactavgonemin);
Read(0x2A, vcodes);
Read(0x2B, icodes);
Read(0x2C, pinstant);
Read(0x2D, flags);
vrms = vrms_irms;
irms = (vrms_irms >> 16);
pimag = pimag & 0x1FFFF;
vrmsavgonesec = vrmsavgonesec_irmsavgonesec;
irmsavgonesec = (vrmsavgonesec_irmsavgonesec >> 16);
vrmsavgonemin = vrmsavgonemin_irmsavgonemin & 0x7FFF;
irmsavgonemin = (vrmsavgonemin_irmsavgonemin >> 16) & 0x7FFF;
pactavgonesec = pactavgonesec & 0x1FFFF;
pactavgonemin = pactavgonemin & 0x1FFFF;
factor_v = (vrmsavgonesec / div1);
AC_Voltage = (factor_v * 240.00 * 1.795);
Serial.print ("AC_Voltage (Vrms)= ");
Serial.println ( AC_Voltage );
float div = 32767.00;
factor_i = (irms/div2);
AC_Current = (factor_i*90.00*2); // 2 is the conversion factor
Serial.print ("AC_Current (Arms) = ");
Serial.println ( AC_Current );
// delay(1000);
if (pfactor<512)
{
Power_Factor = (pfactor/512.000);
Serial.print ("Power_Factor = ");
Serial.println ( Power_Factor );
}
else
{
pf1 = (2048- pfactor);
Power_Factor = (pf1/512.000);
if (Power_Factor >1)
{
Power_Factor = 1;
}
Serial.print ("Power_Factor = ");
Serial.println ( Power_Factor );
}
if (pactive<32767)
{
pact1 = (pactive/32767.00);
Active_Power = (pact1*6900*1.795); // 1.795 is the converson factor
}
else
{
pact1 = (131071- pactive);
pact2 = (pact1/32767.00);
Active_Power = (pact2*6900*1.795);
}
if (pactavgonesec<32767)
{
pact3 = (pactavgonesec/32767.00);
Active_Power_avg = (pact3*6900*1.795);
Serial.print ("Active_Power_avg (Watt) = ");
Serial.println ( Active_Power_avg );
}
else
{
pact3 = (131071- pactavgonesec);
pact4 = (pact3/32767.00);
Active_Power_avg = (pact4*6900*1.795);
Serial.print ("Active_Power_avg (Watt)= ");
Serial.println ( Active_Power_avg );
}
if (paparent<32767)
{
papa1 = (paparent/32767.00);
Aparent_Power = (papa1*6900*1.795);
Serial.print ("Aparent_Power (VA)= ");
Serial.println ( Aparent_Power );
}
else
{
pact1 = (131071- pactive);
pact2 = (pact1/32767.00);
Active_Power = (pact2*6900*1.795);
Serial.print ("Active_Power = ");
Serial.println ( Active_Power );
}
if (pimag<65535)
{
pima1 = (pimag/65535.00);
Reactive_Power = (pima1*6900*1.795);
Serial.print ("Reactive_Power (VAR)= ");
Serial.println (Reactive_Power );
}
else
{
pact1 = (131071- pactive);
pact2 = (pact1/32767.00);
Active_Power = (pact2*6900*1.795);
Serial.print ("Active_Power = ");
Serial.println ( Active_Power );
}
Frequency = (32000.000/(numptsout*2));
Serial.print ("Frequency (Hz) = ");
Serial.println (Frequency);
nextTime = millis() + 500L;
// Blink the LED every half second
if (ledOn)
{
digitalWrite(LEDPin, LOW);
ledOn = false;
}
else
{
digitalWrite(LEDPin, HIGH);
ledOn = true;
}
}
}
/*
* Read a register
*
* address - the address to be written
* value - the value that was read
* returns - the error (0 otherwise)
*/
uint32_t Read(uint8_t address, uint32_t& value)
{
uint16_t results = kNOERROR;
if (UseI2C)
{
Wire.beginTransmission(ADDRESS);
Wire.write(address);
results = Wire.endTransmission();
if (results == kNOERROR)
{
byte a,b,c,d;
Wire.requestFrom(ADDRESS, 4);
a = Wire.read();
b = Wire.read();
c = Wire.read();
d = Wire.read();
value = d;
value = (value<< 8) | c;
value = (value<< 8) | b;
value = (value<< 8) | a;
}
}
else
{
}
return results;
}
/*
* Write a register
*
* address - the address to be written
* value - the value to be written
* returns - the error (0 otherwise)
*/
uint16_t Write(uint8_t address, uint32_t value)
{
uint16_t results = kNOERROR;
if (UseI2C)
{
Wire.beginTransmission(ADDRESS);
// Send the address then the value (least significant byte first)
Wire.write(address);
Wire.write(value);
Wire.write(value >> 8);
Wire.write(value >> 16);
Wire.write(value >> 24);
results = Wire.endTransmission();
}
else
{
}
if (address < 0x10)
{
delay(30); // If writing to EEPROM delay 30 ms
}
return results; // one curly bracket need to close
}
float ConvertUnsignedFixedPoint(uint32_t inputValue, uint16_t binaryPoint, uint16_t width)
{
uint32_t mask;
if (width == 32)
{
mask = 0xFFFFFFFF;
}
else
{
mask = (1UL << width) - 1UL;
}
return (float)(inputValue & mask) / (float)(1L << binaryPoint);
}
float ConvertSignedFixedPoint(uint32_t inputValue, uint16_t binaryPoint, uint16_t width)
{
int32_t signedValue = SignExtendBitfield(inputValue, width);
return (float)signedValue / (float)(1L << binaryPoint);
}
int32_t SignExtendBitfield(uint32_t data, uint16_t width)
{
// If the bitfield is the width of the variable, don't bother trying to sign extend (it already is)
if (width == 32)
{
return (int32_t)data;
}
int32_t x = (int32_t)data;
int32_t mask = 1L << (width - 1);
x = x & ((1 << width) - 1); // make sure the upper bits are zero
return (int32_t)((x ^ mask) - mask);
}
Thanks a lot for this ...Please solve the problem as soon as possible.
Hi,
Thanks a lot for this ...
But first you need to give all your informations: (for sure also - as soon as possible ;-))
* complete schematic of all around the metering IC( power, power supply, load connections microcontroler connections periferial parts...)
* wiring diagrams
* scope pictures of input voltage and input current in one screen
best if you could add
* photos of your application
* informations about the connected load (what is it? 1 phase? 3 phase? how connected?
Klaus
Dear Klaus,Hi,
Thanks a lot for this ...
But first you need to give all your informations: (for sure also - as soon as possible ;-))
* complete schematic of all around the metering IC( power, power supply, load connections microcontroler connections periferial parts...)
* wiring diagrams
* scope pictures of input voltage and input current in one screen
best if you could add
* photos of your application
* informations about the connected load (what is it? 1 phase? 3 phase? how connected?
Klaus
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?