Monitoring Current with ESP32 and ACS712 Sensor

Current Sensor with ESP32 and Blink

In this article, we will explore how to use a current sensor with an ESP32 microcontroller and the Blink platform. We will cover the materials needed, setup, and coding required to get started.

Materials Needed

  • ACS712 current sensor
  • ESP32 microcontroller
  • Socket and plug for load connection
  • Jumper wires

Setup

To set up the current sensor with the ESP32, follow these steps:
  1. Place the ESP32 on a carrier board.
  2. Connect three jumper wires to the current sensor:
    • VCC of the current sensor to 3.3V pin of ESP32
    • Ground of the current sensor to ground of ESP32
    • Output pin of the current sensor to GPIO34 pin of ESP32

Connecting the Load

Connect the load to the current sensor. In this example, we will use a soldering iron as the load.

Setting up Blink

To set up Blink, follow these steps:
  1. Create a template in the Blink dashboard.
  2. Create a virtual pin data stream.
  3. Create a web dashboard.

Creating a Device

Create a device using the template created in the previous step. This will generate a Blink template ID, template name, and authentication token.

Setting up the Mobile Dashboard

Set up the mobile dashboard in the Blink application using the template ID, template name, and authentication token generated in the previous step.

Coding

The code for this project can be found on GitHub. To use it, follow these steps:
  1. Copy the code from the GitHub repository.
  2. Open Arduino IDE and paste the code.

Code Explanation

The code includes several define statements that set up constants for the Blink template ID, name, and authentication token. It also includes libraries for the ACS712 current sensor and the WiFi connection.
const char* BLINK_TEMPLATE_ID = "your_template_id";
const char* BLINK_TEMPLATE_NAME = "your_template_name";
const char* BLINK_AUTH_TOKEN = "your_auth_token";

#include <ACS712.h>
#include <WiFi.h>

// Initialize ACS712 object
ACS712 sensor(34, 3.3, 4095, 125);

void setup() {
  // Connect to WiFi
  WiFi.begin(ssid, password);
}

void loop() {
  // Read current value from sensor
  int currentValue = readCurrent();

  // Send current value to Blink virtual pin V0
  blink.send(V0, currentValue);
}

Calibration

To calibrate the current sensor, follow these steps:
  1. Upload the code to the ESP32.
  2. Open the serial monitor and read the no-load current value.
  3. Assign this value to the calibration factor in the code.
  4. Re-upload the code.

Testing

To test the current sensor, follow these steps:
  1. Connect a load to the current sensor.
  2. Read the current value from the sensor using the code.
  3. Verify that the current value is correct.


Current Sensor A current sensor is an electrical device that converts current flowing through it into a measurable signal, such as voltage or current.
Background The need for current sensing arose with the development of electric power systems. As electrical systems became more complex and widespread, the ability to measure current flow accurately became essential for safe and efficient operation.
Types of Current Sensors There are several types of current sensors available, including:
  Hall Effect Sensors Use a magnetic field to measure current.
  Shunt Resistors Measure the voltage drop across a known resistance.
  Current Transformers Use a magnetic field to measure current, commonly used for high-current applications.
Applications Current sensors are widely used in various fields, including:
  Power Systems Monitor and control current flow in power grids.
  Industrial Automation Measure current consumption of motors, pumps, and other equipment.
  Electrical Vehicles Monitor battery charging and discharging currents.
Advantages Current sensors offer several advantages, including:
  High Accuracy Provide accurate current measurements.
  Wide Range Can measure a wide range of currents, from milliamps to kiloamperes.
  Low Power Consumption Consume minimal power, reducing energy losses.


Introduction

The ESP32 is a powerful microcontroller that can be used for a wide range of applications, including IoT projects. One of the most popular sensors used with the ESP32 is the ACS712 current sensor. In this article, we will explore how to use the ESP32 and ACS712 sensor to monitor current.

What is the ACS712 Sensor?

The ACS712 is a Hall Effect-based linear current sensor that can measure DC currents up to 30A. It has a high accuracy and a low noise output, making it suitable for precise current measurements.

Hardware Requirements

  • ESP32 development board (e.g. ESP32 DevKitC)
  • ACS712 current sensor module
  • Breadboard and jumper wires
  • DC power source (e.g. battery or wall adapter)

Circuit Connection

Connect the ACS712 sensor module to the ESP32 development board as follows:

  • VCC pin of the ACS712 to the VIN pin of the ESP32
  • GND pin of the ACS712 to the GND pin of the ESP32
  • OUT pin of the ACS712 to any analog input pin of the ESP32 (e.g. A0)

Software Requirements

You will need to install the following libraries:

  • ESP32 Arduino Core (if you are using the Arduino IDE)
  • ACS712 library (available on GitHub)

Code Example

Here is an example code that reads the current value from the ACS712 sensor and prints it to the serial monitor:

#include <ACS712.h>

const int ACS712_PIN = A0;
ACS712 acs(ACS712_PIN);

void setup() {
  Serial.begin(115200);
}

void loop() {
  float current = acs.getCurrent();
  Serial.print("Current: ");
  Serial.println(current, 2);
  delay(1000);
}

How it Works

The ACS712 sensor measures the current flowing through a conductor and outputs an analog voltage proportional to the measured current. The ESP32 reads this voltage using its built-in ADC (Analog-to-Digital Converter) and converts it into a digital value. This digital value is then used to calculate the actual current.

