Arduino Mega - ACS712 Current Sensor

This tutorial instructs you how to use the Arduino Mega 2560 to find out how many amps a device really draws, with the help of an ACS712 30A current sensor module. A motor that is about to stall, a heater that is left on, or a charger that has finished its job all show up as a change in current. In detail, we will learn:

We suggest:

Arduino Mega and ACS712 current sensor module

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×ACS712 30A Current Sensor Module
1×12V DC Device (e.g. 12V DC Motor)
1×Alternatively, 12V DC Device (e.g. 12V LED Strip)
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of ACS712 Current Sensor

The ACS712 from Allegro is a current sensor that works with the Hall effect. Inside the chip, the measured current passes through a thick copper path. This current creates a magnetic field, and a Hall sensor placed right next to the path turns that field into a voltage. The copper path has a resistance of only about 1.2 mΩ, so it hardly disturbs the circuit. Also, the copper path is electrically isolated from the sensor pins (2.1 kV RMS), so the load circuit and the Arduino Mega never touch each other.

The chip comes in three versions. They have the same pins and the same code; only the sensitivity and the range are different:

ACS712 5A ACS712 20A ACS712 30A
Current range −5A to +5A −20A to +20A −30A to +30A
Sensitivity 185 mV/A 100 mV/A 66 mV/A

This tutorial uses the 30A version (chip ACS712ELCTR-30A-T). Its main specifications are:

ACS712 30A
Supply voltage 5V (4.5V to 5.5V)
Measured current DC and AC, −30A to +30A
Sensitivity 66 mV per amp
Output at 0A half of the supply, about 2.5V
Output at +30A / −30A about 4.48V / about 0.52V
Bandwidth 80 kHz
Total output error about ±1.5% at 25°C
Isolation 2.1 kV RMS

The output is ratiometric. This means the zero point and the sensitivity follow the real 5V supply. If the 5V pin of the Arduino Mega gives 4.95V, the zero point moves a little too. This is why the code measures the zero point itself instead of trusting the 2.5V value.

The ACS712 Current Sensor Pinout

ACS712 current sensor module pinout

The module has a 3-pin header on one side and a 2-pin screw terminal on the other side.

Header side (to the Arduino Mega):

  • VCC pin: must be connected to the 5V pin of the Arduino Mega. The sensor does not work properly from 3.3V.
  • GND pin: must be connected to GND (0V) of the Arduino Mega.
  • OUT pin: the analog output. It must be connected to an analog input pin of the Arduino Mega.

Screw terminal side (to the load):

  • IP+ and IP− terminals: the current path. They must be placed in series with the load. This means you cut one supply wire of the load and connect its two ends to these two terminals. Current flowing from IP+ to IP− gives a positive value. On some modules these terminals have no label.

The small LED on the board is a power indicator. It lights up when VCC gets 5V.

※ NOTE THAT:

The arrangement of pins on a module may differ from one manufacturer to another. It is essential to always refer to the labels printed on the module when using it. Take a close look!

How the ACS712 Converts Current to Voltage

When no current flows, the OUT pin sits in the middle, at about 2.5V. Current in one direction pushes the output up, and current in the other direction pulls it down. Every amp moves the output by 66 mV. So the Arduino Mega only needs to measure the output voltage and do one small calculation:

Current OUT voltage (with a 5V supply)
−10A about 1.84V
0A about 2.50V
+1A about 2.57V
+10A about 3.16V
+30A about 4.48V

The formula is:

current_A = (Vout - Vzero) / 0.066;

Here, Vout is the voltage on the OUT pin and Vzero is the voltage measured when no current flows.

Wiring Diagram

The Arduino Mega 2560 works at 5V, and the ACS712 output never goes above about 4.5V, so the OUT pin goes straight to analog pin A0 without any resistor. The 12V DC device (such as a DC motor, LED strip or lamp) gets its power from the adapter, and its positive wire passes through the screw terminal of the sensor.

The wiring diagram between Arduino Mega ACS712 current sensor

This image is created using Fritzing. Click to enlarge image

From To
ACS712 VCC Arduino Mega 5V
ACS712 GND Arduino Mega GND
ACS712 OUT Arduino Mega A0
12V adapter + (via DC power jack) ACS712 IP+ terminal
ACS712 IP− terminal DC device + wire
DC device − wire 12V adapter − (via DC power jack)

WARNING

The 12V of the adapter goes only to the screw terminal of the sensor and to the DC device. Never connect it to any pin of the Arduino Mega.

The Arduino Mega has 16 analog inputs (A0 to A15). If A0 is busy in your project, you can use any other one; just change the pin number in the code.

