imranahmed
Advanced Member level 3
- Joined
- Dec 4, 2011
- Messages
- 817
- Helped
- 3
- Reputation
- 6
- Reaction score
- 3
- Trophy points
- 1,298
- Location
- Karachi,Pakistan
- Activity points
- 6,492
/*
Measuring Current Using ACS712
*/
const int analogIn = A0;
int mVperAmp = 66; // 66 for 30A Module
int RawValue= 0;
int 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("Raw Value = " ); // shows pre-scaled value
Serial.print(RawValue);
Serial.print("\t mV = "); // shows the voltage measured
Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps = "); // shows the voltage measured
Serial.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
delay(2500);
}
Please give a detailed error description.But it cannot measure properly
If your readings are fluctuating, it means that the voltage at the ADC is fluctuating as well.
Make sure that the supply to the ACS712 is clean and doesn't contain any noise, else it will vary the DC offset (i.e 2.5V) as well, causing an error in the reading.
If the supply is OK, then you can try adding a RC Filter at the output of CT and then pass the signal to the ADC.
Also, is your current going to go up to 30A? If not, then you may have had a poor choice of the CT, you could have settled to a ACS712-5/ACS712-20 for better sensitivity over your entire operating range.
/*
Code modified by arif from inoace.com, All credit goes to original authors, apart from modifications, of readings to be dependent on current voltage and not ideal 5 volts
*/
float vcc = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
vcc = readVcc() / 1000.0;
Serial.print(“Vcc: “);
Serial.print(vcc);
long average = 0;
for (int i = 0; i < 100; i++) {
average = average + analogRead(A0);
delay(1);
}
average = average / 100;
float sensorValue = average * (5.0 / 1023.0);
Serial.print(” sense: “);
Serial.print(sensorValue, 3);
float acoffset = vcc / 2.0;
Serial.print(” offst: “);
Serial.print(acoffset, 3);
float sensitivity = 0.185 * (vcc / 5.0);
Serial.print(” sensi: “);
Serial.print(sensitivity, 4);
float amps = (sensorValue – acoffset) / sensitivity;
Serial.print(” Amperes: “);
Serial.print (amps);
Serial.println(“…”);
delay(1000);
}
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first – it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}
lol, i just linked to my blog in an answer, and just read afterwards , that its against rules to link to blogs ?
pardon me.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int analogPin = A0; int rawValue = 0; double current = 0.0; void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println("ACS712 30A DC Current Measurement"); Serial.println("---------------------------------"); } void loop() { // put your main code here, to run repeatedly: rawValue = analogRead(analogPin); delay(3); current = ((((double)rawValue * 5.0 / 1023.0) - 2.5) / 0.066); Serial.print(current); Serial.print(" A "); Serial.print(current * 1000.0); Serial.println(" mA"); delay(1000); }
Code C - [expand] 1 2 3 RawValue = analogRead(analogIn); Voltage = ((double)RawValue / 1023.0) * 5000.0; // Gets you mV Amps = ((Voltage - ACSoffset) / mVperAmp);
This is the code which I made and it works fine.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 int analogPin = A0; int rawValue = 0; double current = 0.0; void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println("ACS712 30A DC Current Measurement"); Serial.println("---------------------------------"); } void loop() { // put your main code here, to run repeatedly: rawValue = analogRead(analogPin); delay(3); current = ((((double)rawValue * 5.0 / 1023.0) - 2.5) / 0.066); Serial.print(current); Serial.print(" A "); Serial.print(current * 1000.0); Serial.println(" mA"); delay(1000); }
Your code modified.
Code C - [expand] 1 2 3 RawValue = analogRead(analogIn); Voltage = ((double)RawValue / 1023.0) * 5000.0; // Gets you mV Amps = ((Voltage - ACSoffset) / mVperAmp);
When using Arduino how does voltage fluctuates. I have tested this in hardware and it works fine. Arduino has a 5V regulator and voltage to Arduino is stable.
Maybe it is a problem with non-gunuine Arduino boards. I have used ACS712 5A, 20A, 30A sensors to measure both AC and DC current with Arduino and Genuino genuine boards with USB connection. I have all Genuine Arduino boards with me. If you want I can post a video of Arduino connected to USB measuring current using ACS712 5A sensor module.
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?