#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <math.h> // Include math library for sin function
LiquidCrystal_I2C lcd(0x27, 20, 4); // Adjust address based on your I2C address
const int potPin = A0; // Potentiometer pin
// Define pins for PWM output to control the inverter
const int pwmPinA = 9; // Phase A
const int pwmPinB = 10; // Phase B
const int pwmPinC = 11; // Phase C
const int motorPoles = 4; // Number of poles of the motor
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Initialize the LCD
lcd.init();
lcd.backlight();
// Set PWM pins as outputs
pinMode(pwmPinA, OUTPUT);
pinMode(pwmPinB, OUTPUT);
pinMode(pwmPinC, OUTPUT);
// Display initial message
lcd.setCursor(0, 0);
lcd.print("3-Phase VFD System");
lcd.setCursor(0, 1);
lcd.print("Freq: ");
lcd.setCursor(0, 2);
lcd.print("RPM: ");
}
void loop() {
// Read speed control input from potentiometer
int potValue = analogRead(potPin);
// Map potentiometer value to frequency range
float frequency = map(potValue, 0, 1023, 0, 50); // Example: 0 to 50 Hz
// Calculate RPM based on frequency
float rpm = (120.0 * frequency) / motorPoles;
// Generate PWM signals for 3-phase output
generate3PhasePWM(frequency);
// Display frequency and RPM on LCD
lcd.setCursor(6, 1);
lcd.print(frequency);
lcd.print(" Hz "); // Clear any extra characters
lcd.setCursor(5, 2);
lcd.print(rpm);
lcd.print(" "); // Clear any extra characters
// Print frequency and RPM for debugging
Serial.print("Frequency: ");
Serial.print(frequency);
Serial.print(" Hz, RPM: ");
Serial.println(rpm);
delay(100);
}
void generate3PhasePWM(float frequency) {
if (frequency == 0) {
// If frequency is zero, set PWM outputs to zero
analogWrite(pwmPinA, 0);
analogWrite(pwmPinB, 0);
analogWrite(pwmPinC, 0);
} else {
// Calculate the time period of one cycle
float period = 1000000.0 / frequency; // Period in microseconds
// Generate 3-phase signals with 120-degree phase shift
unsigned long time = micros() % (unsigned long)period;
int pwmA = 128 + 127 * sin(2 * PI * time / period);
int pwmB = 128 + 127 * sin(2 * PI * time / period + 2 * PI / 3);
int pwmC = 128 + 127 * sin(2 * PI * time / period - 2 * PI / 3);
analogWrite(pwmPinA, pwmA);
analogWrite(pwmPinB, pwmB);
analogWrite(pwmPinC, pwmC);
}
}