※ NOTE THAT:

Mains AC (110V or 220V) is dangerous. If you are a beginner, practice with a low-voltage DC load like the 12V DC device in this tutorial. The screw terminal side of the module carries the full voltage of the load, so never touch the screw terminal or its wires while a mains circuit is live.

How To Program For ACS712 Current Sensor

No library is needed. The code only uses built-in Arduino functions.

  • The first step is to specify the Arduino Mega pin that is connected to the OUT pin of the sensor, and the sensitivity of the 30A version:
const int SENSOR_PIN = A0; const float SENSITIVITY = 0.066; // 66 mV per amp
  • The analog value (0 to 1023) is converted to a voltage. The Arduino Mega has a 10-bit ADC with a 5V reference, so one step is about 4.9 mV:
float voltage = analogRead(SENSOR_PIN) * 5.0 / 1023;
  • A single reading is noisy, so many readings are added together and averaged:
long sum = 0; for (int i = 0; i < 200; i++) { sum += analogRead(SENSOR_PIN); } float voltage = (sum / 200.0) * 5.0 / 1023;
  • In setup(), while the load is still OFF, the same averaging (with 1000 readings) gives the real zero voltage:
zeroVoltage = readAverageVoltage(1000);
  • Finally, the current in amps is calculated with the formula:
float current = (voltage - zeroVoltage) / SENSITIVITY;

Arduino Mega 2560 Code for ACS712 Current Sensor

The code below measures DC current. It works like this:

  • At startup, it reads the sensor 1000 times with the load OFF and saves the result as the zero voltage
  • Every 0.5 seconds, it averages 200 readings, then prints the voltage and the current to the Serial Monitor
/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-acs712-current-sensor */ const int SENSOR_PIN = A0; // Arduino Mega pin connected to the OUT pin of the ACS712 const float VREF = 5.0; // ADC reference voltage of the Arduino Mega const int ADC_MAX = 1023; // 10-bit ADC: values from 0 to 1023 const float SENSITIVITY = 0.066; // ACS712 30A version: 66 mV per amp const int SAMPLES = 200; // number of readings averaged for one measurement float zeroVoltage = 2.5; // sensor output at 0 A, measured again in setup() // read the sensor output many times and return the average voltage float readAverageVoltage(int count) { long sum = 0; for (int i = 0; i < count; i++) { sum += analogRead(SENSOR_PIN); } return (sum / (float)count) * VREF / ADC_MAX; } void setup() { Serial.begin(9600); // zero calibration: the load MUST be OFF at this moment Serial.println("Calibrating... keep the load OFF"); delay(500); zeroVoltage = readAverageVoltage(1000); Serial.print("Zero voltage: "); Serial.print(zeroVoltage, 3); Serial.println(" V"); } void loop() { float voltage = readAverageVoltage(SAMPLES); float current = (voltage - zeroVoltage) / SENSITIVITY; // in amps Serial.print("Voltage: "); Serial.print(voltage, 3); Serial.print(" V | Current: "); Serial.print(current, 2); Serial.println(" A"); delay(500); }

Detailed Instructions

  • If this is the first time you use Arduino Mega 2560, see how to setup environment for Arduino Mega on Arduino IDE.
  • Wire the components as shown in the diagram. Leave the 12V adapter unplugged for now, so the DC device is OFF.
  • Connect the Arduino Mega board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Select the Arduino Mega or Mega 2560 board and the correct COM port.
  • Copy the above code and paste it into the Arduino IDE.
  • Click the Upload button to upload the code to the Arduino Mega.
  • Open the Serial Monitor and set the baud rate to 9600. Wait until the zero voltage is printed. The load must stay OFF during this short calibration.
  • Plug in the 12V power adapter to turn the DC device on.
  • See the result on the Serial Monitor:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Calibrating... keep the load OFF Zero voltage: 2.507 V Voltage: 2.509 V | Current: 0.03 A Voltage: 2.505 V | Current: -0.03 A Voltage: 2.511 V | Current: 0.06 A Voltage: 2.651 V | Current: 2.18 A Voltage: 2.583 V | Current: 1.15 A Voltage: 2.574 V | Current: 1.02 A Voltage: 2.578 V | Current: 1.08 A Voltage: 2.572 V | Current: 0.98 A
Ln 11, Col 1
Arduino Mega Or... on COM15
2

This example uses a 12V DC motor as the DC device. The first lines show a few tens of milliamps even with the motor OFF. This is normal noise for the 30A version. When the motor starts, the current jumps for a moment (about 2A here), then settles at about 1A while it spins freely. If you hold the motor shaft with your fingers, the current goes up. A stalled motor draws the highest current.

