# ACS712 Current Sensor with Arduino Tutorial (Sensing DC and AC Current)

ACS712 is a hall effect current sensor. Hall effect will convert the magnetic field created by the current that flows through the sensor to voltage directly proportional to it and makes this sensor able to generate voltage linear with the current that flows through this sensor.

ACS712 has 3 types according to the maximum range of the current measurement. The first is 5A, then 20 amp max, and for 30A max.
So you can choose which is the best sensor for your project. Which range of current do you want to measure. And if you measure a pretty small current like below .4 amp. I don’t recommend using this sensor. Get a shunt current sensor for DC current measurement or CT type for AC measurement instead. Because they will have more sensitivity for small current. It is really difficult to sense a small current using this sensor.

## ACS712 Voltage Output

• 1/2 VCC when no load or it is 2.5V since we use 5V as VCC.
• 5A type generate 185mV every amp current
• 20A type generate 100mV every amp current
• 30A type generate 66mV every amp current

## ACS712 Wiring Diagram with Arduino

In this example, I will measure the current that flows from a 12V power supply to a 12V DC lamp. So the wiring diagram becomes like this.

## The Code to Measure DC Current

So here’s the sketch to measure DC current on pin A0.

```void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
float current = (voltage-2.5)/0.185;
Serial.print("Current : ");
Serial.println(current);
delay(300);
}```

Sometimes the sketch above will give us a little noise when no load is applied. So we can ignore noise by ignoring reading below a certain value. In this example, I will ignore the reading below 0.16 A.

```void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
float voltage = adc * 5 / 1023.0;
float current = (voltage - 2.5) / 0.185;
if (current < 0.16) {
current = 0;
}
Serial.print("Current : ");
Serial.println(current);
delay(300);
}```

## ACS712 Wiring Diagram with Arduino for AC Current

OK, that is for the DC load, now I will tell you how to use it with AC current. First, For an AC let’s change the load and the source. I will connect the sensor directly from an AC source. In my country, it’s 220V AC. In your country, it can be 110V. And the load I change to a terminal, so I can plug anything to it.

Like the name, Alternating Current means the current is alternating, so it is not so easy to measure the current. Because it has different values at any time. So you have to find the RMS value. To simplify this, I will just use a library.

You can download the ACS712 library here. Just download the library extract at your Arduino library folder and restart the Arduino. Once it’s done, you can use the code below.

```/*ACS712 Current Sensor Demo
By miliohm.com
*/

#include "ACS712.h"

ACS712 sensor(ACS712_05B, A0);
//ACS712_20A for 20 Amp type
//ACS712_30A for 30 Amp type

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

void loop() {
float I = sensor.getCurrentAC();
//ignoring the value below 0.09
if (I < 0.09) {
I = 0;
}
Serial.println(I);
delay(300);
}```

Use the sketch above and upload it. For this demonstration, I’ll use an Iron. And that’s it when I turned on the Iron, the current will be measured clearly. It is about 1.48 to 1.5

## Displaying the Current on the LCD

You can follow this wiring diagram to display the current to an LCD. I use an I2C LCD backpack with a 16X2 LCD.

You need to download the library for the LCD here. If you need a detailed tutorial on how to use I2C LCD you can find it here. Here’s the sketch to display the current value to an LCD.

```/*ACS712 Current Sensor Demo
By miliohm.com
*/

#include "ACS712.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

ACS712 sensor(ACS712_05B, A0);
//ACS712_20A for 20 Amp type
//ACS712_30A for 30 Amp type

void setup() {
Serial.begin(9600);
sensor.calibrate();
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("ACS712 Demo");
lcd.print("By miliohm.com");
}

void loop() {
float I = sensor.getCurrentAC();
if (I < 0.09) {
I = 0;
}
lcd.setCursor(0, 0);
lcd.print("Current : ");
lcd.print(I);
lcd.print(" A");
delay(300);
}```

That’s it the current is now displayed on the LCD. Here’s the complete video tutorial on my channel if you need it.