Conclusion

In this article, we have shown how to use the ESP32 and ACS712 sensor to monitor current. This is a simple yet powerful project that can be used in various applications such as power monitoring, robotics, and IoT projects.


Q1: What is the purpose of using an ACS712 sensor with ESP32? The ACS712 sensor is used to monitor current, and when paired with ESP32, it allows for accurate measurement and monitoring of current consumption in a circuit.
Q2: How does the ACS712 sensor work? The ACS712 sensor is a Hall Effect-based linear current sensor that measures current by detecting the magnetic field generated by the current flowing through a conductor.
Q3: What are the advantages of using an ACS712 sensor with ESP32? The combination offers high accuracy, low power consumption, and ease of use, making it suitable for IoT applications such as energy monitoring and automation.
Q4: How do I connect the ACS712 sensor to ESP32? The ACS712 sensor can be connected to ESP32 using a breadboard or PCB, ensuring proper connections between VCC, GND, and OUT pins of the sensor and corresponding GPIO pins on the ESP32 board.
Q5: What is the output of the ACS712 sensor? The ACS712 sensor outputs an analog voltage proportional to the measured current, which can be read by the ESP32's ADC (Analog-to-Digital Converter) pins.
Q6: How do I calibrate the ACS712 sensor? Calibration involves applying a known current to the sensor and measuring the output voltage, then adjusting the sensitivity of the sensor using a calibration constant in the code.
Q7: Can I use the ACS712 sensor for high-current applications? The ACS712 sensor is suitable for measuring currents up to 5A, but it's not recommended for high-current applications. For higher current measurements, consider using a different sensor or a current transformer.
Q8: How do I read the output of the ACS712 sensor in ESP32? The output voltage from the ACS712 sensor can be read by the ESP32's ADC pins, and then converted to a digital value using the `analogRead()` function.
Q9: Can I use multiple ACS712 sensors with a single ESP32? Yes, multiple ACS712 sensors can be connected to a single ESP32 board, allowing for monitoring of multiple currents simultaneously.
Q10: What are some potential applications of using an ACS712 sensor with ESP32? Potential applications include energy monitoring systems, home automation, IoT-based current measurement, and industrial control systems.




Rank Pioneers/Companies Description
1 Adafruit Known for their ESP32 and ACS712 sensor tutorials, Adafruit is a leading provider of DIY electronics.
2 SparkFun SparkFun offers a range of ESP32 and ACS712 sensor boards, as well as tutorials and guides for monitoring current.
3 Espressif Systems As the manufacturer of the ESP32 chip, Espressif provides extensive documentation and resources for monitoring current with ACS712 sensor.
4 Allegro MicroSystems As the manufacturer of the ACS712 sensor, Allegro provides detailed datasheets and application notes for current monitoring.
5 Digi-Key Electronics Digi-Key offers a wide range of ESP32 and ACS712 sensor boards, as well as tools and resources for monitoring current.
6 Seeed Studio Seeed Studio provides a range of ESP32 and ACS712 sensor boards, as well as tutorials and guides for monitoring current.
7 DFRobot DFRobot offers a range of ESP32 and ACS712 sensor boards, as well as tutorials and guides for monitoring current.
8 Gravitech Gravitech provides a range of ESP32 and ACS712 sensor boards, as well as tutorials and guides for monitoring current.
9 ElecFreaks ElecFreaks offers a range of ESP32 and ACS712 sensor boards, as well as tutorials and guides for monitoring current.
10 IoT Playground IoT Playground provides a range of ESP32 and ACS712 sensor boards, as well as tutorials and guides for monitoring current.




Component Description Technical Details
ESP32 Microcontroller Used for reading sensor data and sending it to a server or displaying on an LCD.
  • Microcontroller: Tensilica Xtensa LX6
  • Clock Speed: Up to 240 MHz
  • Memory: 520 KB SRAM, 4 MB Flash
  • Communication Protocols: Wi-Fi, Bluetooth, UART, SPI, I2C
ACS712 Current Sensor Used for measuring current in a circuit.
  • Sensing Range: ±5A, ±20A, or ±30A depending on the model
  • Sensitivity: 185 mV/A (±5A), 100 mV/A (±20A), or 66 mV/A (±30A)
  • Accuracy: ±1.5% at room temperature
  • Operating Temperature: -40°C to +85°C
Analog-to-Digital Converter (ADC) Used for converting the analog output of the ACS712 sensor to a digital signal.
  • Resolution: 12-bit (4096 steps)
  • Sampling Rate: Up to 200 ksps
  • Input Range: 0-3.3V
Circuit Diagram The circuit consists of the ESP32 microcontroller, ACS712 current sensor, and an LCD display. Circuit Diagram
Code The code reads the analog output of the ACS712 sensor using the ADC, converts it to a current value, and sends it to a server or displays on an LCD.
const int ACS712_PIN = 34; // Pin connected to the ACS712 sensor

void setup() {
  Serial.begin(115200);
  pinMode(ACS712_PIN, INPUT);
}

void loop() {
  int adcValue = analogRead(ACS712_PIN);
  float current = mapFloat(adcValue, 0, 4095, -30, 30); // Map the ADC value to a current value
  Serial.println(current);
  delay(1000);
}