Skip to content

Measure any AC current with ACS712 and Arduino + LCD / OLED

 

PLEASE BE CAREFUL IF YOU’RE USING THE POWER LINE

       Hello, and welcome to another tutorial, this one is about measuring Alternating Current (AC), using ACS712 I’m using the 30Amps version which is ACS712 30A, and our lovely Arduino Uno board, I tried to add an OLED screen but unfortunately ended up breaking it while shooting the tutorial so I switched to the LCD, but below you’ll find the wiring and codes for both versions.

And by “Any AC…” I mean that we gonna see a code/library that works for all signal types not only sinewaves one, and our Ammeter will be able to calculate the TRUE ROOT MEAN SQUARE. And note that the sensor uses the Hall Effect (production of a voltage difference across an electrical conductor, transverse to an electric current in the conductor and to an applied magnetic field perpendicular to the current)

You can combine this project with this one: Easy measure of AC Voltage using Arduino and ZMPT101B

Tests will be done on an incandescent light bulb, controlled by a light dimmer in series with a reference multimeter and the ACS712 current sensor module.

Wiring all

Here’s the plan:

  • First we need to interface our module with the Arduino board and check the signal shape when the dimmer is ON – Full Cycle – Half Cycle.
  • Then we gonna chech a simple code that doesn’t require any library, but it works only with Sinewaves signal (it will be tested).
  • After that we gonna see the code that will measure the TRMS of the AC, and use the LCD.

Parts

Please be carfeul when chosing the ACS712, don’t try to play it safe like I did (by purchasing a 30Amps version), this will just give big fluctuation when you try to use it for a domestic use or small amps applications, so here for me a 5A or 10A version would have been fine.
Also note that 30A or 20A … will affect your calibrations too, but it’s explained how to calibrate in video and in codes comments.

Those are the parts I used, you can replace the UNO with any compatible board, or that LCD i²c with an OLED or any display you prefer.

Test 1

Wiring

 

test 1_bblolz.png
Direct test

Code

/*This code works with ACS712 Current sensor, it permits to read the raw data
  It's better to use it with Serial Plotter
  More details on www.surtrtech.com
*/

#define Current_sensor A0  //The sensor analog input pin

float i;


void setup() {

Serial.begin(9600);
pinMode(Current_sensor, INPUT);

}

void loop() {
  i = analogRead(Current_sensor);
  Serial.println(i);
  delay(100);                     //Modifying or removing the delay will change the way the signal is shown 
                                  //set it until you get the correct sinewave shap

}

 

 

Results

Upload the code and launch the serial plotter, you should be able to see a sinewave like signal, the red line shows when I turned down the dimmer and the current started having that shape that you usually find in a triac.

Test_1
Direct wiring and show of the serial plotter

Test 2

Now if you want to measure the current using the code above only, you’ll get values around 512 – 511, they don’t reflect the RMS value at all. So to measure the signal RMS value we gonna keep the same wiring as above but now we need another code that can calculate the RMS.

Code

Here you can download the second code from Drive: Download code .ino here

(I can’t write all the code here, because it won’t display well, and will give you errors if you try to copy and compile it).

Results

Test_2

As you can see the values on the left (RMS) are the same as the value measured by the multimeter, on the right those are Peak to Peak values, and note that the dimmer is at full cycle as I’m using 75W bulb and 230VAC/50Hz.

!!!!!!!!!!!!!!!!     BUT   !!!!!!!!!!!!!!!!!!!!!

Test_3

Here I turned all the way down my light dimmer, which means I’m approximately around 1/4 of a cycle, the multimeter is giving me 0.21A but my code gives me 0.36A, the code still measures from Peak to Peak, but as you can see above my signal is not linear when I turn the dimmer all the way down.

And that’s the difference between a TRMS measuring and a measuring that works only with sinewaves signal.

Test 3

Now you see the problem of the code above, but fortunately there’s a library to the rescue, I really appreciate that library for all the work it does because as you know if you want to calculate a RMS value you need to use Integral Calculus, and that’s way difficult to achieve on an electronic development board, you can check the library cpp file to see what methods are used like the “Average” “Sigma”… Things related to statistics/probabilities ….

Library

Here you can download the Filters library: Download the library here.

Wiring

For the wiring we can keep everything the same as above.

Code

Here you can download the Code I used: Download the code here. The code contains a lot of comment to make it easy for you.

Results

The correct results are on the middle, the left ones are without calibration, and the right ones are used to demonstrate how to calibrate (check the video)

Test_4_1
At full cycle
Test_4_2
At 1/4 of a cylce the code doesn’t have any problem to measure non linear signals

As you saw the huge difference between the two codes response to a non linear signal but the second one act as a TRMS multimeter.

Test 4

Here we keep the same things all we do is instead of using the serial monitor we can use LCD or OLED

Wirings

 

test lcd_bb
Using the LCD

 

test oled_bb
Using the OLED

Codes

Here you can download the codes :

Libraries