If the current is negative when you expect a positive value, the current flows from IP− to IP+. Swap the two wires on the screw terminal, or just read the absolute value.

Measuring AC Current

An AC current changes its direction many times per second (50 or 60 times, depending on your country). The output of the ACS712 therefore swings above and below the zero voltage, and a simple average would give almost 0A. Instead, we measure the RMS (root mean square) value, which is the number written on AC devices and shown by a multimeter.

The idea is simple:

  • Read the sensor as fast as possible for 200 ms. This covers 10 full cycles at 50 Hz, or 12 cycles at 60 Hz.
  • For each reading, take its distance from the zero voltage and square it.
  • Average the squares, take the square root, and divide by 0.066:
rmsCurrent = sqrt(sumSquares / count) / 0.066;

The wiring to the Arduino Mega is the same as above. Only the screw terminal is now in series with one wire of the AC device.

WARNING

Mains AC is dangerous and can kill. Only work on it if you have the right skills. Switch the mains OFF before you touch the wiring, keep the module in a closed box, and never touch the screw terminal while the device is plugged in.

Arduino Mega 2560 Code for AC Current

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-acs712-current-sensor */ const int SENSOR_PIN = A0; // Arduino Mega pin connected to the OUT pin of the ACS712 const float VREF = 5.0; // ADC reference voltage of the Arduino Mega const int ADC_MAX = 1023; // 10-bit ADC: values from 0 to 1023 const float SENSITIVITY = 0.066; // ACS712 30A version: 66 mV per amp const unsigned long WINDOW_MS = 200; // 200 ms = 10 cycles at 50 Hz, 12 cycles at 60 Hz float zeroVoltage = 2.5; // sensor output at 0 A, measured again in setup() void setup() { Serial.begin(9600); // zero calibration: no current may flow at this moment Serial.println("Calibrating... keep the load OFF"); delay(500); long sum = 0; for (int i = 0; i < 1000; i++) { sum += analogRead(SENSOR_PIN); } zeroVoltage = (sum / 1000.0) * VREF / ADC_MAX; Serial.print("Zero voltage: "); Serial.print(zeroVoltage, 3); Serial.println(" V"); } void loop() { // sample the output during the whole time window float sumSquares = 0; long count = 0; unsigned long start = millis(); while (millis() - start < WINDOW_MS) { float voltage = analogRead(SENSOR_PIN) * VREF / ADC_MAX; float diff = voltage - zeroVoltage; // distance from the zero point sumSquares += diff * diff; count++; } // RMS voltage, then RMS current float rmsVoltage = sqrt(sumSquares / count); float rmsCurrent = rmsVoltage / SENSITIVITY; Serial.print("AC current (RMS): "); Serial.print(rmsCurrent, 2); Serial.println(" A"); delay(500); }

Upload it in the same way as the DC code. Keep the AC device OFF while the board starts, then turn it on. The example output below comes from a 230V hair dryer on its low setting:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Calibrating... keep the load OFF Zero voltage: 2.503 V AC current (RMS): 0.09 A AC current (RMS): 0.08 A AC current (RMS): 3.91 A AC current (RMS): 3.88 A AC current (RMS): 3.94 A
Ln 11, Col 1
Arduino Mega Or... on COM15
2

With the device OFF, the RMS value is not exactly 0A, because the noise is also squared and added. A small value below about 0.1A can be treated as 0A.

Additional Knowledge

Small Currents and the 30A Version

The Arduino Mega ADC has 1024 steps over 5V, so one step is about 4.9 mV. With 66 mV per amp, one step means about 74 mA. In addition, the sensor output has some noise, so ±50 to 100 mA at 0A is normal.

Load Typical current Good fit for the 30A version?
Small LED or sensor under 0.1A no, the value is lost in the noise
12V LED strip (1 meter) about 0.5A yes, but averaging is important
12V DC motor about 0.5A to 3A yes
Heater, kettle, power tool 5A to 20A yes

It is evident that the 30A version fits loads of about 0.5A and up. For smaller loads, the 5A version (185 mV/A) gives almost three times finer readings with the same wiring and code. Just change the sensitivity in the code to 0.185.

Tips for Stable Readings

  • Always calibrate the zero point with the load OFF. If you power up the board while the DC device is running, every reading will be wrong by the same amount.
  • Average more readings for a steadier value, or fewer readings for a faster response.
  • Keep the sensor wires short and away from the DC device wires.
  • Power the Arduino Mega from a stable 5V source, because the sensor output follows the 5V supply.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!