Arduino Giga R1 WiFi ACS712 Current Sensor

In this guide, we will learn how to use the ACS712 30A current sensor with the Arduino Giga R1 WiFi to find out how many amps a load really pulls. In detail, we will learn:

Arduino Giga R1 WiFi and ACS712 30A current sensor module

Knowing the current is handy in many projects. A motor that pulls much more than normal is probably stuck. A charger that drops to almost zero amps is probably done. The Giga reads its analog pins with 12 bits, so it can follow these changes in fine steps.

Overview of ACS712 Current Sensor

The ACS712 is a small chip from Allegro that measures current with the Hall effect. Inside the chip, the load current runs through a thick copper path. This current makes a magnetic field, and a Hall sensor right next to the path turns that field into a voltage. The copper path has only about 1.2 mΩ of resistance, so it hardly changes your circuit. And the load side is isolated from the sensor side (2.1 kV RMS), so the Giga never touches the load wires.

The chip comes in three current ranges. They look almost the same, so read the text on the chip before you start. This guide uses the 30A version.

Version Range Sensitivity Output at the highest current
ACS712ELCTR-05B −5A to +5A 185 mV per amp about 3.43V
ACS712ELCTR-20A −20A to +20A 100 mV per amp about 4.5V
ACS712ELCTR-30A −30A to +30A 66 mV per amp about 4.48V

Main data of the 30A module:

Feature ACS712 30A
Supply voltage 5V (4.5V to 5.5V)
Output at 0A about half of VCC, so 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
Measures DC and AC current

How the output moves:

  • With no current, OUT stays near 2.5V.
  • Current that enters at IP+ and leaves at IP− pushes OUT up, by 66 mV for each amp.
  • Current the other way pulls OUT down by the same 66 mV per amp.
  • The output is ratiometric. It follows the real 5V supply. If USB gives only 4.9V, the zero point moves to about 2.45V. This is why the code measures the zero point at start instead of trusting 2.5V.

Be realistic about small loads. With the divider and the 12-bit ADC of the Giga, one ADC step is about 18 mA of current. But the module itself has noise, so seeing ±0.05A to ±0.1A with nothing connected is normal. This 30A version works best for loads of about 0.5A and up: motors, lamps, LED strips, heaters and chargers. For tiny currents, the 5A version is a better pick.

Pinout

ACS712 current sensor module pinout

Signal header (3 pins, goes to the Giga side):

  • VCC pin: connect to 5V. The chip does not work well from 3.3V.
  • GND pin: connect to GND (0V)
  • OUT pin: analog voltage, about 2.5V at 0A. Connect it to a Giga analog pin through the voltage divider, never directly.

Screw terminal (2 terminals, goes to the load side):

  • IP+ and IP−: put them in series with the load. Cut one supply wire of the load and fix the two cut ends into these terminals. On some modules these two terminals have no label.

There is also a red power LED. It lights up when VCC gets 5V.

Wiring Diagram

Every analog pin of the Arduino Giga R1 WiFi accepts 3.3V at most. The ACS712 must run from 5V, and its OUT pin can rise to about 4.5V. So we put two resistors between OUT and A0. They scale the signal down to two thirds, which turns 4.5V into a safe 3.0V.

The wiring diagram between Arduino Giga R1 WiFi ACS712 current sensor

This image is created using Fritzing. Click to enlarge image

Sensor side:

From To
ACS712 VCC Arduino Giga R1 WiFi 5V
ACS712 GND Arduino Giga R1 WiFi GND
ACS712 OUT one end of the 10kΩ resistor
Other end of the 10kΩ resistor Arduino Giga R1 WiFi A0, and one end of the 20kΩ resistor
Other end of the 20kΩ resistor Arduino Giga R1 WiFi GND

Load side:

From To
12V adapter + (via DC power jack) ACS712 IP+
ACS712 IP− DC device +
DC device − 12V adapter − (via DC power jack)

The isolation is already inside the chip, so there's no need to join the 12V ground with the Giga ground. This makes wiring or soldering easier. If you have no 20kΩ resistor, two 10kΩ resistors in series do the same job.

※ NOTE THAT:

Mains AC (110V/220V) can kill. Learn with a low-voltage DC load like the 12V DC device here first. The screw terminal and the copper side of the module carry the full load voltage, so never touch them, or any wire on them, while a mains circuit is live.

How To Program For ACS712 Current Sensor

No library is needed. The sketch uses only analogRead() and a little math.

  • Set the analog pin and the numbers of your hardware:
const int SENSOR_PIN = A0; const float ADC_REF = 3.3; // Giga analog range const int ADC_MAX = 4095; // 12-bit const float DIVIDER_RATIO = 1.5; // (10k + 20k) / 20k const float SENSITIVITY = 0.066; // 66 mV per amp
  • Switch the ADC to 12-bit mode in setup():
analogReadResolution(12);
  • Average many readings, then turn the result into the OUT voltage. Multiplying by 1.5 undoes the divider:
float pinVoltage = (sum / (float)count) * ADC_REF / ADC_MAX; float voltage = pinVoltage * DIVIDER_RATIO;
  • Learn the zero point once at start, while no current flows:
zeroVoltage = readSensorVoltage(1000);
  • Turn the voltage into amps:
float current = (voltage - zeroVoltage) / SENSITIVITY;

The formula is simple: current in amps = (Vout − Vzero) / 0.066. For example, if OUT sits at 2.556V and the zero point was 2.489V, then (2.556 − 2.489) / 0.066 ≈ 1.02A.

Avoid a global variable named error in your own sketches on this board. The Giga core is built on Mbed OS, which already has a function called error(), and the two names clash at compile time.

Arduino Giga R1 WiFi Code - ACS712 Current Sensor

What the sketch does:

  1. Waits one second for the sensor to settle.
  2. Averages 1000 readings with the load off and keeps this as the zero point.
  3. Every half second, averages 300 new readings and calculates the current.
  4. Prints the OUT voltage and the current. A minus sign means the current flows the other way.
/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-acs712-current-sensor */ // Arduino Giga R1 WiFi - measure DC current with the ACS712 30A module // ACS712 VCC -> 5V, GND -> GND // ACS712 OUT -> 10k resistor -> A0 -> 20k resistor -> GND (keeps A0 below 3.3V) // No library needed. const int SENSOR_PIN = A0; // analog pin in the middle of the divider const float ADC_REF = 3.3; // Giga analog pins measure 0 to 3.3V const int ADC_MAX = 4095; // 12-bit reading: 0 to 4095 const float DIVIDER_RATIO = 1.5; // (10k + 20k) / 20k: turns the A0 voltage back into the OUT voltage const float SENSITIVITY = 0.066; // 30A version: 66 mV (0.066V) per amp const int SAMPLES = 300; // readings averaged for one result float zeroVoltage = 2.5; // OUT voltage at 0A, measured again in setup() // Average many readings and return the voltage on the ACS712 OUT pin float readSensorVoltage(int count) { long sum = 0; for (int i = 0; i < count; i++) { sum += analogRead(SENSOR_PIN); } float pinVoltage = (sum / (float)count) * ADC_REF / ADC_MAX; return pinVoltage * DIVIDER_RATIO; } void setup() { Serial.begin(9600); analogReadResolution(12); // use the full 12-bit ADC of the Giga delay(1000); // let the sensor power settle // The load MUST be OFF here, so we can learn the real zero point Serial.println("Calibrating... keep the load OFF"); zeroVoltage = readSensorVoltage(1000); Serial.print("Zero point: "); Serial.print(zeroVoltage, 3); Serial.println(" V"); } void loop() { float voltage = readSensorVoltage(SAMPLES); float current = (voltage - zeroVoltage) / SENSITIVITY; // amps; the sign shows the direction Serial.print("OUT: "); Serial.print(voltage, 3); Serial.print(" V | Current: "); Serial.print(current, 2); Serial.println(" A"); delay(500); }

Detailed Instructions

Follow these instructions step by step:

  • New to this board? Set it up first with the Arduino Giga R1 WiFi getting started guide.
  • Wire the components according to the provided diagram. Check the divider twice: A0 must sit between the 10kΩ and the 20kΩ resistor.
  • Keep the 12V adapter unplugged, so the DC device is off.
  • Plug the Giga R1 WiFi into your computer with the USB Type-C cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Giga R1 WiFi board (e.g., Arduino Giga R1 WiFi) and COM port.
  • Paste the code above into a new sketch.
  • Press the Upload button, then open the Serial Monitor at 9600 baud.
  • Wait until the zero point is printed. The load must stay off until then.
  • Now plug in the 12V adapter. The DC device turns on, and the current shows up in the Serial Monitor.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' on 'COM15')
