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:
- What the ACS712 does and what its pins are for
- How to put the ACS712 in series with a 12V DC load and connect it to the Arduino Mega 2560
- How to program the Arduino Mega 2560 to calibrate the zero point and read DC current in amps
- How to program the Arduino Mega 2560 to read the RMS value of an AC current
We suggest:
- Utilizing a MOSFET module if you also want the Arduino Mega to switch the same 12V load ON and OFF, or to control its power.
- Utilizing a PC817 optocoupler if you only need to know whether a 12V or 24V signal is present, not how much current flows.

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
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

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:
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.

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:
- 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:
- A single reading is noisy, so many readings are added together and averaged:
- In setup(), while the load is still OFF, the same averaging (with 1000 readings) gives the real zero voltage:
- Finally, the current in amps is calculated with the formula:
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
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:
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:
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
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:
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.