This circuit is designed to measure voltage, current, and power using an Arduino UNO, a 16x2 I2C LCD, a voltage sensor, and a current sensor. The measured values are displayed on the LCD and can also be monitored via the Arduino's serial monitor.
16x2 I2C LCD
LED: Two Pin (red)
Resistor
9V Battery
ACS712 Current Sensor 5A 20A 30A
Voltage Sensor DC 25V
Arduino UNO
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// I2C LCD Initialization
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Sensor Pins
const int VOLTAGE_SENSOR = A0;
const int CURRENT_SENSOR = A1;
// Calibration values
const float VOLTAGE_FACTOR = 5.0 / 1024.0; // 5V reference / 1024 steps
const float VOLTAGE_DIVIDER_RATIO = 4.964; // HW-067
const float CURRENT_SENSITIVITY = 1.33985; // Sensitivity for ACS712 5A
const float VOLTAGE_OFFSET = 2.5;
const int SAMPLES = 100; // Number of samples for averaging
const float CURRENT_THRESHOLD = 0.013; // Ignore readings below 13mA
// Function to get average reading from a pin
float getAverageReading(int pin) {
long sum = 0;
for(int i = 0; i < SAMPLES; i++) {
sum += analogRead(pin);
delayMicroseconds(100);
}
return sum / (float)SAMPLES;
}
void setup() {
Serial.begin(9600);
lcd.init();
lcd.backlight();
pinMode(VOLTAGE_SENSOR, INPUT);
pinMode(CURRENT_SENSOR, INPUT);
delay(120);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Guc Olcer Hazir");
delay(2500);
}
void loop() {
// Get readings
float rawVoltage = getAverageReading(VOLTAGE_SENSOR);
float rawCurrent = getAverageReading(CURRENT_SENSOR);
// Calculate voltage
float voltage = (rawVoltage * VOLTAGE_FACTOR * VOLTAGE_DIVIDER_RATIO);
// Calculate current
float voltage_current = (rawCurrent * VOLTAGE_FACTOR);
float current = ((voltage_current - VOLTAGE_OFFSET) / CURRENT_SENSITIVITY);
// Apply current threshold
current = abs(current);
if(current < CURRENT_THRESHOLD) {
current = 0;
}
// Calculate power
float power = voltage * current;
// Convert to mA and mW
float current_mA = current * 1000;
float power_mW = power * 1000;
// Ensure power is zero if current is zero
if(current == 0) {
power_mW = 0;
}
// Display on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("V:");
lcd.print(voltage, 1);
lcd.print("V I:");
lcd.print(current_mA, 1);
lcd.print("mA");
lcd.setCursor(0, 1);
lcd.print("Guc:");
lcd.print(power_mW, 1);
lcd.print("mW");
// Print to Serial Monitor
Serial.println("Olcumler:");
Serial.print("Voltaj: ");
Serial.print(voltage, 3);
Serial.println("V");
Serial.print("Akim: ");
Serial.print(current_mA, 3);
Serial.println("mA");
Serial.print("Guc: ");
Serial.print(power_mW, 3);
Serial.println("mW");
Serial.println();
delay(500);
}
This code initializes the I2C LCD and sets up the voltage and current sensors. It reads the sensor values, calculates the voltage, current, and power, and displays these values on the LCD and the serial monitor.