/*
Measuring Current Using ACS712
www.circuits4you.com
*/
const int analogIn = A0;
double mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
double RawValue= 0;
double ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
RawValue = analogRead(analogIn);
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
Serial.print("Amps = "); // shows the voltage measured
Serial.println(Amps,2); // the '2' after voltage allows you to display 2 digits after decimal point
delay(1000);
}
change declaration of mVperAmp to 66.0 (I'm guessing your amps is getting converted to an integer
write the RawValue right after you read it--i'd be curious to see if you are reading anything
Nope. It's an analogue input pin, read with an analogRead - why would you also want to make it a digital input? (which'll be overridden in the analogRead)
Nope. You're assigning it to a "double" (aka float) - the compiler is smart enough to do the conversion.
One thing I like to do with any code like this is first print a "version message" or something similar to the display to verify the display is working as expected. Then, during the debug stage, always print the raw data first then your calculated data - that gives you a quick way to determine if you are reading garbage or creating it in your calculations. Once it all works as expected, comment out the raw data print statement. The trick when debugging is to break it down into chunks and figure which "chunk" is where the problem is (goes with good coding too).
/*
Measuring Current Using ACS712
www.circuits4you.com
*/
const int analogIn = A0;
double mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
double RawValue= 0;
double ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
void setup(){
Serial.begin(9600);
pinMode(analogIn,INPUT);
}
void loop(){
RawValue = analogRead(analogIn);
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
Serial.print("Amps = "); // shows the voltage measured
Serial.println(Amps,2); // the '2' after voltage allows you to display 2 digits after decimal point
delay(1000);
}
/*
Measuring AC Current Using ACS712
www.circuits4you.com
*/
const int sensorIn = A0;
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707; //root 2 is 0.707
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.print(AmpsRMS);
Serial.println(" Amps RMS");
}
float getVPP()
{
float result;
int readValue; //value read from the sensor
int maxValue = 0; // store max value here
int minValue = 1024; // store min value here
uint32_t start_time = millis();
while((millis()-start_time) < 1000) //sample for 1 Sec
{
readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue - minValue) * 5.0)/1024.0;
return result;
}
Are you sure your "read for 1 second" approach is valid? minValue will almost certainly be 0 every time I would think as you are bound to read at the zero crossing (or near it) at least once, unless...
...your read frequency inside getVPP() could "beat" with the mains frequency which would give you skewed results that would be different every time you called getVPP(). Unlikely with a fast loop I suppose, but how long does analogRead() take? What if it takes 20mS and that's the period of the AC wave form.
The code in post#0 seems fine for testing.
OP did not mention the test current through the sensor.
-0.22 or -0.15 without, or with a small current seems normal.
Sensors like this, measured with an Arduino, have the 30Amp span spread out over ~450 A/D values.
The setup should work, but zeroing without current, and measuring to 0.1Amp requires better code.
Try sending a few Amps DC through the sensor.
Leo..