Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: VFD Simulation
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2025-08-27 20:58:26
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* simulation a vfd of a 3ph asynchronouus motor by */
- /* PROTEUS Arduino mega 2560 => 3 IR2112 => INVERTER */
- /* => MOTOR Pins 2 to 7 UVW high and low A0 = Pot */
- /* Pin 8 = DIRECTION BUTTON */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- // This project simulates a 3-phase VFD control using 3 IR2112 drivers connected to an Arduino Mega 2560.
- // Outputs U_H, V_H, W_H drive the high-side MOSFETs; U_L, V_L, W_L drive the corresponding low-side MOSFETs.
- // Potentiometer at A0 provides speed reference; Button at digital pin 8 changes rotation direction.
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** PREPROCESSOR CONSTANTS & GLOBAL VARIABLES *****/
- const int U_H = 2; // Phase U high-side PWM output
- const int V_H = 3; // Phase V high-side PWM output
- const int W_H = 4; // Phase W high-side PWM output
- const int U_L = 5; // Phase U low-side PWM output
- const int V_L = 6; // Phase V low-side PWM output
- const int W_L = 7; // Phase W low-side PWM output
- const int POT_PIN = A0; // Speed reference potentiometer input
- const int DIR_PIN = 8; // Direction control button (with internal pull-up)
- const unsigned long DEBOUNCE_MS = 200; // Debounce time for direction toggle
- float angleRad = 0.0f; // Electrical angle for PWM modulation
- const float TWO_PI = 6.28318530718f;
- bool directionForward = true; // Rotation direction
- unsigned long lastBtnTime = 0; // Debounce timer for button
- int lastBtnState = HIGH; // Previous button state
- unsigned long lastUpdateMs = 0; // Timing for integration step
- /****** SETUP: initialize pins & states *****/
- void setup(void)
- {
- // Initialize PWM outputs for all six transistors as outputs
- pinMode(U_H, OUTPUT);
- pinMode(V_H, OUTPUT);
- pinMode(W_H, OUTPUT);
- pinMode(U_L, OUTPUT);
- pinMode(V_L, OUTPUT);
- pinMode(W_L, OUTPUT);
- // Direction button with internal pull-up
- pinMode(DIR_PIN, INPUT_PULLUP);
- // Ensure all outputs start in a safe OFF state
- analogWrite(U_H, 0);
- analogWrite(V_H, 0);
- analogWrite(W_H, 0);
- analogWrite(U_L, 0);
- analogWrite(V_L, 0);
- analogWrite(W_L, 0);
- // Initialize timing
- lastUpdateMs = millis();
- lastBtnTime = millis();
- }
- /****** MAIN LOOP: update PWM based on pot and direction *****/
- void loop(void)
- {
- // Read direction button (active low due to pull-up)
- int btnState = digitalRead(DIR_PIN);
- unsigned long now = millis();
- // Debounce: toggle direction on rising edge of a press
- if (btnState == LOW && lastBtnState == HIGH) {
- if ((now - lastBtnTime) >= DEBOUNCE_MS) {
- directionForward = !directionForward;
- lastBtnTime = now;
- }
- }
- lastBtnState = btnState;
- // Speed reference from potentiometer (A0)
- int potVal = analogRead(POT_PIN); // 0 - 1023
- float potNorm = (float)potVal / 1023.0f; // 0.0 - 1.0
- // Map pot to an angular velocity (0 - ~2.0 rad/s). This is arbitrary for simulation.
- float omega = potNorm * 2.0f; // rad/s
- // Time delta (seconds)
- unsigned long currentMs = millis();
- float dt = (currentMs - lastUpdateMs) / 1000.0f;
- if (dt <= 0.0f) dt = 0.001f; // guard against division by zero / no time passage
- lastUpdateMs = currentMs;
- // Update electrical angle according to direction
- if (directionForward) angleRad += omega * dt;
- else angleRad -= omega * dt;
- // Keep angle bounded
- if (angleRad > TWO_PI) angleRad -= TWO_PI;
- if (angleRad < 0.0f) angleRad += TWO_PI;
- // Compute phase values for a 3-phase system (ABC) with 120-degree phase shifts
- float phaseA = sin(angleRad); // A
- float phaseB = sin(angleRad - (2.0f * 3.14159265359f / 3.0f)); // B (-120 deg)
- float phaseC = sin(angleRad - (4.0f * 3.14159265359f / 3.0f)); // C (-240 deg)
- // Convert phases (-1..1) to PWM for each leg: positive -> high-side PWM; negative -> low-side PWM
- int dutyA_high = (phaseA >= 0.0f) ? (int)(phaseA * 255.0f) : 0;
- int dutyA_low = (phaseA < 0.0f) ? (int)((-phaseA) * 255.0f) : 0;
- int dutyB_high = (phaseB >= 0.0f) ? (int)(phaseB * 255.0f) : 0;
- int dutyB_low = (phaseB < 0.0f) ? (int)((-phaseB) * 255.0f) : 0;
- int dutyC_high = (phaseC >= 0.0f) ? (int)(phaseC * 255.0f) : 0;
- int dutyC_low = (phaseC < 0.0f) ? (int)((-phaseC) * 255.0f) : 0;
- // Write PWM values to outputs
- analogWrite(U_H, constrain(dutyA_high, 0, 255));
- analogWrite(U_L, constrain(dutyA_low, 0, 255));
- analogWrite(V_H, constrain(dutyB_high, 0, 255));
- analogWrite(V_L, constrain(dutyB_low, 0, 255));
- analogWrite(W_H, constrain(dutyC_high, 0, 255));
- analogWrite(W_L, constrain(dutyC_low, 0, 255));
- // Loop pacing: base update happens continuously; keep CPU busy-lightly.
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment