Measure CURRENT WITH Esp 32 AND CURRENT SENSOR

 

Title: Measuring AC Current Using a 30A Current Sensor with ESP32 and OLED Display

Introduction: In this blog, I'll guide you through a simple project to measure AC current using a 30A current sensor, the ESP32 microcontroller, and an OLED display. This setup is perfect for monitoring power usage, allowing you to keep track of current consumption in your home appliances or other electronics.

Let’s dive into the components, circuit setup, code, and step-by-step instructions for creating this handy current measurement system.











Components Needed:

  1. ESP32 Microcontroller: A powerful, low-cost microcontroller with WiFi and Bluetooth capabilities.
  2. 30A AC Current Sensor: Designed to measure AC current up to 30 amps. For this project, I’m using a non-invasive current sensor.
  3. 0.96" OLED Display (I2C): A compact display for real-time current reading.
  4. Jumper Wires: For connecting components.

Circuit Connections:

ComponentESP32 Pin
      Current Sensor      34
OLED Display SDA21
OLED Display SCL22
Power (VCC/GND)3.3V/GND

Step-by-Step Circuit Setup:

  1. Connect the Current Sensor: Attach the current sensor’s output to GPIO 34 on the ESP32. This analog pin will read the current signal.

  2. Connect the OLED Display: Use the I2C protocol to connect the OLED display.

    • SDA pin goes to GPIO 21
    • SCL pin goes to GPIO 22
    • VCC and GND pins on the display go to the 3.3V and GND pins on the ESP32

CODE FOR THIS BELOW 


The code below reads the current values from the current sensor, processes the readings, and displays the measured current on the OLED.



#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// OLED display width and height
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

// OLED reset pin
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define CURRENT_SENSOR_PIN 34  // Pin for current sensor

void setup() {
  Serial.begin(115200);
  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("OLED initialization failed"));
    for (;;);
  }
  
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);
  
  // Display a startup message
  display.setCursor(0, 0);
  display.print("AC Current Meter");
  display.display();
  delay(2000);
}

void loop() {
  int sensorValue = analogRead(CURRENT_SENSOR_PIN);  // Read raw sensor value
  float voltage = sensorValue * (3.3 / 4095.0);      // Convert to voltage (ESP32 ADC is 12-bit)
  float current = (voltage - 1.65) / 0.066;          // Convert voltage to current

  // Display the current reading on the OLED
  display.clearDisplay();
  display.setCursor(0, 0);
  display.print("Current (A): ");
  display.setCursor(0, 16);
  display.print(current, 2);  // Display with 2 decimal places
  display.display();

  Serial.print("Current: ");
  Serial.print(current);
  Serial.println(" A");

  delay(1000);  // Update every second
}


 

Code Explanation

  1. Libraries: The Adafruit_GFX and Adafruit_SSD1306 libraries are essential for the OLED display. Make sure to install them in the Arduino IDE.

  2. Setup: In the setup() function, we initialize the Serial monitor and OLED. A startup message appears on the OLED display for a brief moment.

  3. Reading and Converting the Current:

    • The analogRead function reads the raw sensor data on pin 34.
    • This reading is converted to voltage based on the ESP32’s 3.3V reference.
    • We calculate the current from the voltage reading, subtracting the offset and dividing by the sensitivity of the current sensor (typically around 0.066V/A for a 30A sensor).
  4. Display and Serial Output: The calculated current is displayed on the OLED screen and printed to the Serial monitor.


Project Tips:

  • Calibrate: Test the sensor by measuring a known load to check the accuracy of your readings. Adjust the sensitivity value if necessary.
  • Safety First: When dealing with AC current, always prioritize safety. Ensure all connections are secure and keep your hands away from any live AC parts.
  • Power Supply: For stable operation, power the ESP32 using an external USB power source or a dedicated power adapter.




Comments

Popular posts from this blog

Indian Railway Recruitment 2024, 10th Pass, 1646 Vacancies, Apply Online

Real Madrid vs atlético Madrid

Automatic Cloth Protector From Rain Project