Table of Contents
Overview:
In this Project let learn how to interface an ACS712 current sensor module with ESP32 microcontroller board to measure the AC current of the load and display the current in Amperes and power consumption in Wattage of it on 16X2 LCD display module.
So, by using this project you can build a power monitor or AC power consumption monitoring system. You can also implement IOT to get the data over the internet or cloud. This ACS712 sensor comes in 3 version, which you can choose according to your need. 5A, 20A and 30A versions of ACS712 are available in the market.
Related Project:Â Measure AC current using ACS712 sensor with Arduino
Required components:
Product Name | Quantity | ||
---|---|---|---|
ESP32 Microcontroller | 1 | https://amzn.to/3s0Sut6 | https://amzn.to/3Kw2np2 |
ACS712 current sensor module | 1 | https://amzn.to/3v4ok8y | https://amzn.to/3EaJ9Dz |
LCD 16X2 module with or without I2C adapter | 1 | https://amzn.to/3BNSRup | https://amzn.to/363ki7F |
5V power supply (Micro USB or External). | 1 | https://amzn.to/3s1a8g3 | https://amzn.to/364yInH |
Few Connecting Wires | https://amzn.to/3H2BV4e | https://amzn.to/3J0WVu2 |
ACS712 current sensor module:
ACS712 IC uses hall effect principle to measure the current which is developed by Allegro Micro Systems. ACS712 IC consists of a low-offset, precise and linear Hall sensor circuit with a copper conduction path at the surface of the die.
ACS712 modules has 3 versions with different sensitivities.
You need to choose the right module according to your requirement, as higher the capacity of the sensor lower the accuracy.
5A Module | 20A Module | 30A Module |
185mV/Amp | 100mV/Amp | 66mV per Amp |
From the above table you can see the higher the measuring capacity the sensitivity of measuring the Milli Volts per Amp decreases. To measure smaller currents using smaller range module gives more accurate results than using high range module.
For more details about the ACS712, Please refer ACS712 datasheet :Â here
ACS712 current sensor module Specifications
- Measures both AC and DC current
- Very small in size
- Available in 5A, 20A and 30A modules
- 66 to 185 mV/A output sensitivity
- Outputs analog voltage, so its easy to connect with most of the Microcontrollers.
For Principle, pinout diagrams and Working of ACS712 module in detail please refer our previous article: ACS712 Principle, working and pinout.
Interfacing ACS712 Current sensor with ESP32 to display result over LCD display.
As you can see from the above circuit diagram the LCD display and ACS 712 current sensor VCC and GND pins are powered through the VIN and GND pins of ESP32. If you are not getting 5volts voltage from the VIN pin of ESP32 connect an external 5V power supply to ACS712 module as it requires exact 5volts for calculations.
The Data pin OUT of ACS712 is connected to Analog pin D34 of ESP32. The I2C communication pins SDA and SCL pins of LCD display are connected to D21 and D22 pins respectively.
If you want to use an LCD display with out I2c refer this article:Â Interfacing 16X2 LCD Module with ESP32 without I2C
After connecting everything its time to upload the program code.
Program code:
Connect the ESP32 microcontroller board to the PC where Arduino IDE is installed through USB cable. Choose the correct port and board as ESP32 Wrover Module from Tools menu and install the required libraries from the Library manager or download from the below links and extract them in Arduino Libraries folder.
Required Libraries:
- Download LiquidCrystal_I2C – Link
Next copy the below code and paste it in IDE workspace and press upload button. That’s it the code will be uploaded.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
// Tested and compiled with no errors // measuring AC current using ACS712 current sensor with ESP32 Microcontroller // The ACS712 works with high voltage AC so be careful ! // source - /www.circuitschools.com #include <LiquidCrystal_I2C.h> const int sensorIn = 34; // pin where the OUT pin from sensor is connected on Arduino int mVperAmp = 185; // this the 5A version of the ACS712 -use 100 for 20A Module and 66 for 30A Module int Watt = 0; double Voltage = 0; double VRMS = 0; double AmpsRMS = 0; // initialize the LCD library with I2C address and LCD size LiquidCrystal_I2C lcd (0x27, 16,2); void setup() { Serial.begin (9600); Serial.println ("ACS712 current sensor"); // Initialize the LCD connected lcd. init (); // Turn on the backlight on LCD. lcd. backlight (); lcd.print ("ACS712 current"); lcd. setCursor (0, 1); lcd.print ("sensor"); delay(1000); lcd.clear(); } void loop() { Serial.println (""); Voltage = getVPP(); VRMS = (Voltage/2.0) *0.707; //root 2 is 0.707 AmpsRMS = ((VRMS * 1000)/mVperAmp)-0.3; //0.3 is the error I got for my sensor Serial.print(AmpsRMS); Serial.print(" Amps RMS --- "); Watt = (AmpsRMS*240/1.2); // note: 1.2 is my own empirically established calibration factor // as the voltage measured at A0 depends on the length of the OUT-to-A0 wire // 240 is the main AC power voltage – this parameter changes locally Serial.print(Watt); Serial.println(" Watts"); lcd. setCursor (0, 0); lcd.print (AmpsRMS); lcd.print (" Amps "); //Here cursor is placed on first position (col: 0) of the second line (row: 1) lcd. setCursor (0, 1); lcd.print (Watt); lcd.print (" watt "); delay (100); } // ***** function calls ****** float getVPP() { float result; int readValue; // value read from the sensor int maxValue = 0; // store max value here int minValue = 4096; // store min value here ESP32 ADC resolution uint32_t start_time = millis(); while((millis()-start_time) < 1000) //sample for 1 Sec { readValue = analogRead(sensorIn); // see if you have a new maxValue if (readValue > maxValue) { /*record the maximum sensor value*/ maxValue = readValue; } if (readValue < minValue) { /*record the minimum sensor value*/ minValue = readValue; } } // Subtract min from max result = ((maxValue - minValue) * 5)/4096.0; //ESP32 ADC resolution 4096 return result; } |
After uploading the code open serial monitor at baud rate 9600 to check how the sensor is working. Serial monitor shows results as shown in the below output screenshot.
Every ACS712 current sensor has some level of error, here I am using 30A version so the sensitivity is low, in the result there are some fluctuations.
Calibrating ACS712 current sensor:
At no load situation open serial monitor and check weither the amp values are near to Zero. If the values are not near to zero, and if the values are positive then substract the error value from the line 36, if the values are negative then add the error value till you reach the near zero level.
Calibration is important to get the nearly accurate results, You can also calibrate with some known current loads and correct the error through the code.
Outputs:
When no load is connected the LCD display outputs as below:
When a small load is connected the LCD display outputs as below image:
If you like this project please subscribe to our YouTube Channel “Circuit Schools“ to encourage us to publish more interesting projects. If you have any doubts write to us from below comment section.