New Line
9600 baud
Calibrating... keep the load OFF Zero point: 2.489 V OUT: 2.491 V | Current: 0.03 A OUT: 2.487 V | Current: -0.03 A OUT: 2.490 V | Current: 0.02 A OUT: 2.583 V | Current: 1.42 A OUT: 2.559 V | Current: 1.06 A OUT: 2.552 V | Current: 0.95 A OUT: 2.561 V | Current: 1.09 A OUT: 2.556 V | Current: 1.02 A
Ln 11, Col 1
Arduino Giga R1 on COM15
2

The first three lines are the idle noise, a few hundredths of an amp around zero. With a 12V DC motor as the DC device, the 1.42A line is the short surge when the motor starts. After that, the motor settles near 1A. Hold the motor shaft lightly with your fingers and the current goes up, because a motor under load pulls more.

Do you see negative values while the DC device is on? Then the current enters at IP− instead of IP+. Swap the two wires on the screw terminal, or just ignore the sign.

Measuring AC Current

With AC, the current changes direction many times every second (50 or 60 times, depending on your country). So the OUT pin swings up and down around the zero point, and a simple average is close to 0A. What we need is the RMS value. This is the number that a multimeter shows for AC.

How the second sketch does it:

  1. For 200 ms, it reads A0 as fast as it can. 200 ms covers 10 full cycles at 50Hz and 12 at 60Hz.
  2. For each reading, it takes the distance from the zero point and squares it.
  3. At the end, it takes the square root of the average: RMS = sqrt(mean((V − Vzero)²)).
  4. It divides the result by 0.066 to get amps RMS.

The wiring on the Giga side does not change. The load side changes: the ACS712 sits in series with one wire of the AC device.

※ NOTE THAT:

Again: mains voltage is dangerous. Only work with mains if you have the skill to do it safely, and never touch the module's screw terminal while the circuit is plugged in.

/* * This Arduino Giga R1 WiFi code was developed by newbiely.com * * This Arduino Giga R1 WiFi code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-giga/arduino-giga-r1-wifi-acs712-current-sensor */ // Arduino Giga R1 WiFi - measure AC current (RMS) with the ACS712 30A module // ACS712 VCC -> 5V, GND -> GND // ACS712 OUT -> 10k resistor -> A0 -> 20k resistor -> GND (keeps A0 below 3.3V) // No library needed. const int SENSOR_PIN = A0; const float ADC_REF = 3.3; // Giga analog pins measure 0 to 3.3V const int ADC_MAX = 4095; // 12-bit reading const float DIVIDER_RATIO = 1.5; // undo the 10k/20k divider const float SENSITIVITY = 0.066; // 30A version: 0.066V per amp const unsigned long WINDOW_MS = 200; // 200 ms = 10 cycles at 50Hz, 12 cycles at 60Hz float zeroVoltage = 2.5; // OUT voltage at 0A, measured in setup() // Convert one ADC reading into the voltage on the ACS712 OUT pin float toSensorVoltage(float raw) { return raw * ADC_REF / ADC_MAX * DIVIDER_RATIO; } void setup() { Serial.begin(9600); analogReadResolution(12); delay(1000); // No current may flow now: the load must be switched OFF Serial.println("Calibrating... keep the load OFF"); long sum = 0; for (int i = 0; i < 1000; i++) { sum += analogRead(SENSOR_PIN); } zeroVoltage = toSensorVoltage(sum / 1000.0); Serial.print("Zero point: "); Serial.print(zeroVoltage, 3); Serial.println(" V"); } void loop() { // Sample for the whole window and add up the squared distance from zero double sumSquares = 0; long count = 0; unsigned long start = millis(); while (millis() - start < WINDOW_MS) { float diff = toSensorVoltage(analogRead(SENSOR_PIN)) - zeroVoltage; sumSquares += diff * diff; count++; } float rmsVoltage = sqrt(sumSquares / count); // RMS of the swing around zero float rmsCurrent = rmsVoltage / SENSITIVITY; // amps RMS Serial.print("AC current: "); Serial.print(rmsCurrent, 2); Serial.println(" A (RMS)"); delay(300); }

Upload it the same way, with the AC device switched off at power-up. Then switch the device on. Here is what a small heater looked like:

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Giga R1
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Giga R1' on 'COM15')
New Line
9600 baud
Calibrating... keep the load OFF Zero point: 2.493 V AC current: 0.07 A (RMS) AC current: 0.08 A (RMS) AC current: 3.41 A (RMS) AC current: 3.38 A (RMS) AC current: 3.44 A (RMS)
Ln 11, Col 1
Arduino Giga R1 on COM15
2

With the device off, the RMS value is not exactly zero. Noise is always positive after squaring, so 0.05A to 0.1A at idle is normal. You can subtract this small value in code if it bothers you.

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!