Example Code for Arduino-Read AC Current

This example demonstrates how to read data from the Analog AC Current Sensor.

Hardware Preparation

Arduino UNO or simliar x1
AC Current Signal Conversion Module x1
Open Type AC Transformer Probe x1
Gravity-3P Analog Sensor Cable x1

Software Preparation

Arduino IDE V1.6.5 Click to Download Arduino IDE from Arduino®

Wiring Diagram

> The AC transformer probe can only clamp to one of the AC wire. It cannot be clamped both at the same time\!
## Other Preparation Work
  • Connect the module to the A2 port of the Arduino UNO according to the wiring diagram.
  • Modify the value of #define ACDetectionRange 20 in the sample code based on the range of the AC transformer you are using. For example, if the transformer range is 5A (SEN0287), set this value to 5.

About Calibration
The analog reading is affected by the accuracy of the reference voltage. For higher measurement accuracy, use a high-precision multimeter to measure the controller’s analog reference voltage (usually equal to the supply voltage), and then modify the value of #define VREF 5.0 in the sample code accordingly.

Sample Code

/*!
 * @file readACCurrent.
 * @n This example reads Analog AC Current Sensor.

 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (https://www.dfrobot.com)
 * @licence     The MIT License (MIT)
 * @get from https://www.dfrobot.com

 Created 2016-3-10
 By berinie Chen <[email protected]>
 
 Revised 2019-8-6
 By Henry Zhao<[email protected]>
*/

const int ACPin = A2;         //set arduino signal read pin
#define ACTectionRange 20;    //set Non-invasive AC Current Sensor tection range (5A,10A,20A)

// VREF: Analog reference
// For Arduino UNO, Leonardo and mega2560, etc. change VREF to 5
// For Arduino Zero, Due, MKR Family, ESP32, etc. 3V3 controllers, change VREF to 3.3
#define VREF 5.0

float readACCurrentValue()
{
  float ACCurrtntValue = 0;
  float peakVoltage = 0;  
  float voltageVirtualValue = 0;  //Vrms
  for (int i = 0; i < 5; i++)
  {
    peakVoltage += analogRead(ACPin);   //read peak voltage
    delay(1);
  }
  peakVoltage = peakVoltage / 5;   
  voltageVirtualValue = peakVoltage * 0.707;    //change the peak voltage to the Virtual Value of voltage
  
  /*The circuit is amplified by 2 times, so it is divided by 2.*/
  voltageVirtualValue = (voltageVirtualValue / 1024 * VREF ) / 2;  
  
  ACCurrtntValue = voltageVirtualValue * ACTectionRange;
  
  return ACCurrtntValue;
}

void setup() 
{
  Serial.begin(115200);
  pinMode(13, OUTPUT);
  
}

void loop() 
{
  float ACCurrentValue = readACCurrentValue(); //read AC Current Value
  Serial.print(ACCurrentValue);
  Serial.println(" A");
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

Result

Open the Serial Monitor, set the baud rate to 115200, and observe the data output.

Was this article helpful?

TOP