OLED i²c display Adafruit libraries: – https://github.com/adafruit/Adafruit_SSD1306

https://github.com/adafruit/Adafruit-GFX-Library

LCD i²c library you can Download it here.

Result

Test_5

This is the only test I could do because I broke my OLED screen while trying to shoot the video tutorial 😦

Overall this works the same way as we saw before it just displays it on a LCD i²c screen.

That’s all folks, I hope you like this tutorial don’t forget to like the video tutorial and subscribe to surtrtech channel to show support, thank you very much.

Yassine View All

Automation and Electrical Engineer, Electronics amateur trying to share my little projects.

17 thoughts on “Measure any AC current with ACS712 and Arduino + LCD / OLED Leave a comment

  1. Like you said in this video , i need to combine this code with your voltmeter code and make a watt meter (no need to consider power factor ) , I tried … , Successfully uploaded it to my arduino but the values i get is just too weird . I don’t know what I did wrong , i did change slope to Vslope for voltage sensor and Cslope for current sensor (intersept too). In short i am unable to combine these two programs into a working watt meter program , please help me as soon as possible

    Like

    • Hello, you should put two separate slope/intercept for each element to measure, also put two separate “RunningStatistics” elements with their own parameters and functions… you can do the same thing copy/paste and change the name, first measure the Voltage for example and store it, then call the second function to measure Current… you can obtain the (phi) angle by measuring the duration between the current and the voltage.

      I’ve never did this before, you can try, just be careful.

      Like

      • i changed input stats to vinputStats and cinputStats for voltage and current respectively. now the program is working BUT, the while loop for voltage measurement comes inside the while loop for current measurement. so the program is revolving around the second while loop and not exiting to the main while loop.
        in short, please tell me how to write the while loops for current and voltage measurement inside void loop.
        this is the problem :

        void loop() {
        RunningStatistics CinputStats; // create statistics to look at the raw test signal
        CinputStats.setWindowSecs( windowLength ); //Set the window length

        while( true ) {
        ACS_Value = analogRead(ACS_Pin); // read the analog in value:
        CinputStats.input(ACS_Value); // log to Stats function

        if((unsigned long)(millis() – previousMillis) >= printPeriod) { //every second we do the calculation
        previousMillis = millis(); // update time

        Amps_TRMS = Cintercept + Cslope * CinputStats.sigma();

        Serial.println( ” Amps: ” );
        Serial.println( Amps_TRMS );
        Serial.println( ” ” );

        lcd.setCursor(2,0);
        lcd.print(Amps_TRMS);
        lcd.setCursor(9,0);
        lcd.print(“A”);

        }
        // }

        /* This code works with ZMPT101B AC voltage sensor module and 128×32 OLED display

        */

        RunningStatistics VinputStats; //Easy life lines, actual calculation of the RMS requires a load of coding
        VinputStats.setWindowSecs( windowLength );

        while( true ) {
        Sensor = analogRead(A0); // read the analog in value:
        VinputStats.input(Sensor); // log to Stats function

        if((unsigned long)(millis() – previousMillis) >= printPeriod) {
        previousMillis = millis(); // update time every second

        Serial.print( “\n” );

        current_Volts = Vintercept + Vslope * VinputStats.sigma(); //Calibartions for offset and amplitude
        current_Volts= current_Volts*(40.3231); //Further calibrations for the amplitude

        Serial.print( “\tVoltage: ” );
        Serial.print( current_Volts ); //Calculation and Value display is done the rest is if you’re using an OLED display

        lcd.setCursor(5,1);
        lcd.print(current_Volts);
        lcd.setCursor(11,1);
        lcd.print(“V”);
        }

        }

        }
        }

        Like

      • It’s not exiting the first loop because of the while loop… please learn about loops in the Arduino references first.

        As I told you, you better put each measuring in a function that the code will execute separately each time… maybe I used an example of the funtion with the ESP8266 and ZMPT101B, you can check the other article because this one is meant for measuring one current signal only.

        Like

  2. Hello, thank you very much for this project. I need help with the filter library. Download the Filters library, but it shows an error when compiling. The type_traits library is missing, but I only find ArxTypeTraits and I can’t get it to work either.
    In Helper.h I have changed #include to #include
    And it keeps showing errors when compiling:
    error: ‘RunningStatistics’ was not declared in this scope
    error: ‘inputStats’ was not declared in this scope
    Where do I find type_traits that works with this project?
    Thank you
    Diego.

    Like

  3. Is there a way around the while(true) loop?
    I’m using the code on a nodemcu and sending the data to firebase, if I include the firebase.setfloat command in the while loop wrong values are recorded.

    Like

  4. Good evening, I’m using your code I set slope and intercept to 0 and my output was 0, how do I get my intercept
    I know it’s output/ expected but my output is 0

    Like

    • Hello, if you set the slope to 0 you’ll always get 0 (+intercept) as output, at first the intercept should be 0 and the slope at 1 to get the raw value then you can adjust as shown in the tutorial.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: