ACS712 Using

hi ,guys i want to measure current using ACS712 and plz provide me with the sample code???

raghul_emcee:
hi ,guys i want to measure current

AC, DC, how much.

raghul_emcee:
using ACS712

Which one. There are many.

raghul_emcee:
and plz provide me with the sample code???

What have you done so far. Show us your code.
Did you do what CrossRoads told you.

Did you search the forum by entering "ACS712" in the search box on top of this page?
Leo..

im using acs712 30A module i want to measure DC

int sensorPin=A0;
void setup() {
  Serial.begin(9600);
  // put your setup code here, to run once:
  

}

void loop() {
  // put your main code here, to run repeatedly:
 
  double n=0;double s=0;
    for(int i=0;i<20;i++)
    {
    s=analogRead(sensorPin);
    s=(428-s);
    n=n+s;    
    } 
    n=(n/20)*10;
    Serial.println(n);  
    
    
    delay(25);
  

}

Some (untested) code, with smoothing, for a 30A bi-directional sensor.
Read the comments. You have to change/calibrate some things.

// 30A bi-directional ACS712

int sensorPin = A0; // the pin that the sensor is connected to

// YOUR calibration
float offset = 511.5; // zeroing bi-directional sensor | could be 510.0 to 513.0
float span = 0.07315; // span | depends on type of sensor used (e.g. 30A) | finetune max readout

const byte numReadings = 32; // number of readings for smoothing (max 64)
int readings[numReadings]; // raw A/D readings
byte index = 0; // index of the current reading
unsigned int total = 0; // running total
float current; // resulting current

void setup() {
  Serial.begin(9600); // ***set serial monitor to this value***
  for (index = 0; index < numReadings; index++) { // fast-fill the array at startup
    readings[index] = analogRead(sensorPin);
    total = total + readings[index];
  }
  index = 0; // reset index
}

void loop() {
  total = total - readings[index]; // subtract the last reading
  readings[index] = analogRead(sensorPin); // one unused reading to clear any ghost charge
  readings[index] = analogRead(sensorPin); // read the sensor
  total = total + readings[index]; // add the reading to the total
  index = index + 1; // advance to the next position in the array
  if (index >= numReadings) // if we're at the end of the array
    index = 0; // wrap around to the beginning

  // convert value to current
  current = (total / numReadings - offset) * span; // value to current conversion

  // print to serial monitor
  Serial.print("Current is  ");
  Serial.print(current); // default two decimal places | ...(current, 1);  is one decimal place
  Serial.println("  Amp");

  delay(100); // use a non-blocking delay when combined with other code
}

Leo…

Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp
Current is  -6.11  Amp

the output is not correct i have used a .3A source

Calibrate.

  1. Change "511.5" in small steps, and re-upload, untill you have ~0.00Amp in the readout without any current through the sensor.
    Start with whole numbers, e.g. 510, 511 or 512, and change to decimals after that.
    You might end up with e.g. 510.39

  2. Next send a known current through the sensor, and change the "span" number in small 0.000x steps untill you have the right value on the serial monitor.

Leo..

thanks for your reply,but im not getting around my values are around 430,429.without load

Is this a standard 5volt Arduino.
Is it only connected to USB, or also to an external supply.
Did you measure the 5volt pin (with a DMM), and the sensor output pin. Is the output 2.5volt without current.
Post a picture of your setup.
Leo..

Can you measure your aRef with a DMM? I have a Nano from Hossen with aRef of 4.41V, check that first.

i’m using nano

A lower Aref would mean a higher A/D value.
Leo..

how to measure aref in nano?
standard arduino
USB power supply
till now i didnt measure the aRef
@Leo@outsider

I don't expect problems with Aref.
As said, if Aref is lower than the 5volt rail, you will have a higher A/D value (>512) when you connect a 2.5volt sensor (no current) output to A0.
You say you have an A/D value of ~430 with CrossRoad's sketch.
That can only mean a lower sensor output voltage (~2.1volt).
Measure and report back.
A picture could help.
Leo..

my aREF is 3.68v

i'hv tried many times still getting 430,and do i have to make any changes regarding the aref=3.68?

sorry thats the wrong aref,the correct is 4.35v

raghul_emcee:
my aREF is 3.68v

And the 5volt pin?
And the sensor output?

which 5v pin are you asking about.?
my current ( CURRENT SENSOR ) reading is 430 and 431 without load alternatively

float amps = 0;
float maxAmps = 0;
float minAmps = 0;
float lastAmps = 0;
float noise = 0;
int analogInPin2=A0;
double in;
double resultV;
double meanV;

void setup() {
Serial.begin(9600);
}

void loop() {
 for(int i=0;i<20;i++)//mean 
  {
  in=analogRead(analogInPin2);
  resultV=((2.1041057-(in*5)/1023))/.666+resultV;//cal V
  //delay(20);
  }
  meanV=resultV/20; 
   
  Serial.println(meanV);
}

2.PNG

1.PNG

raghul_emcee:
which 5v pin are you asking about.?

Really?
To which three pins did you connect the sensor to.
High time you posted a picture.
Leo..

sensor vcc-arduino vin
sensor gnd-arduino gnd
out - A0