Using an Allegro ACS712 DC/AC Current Sensor with Arduino Code

Using an Allegro ACS712 DC/AC Current Sensor with Arduino Code

In this tutorial, we will learn how to use the Allegro ACS712 current sensor to measure both AC and DC current with an Arduino. The ACS712 sensor outputs a voltage proportional to the current flowing through it, allowing us to easily read and display the current values on the Arduino's serial monitor. This project is ideal for those looking to monitor current in various applications, such as power supplies or battery systems.

We will build a simple circuit that connects the ACS712 sensor to the Arduino, and then write a program to read the sensor's output. The expected outcome is to have real-time current readings displayed on the serial monitor. For clarification on the implementation, be sure to check the video (in video at 02:00).

Hardware Explained

The Allegro ACS712 current sensor is a compact module that can measure currents up to 30A, depending on the version you choose. It has three pins: VCC for power, GND for ground, and an output pin that provides the voltage corresponding to the measured current. The sensor uses a Hall effect principle, where the current passing through a conductor generates a magnetic field. This field is detected and converted into a voltage, which is then sent to the Arduino.

The module is available in different current ratings (5A, 20A, and 30A) and outputs voltage levels of 185 mV/A, 100 mV/A, and 66 mV/A respectively. This means that for every ampere of current flowing through the sensor, you will get a corresponding voltage output that you can read and process. This feature makes it versatile for various applications.

Datasheet Details

ManufacturerAllegro Microsystems
Part numberACS712ELCTR-30A-T
Logic/IO voltage4.5–5.5 V
Supply voltage5 V
Output current (per channel)30 A max
PWM frequency guidanceN/A
Input logic thresholdsN/A
Voltage drop / RDS(on) / saturation1.2 mΩ
Thermal limits150 °C max
PackageTO-220
Notes / variantsAvailable in 5A, 20A, and 30A

  • Ensure proper wiring to avoid short circuits.
  • Use appropriate gauge wire for current ratings (12 AWG for 30A).
  • Monitor the temperature of the module during operation.
  • Consider adding a heat sink for higher current applications.
  • Calibrate the sensor output based on your specific application.

Wiring Instructions

Arduino wiring for Allegro ACS712 current sensor

To wire the Allegro ACS712 sensor to your Arduino, start by connecting the VCC pin of the ACS712 to the 5V pin on the Arduino. Next, connect the GND pin of the sensor to one of the GND pins on the Arduino. The output pin of the ACS712 should be connected to the analog input pin A0 on the Arduino.

For the current measurement, connect the load in series with the sensor. This means that the positive wire from your power source should go into one of the current sensor's input terminals, while the other terminal connects to the load. The other side of the load should return to the power source, completing the circuit. Make sure to use appropriate wire gauge for the current you are measuring to avoid overheating.

Code Examples & Walkthrough

The following code snippet initializes the analog pin and sets up the serial monitor:

#define VIN A0 // define the Arduino pin A0 as voltage input (V in)
const float VCC = 5.0; // supply voltage is from 4.5 to 5.5V. Normally 5V.

Here, we define VIN as the analog input pin where the sensor output is connected. We also set the supply voltage VCC to 5V.

In the loop function, we read the voltage from the ACS712 sensor and calculate the current:

float voltage_raw = (5.0 / 1023.0) * analogRead(VIN); // Read the voltage from sensor
float current = voltage / sensitivity[model]; // Calculate current based on sensitivity

Here, we read the raw voltage from the sensor and convert it to the actual current using the sensitivity array based on the model of the sensor being used.

Finally, we print the current readings to the serial monitor:

if (abs(current) > cutOffLimit) {
    Serial.print("V: ");
    Serial.print(voltage, 3); // print voltage with 3 decimal places
    Serial.print("V, I: ");
    Serial.print(current, 2); // print the current with 2 decimal places
    Serial.println("A");
} else {
    Serial.println("No Current");
}

This section checks if the current exceeds a defined cutoff limit, and prints the voltage and current readings to the serial monitor. If the current is below the threshold, it outputs "No Current".

For the full code, please refer to the code loaded below the article.

Demonstration / What to Expect

When you run the code and power the circuit, you should see current readings displayed on the serial monitor. As you adjust the load, the readings should change accordingly, provided the current is above the cutoff limit. Be cautious of the temperature of the sensor, especially when operating near its maximum ratings.

Images

Arduino wiring for Allegro ACS712 current sensor
Arduino wiring for Allegro ACS712 current sensor
ACS712_in_series_with_load
ACS712_in_series_with_load
allegro_ACS712_module-1
allegro_ACS712_module-1
allegro_ACS712_module-2
allegro_ACS712_module-2
allegro_ACS712_module-4
allegro_ACS712_module-4
allegro_ACS712_module-0
allegro_ACS712_module-0
130-Allegro ACS712 DC/AC current sensor with Arduino code
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

Datasheet (pdf)

Fritzing File