(https://i.ibb.co/fHK25J3/ACS712-hookup-arduino.webp) (https://ibb.co/fHK25J3) (https://i.ibb.co/C6HZM7Y/ACS712-Arduino-Circuit.jpg) (https://ibb.co/C6HZM7Y)
/*
ACS712 Current Sensor
modified on 06 Sep 2020
by Mohammad Reza Akbari @ Electropeak
Home
*/
int analogPin = A0; // Current sensor output
const int averageValue = 500;
long int sensorValue = 0; // variable to store the sensor value read
float voltage = 0;
float current = 0;
void setup() {
Serial.begin(9600); // setup serial
}
void loop() {
for (int i = 0; i < averageValue; i++)
{
sensorValue += analogRead(analogPin);
// wait 2 milliseconds before the next loop
delay(2);
}
sensorValue = sensorValue / averageValue;
voltage = sensorValue * 5.0 / 1024.0;
current = (voltage - 2.5) / 0.185;
Serial.print("ADC Value: ");
Serial.print(sensorValue);
Serial.print(" ADC Voltage: ");
Serial.print(voltage);
Serial.print("V");
Serial.print(" Current: ");
Serial.print(current);
Serial.println("A");
}
_______________________________________________________________________________________________________________________________________________________
/*
ACS712 Current Sensor
*/
void setup() {
Serial.begin(9600); //Start Serial Monitor to display current read value on Serial monitor
}
void loop() {
unsigned int x=0;
float AcsValue=0.0,Samples=0.0,AvgAcs=0.0,AcsValueF=0.0;
for (int x = 0; x < 150; x++){ //Get 150 samples
AcsValue = analogRead(A0); //Read current sensor values
Samples = Samples + AcsValue; //Add samples together
delay (3); // let ADC settle before next sample 3ms
}
AvgAcs=Samples/150.0;//Taking Average of Samples
//((AvgAcs * (5.0 / 1024.0)) is converitng the read voltage in 0-5 volts
//2.5 is offset(I assumed that arduino is working on 5v so the viout at no current comes
//out to be 2.5 which is out offset. If your arduino is working on different voltage than
//you must change the offset according to the input voltage)
//0.185v(185mV) is rise in output voltage when 1A current flows at input
AcsValueF = (2.5 - (AvgAcs * (5.0 / 1024.0)) )/0.185;
Serial.print(AcsValueF);//Print the read current on Serial monitor
delay(50);
}