Example Code for Arduino-Display AC Current on LCD

This example demonstrates how to read the Analog AC Current Sensor and display the measured AC current on the LCD.

Hardware Preparation

Arduino UNO or simliar x1
AC Current Signal Conversion Module x1
Open Type AC Transformer Probe x1
LCD Keypad Shield For Arduino 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_LCD.
 * @n This example reads Analog AC Current Sensor and display on the LCD.

 * @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]>
*/

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);        // select the pins used on the LCD panel

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);
  lcd.begin(16, 2);                       // start the library
  pinMode(13, OUTPUT);
}

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

Result

The LCD displays "AC CURRENT" on the first line and the measured current value (in amps) on the second line. The Arduino’s built-in LED (pin 13) blinks once per second (it toggles every 500 ms).

Was this article helpful?

TOP