pleasedontcode

VFD Simulation rev_02

Aug 27th, 2025
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: VFD Simulation
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-08-27 20:58:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* simulation a vfd of a 3ph asynchronouus motor by */
  21.     /* PROTEUS  Arduino mega 2560 => 3 IR2112 => INVERTER */
  22.     /* => MOTOR  Pins 2 to 7 UVW high and low   A0 = Pot */
  23.     /* Pin 8 = DIRECTION BUTTON */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. // This project simulates a 3-phase VFD control using 3 IR2112 drivers connected to an Arduino Mega 2560.
  31. // Outputs U_H, V_H, W_H drive the high-side MOSFETs; U_L, V_L, W_L drive the corresponding low-side MOSFETs.
  32. // Potentiometer at A0 provides speed reference; Button at digital pin 8 changes rotation direction.
  33.  
  34. /****** FUNCTION PROTOTYPES *****/
  35. void setup(void);
  36. void loop(void);
  37.  
  38. /****** PREPROCESSOR CONSTANTS & GLOBAL VARIABLES *****/
  39. const int U_H = 2; // Phase U high-side PWM output
  40. const int V_H = 3; // Phase V high-side PWM output
  41. const int W_H = 4; // Phase W high-side PWM output
  42.  
  43. const int U_L = 5; // Phase U low-side PWM output
  44. const int V_L = 6; // Phase V low-side PWM output
  45. const int W_L = 7; // Phase W low-side PWM output
  46.  
  47. const int POT_PIN = A0;     // Speed reference potentiometer input
  48. const int DIR_PIN = 8;      // Direction control button (with internal pull-up)
  49.  
  50. const unsigned long DEBOUNCE_MS = 200; // Debounce time for direction toggle
  51.  
  52. float angleRad = 0.0f;            // Electrical angle for PWM modulation
  53. const float TWO_PI = 6.28318530718f;
  54.  
  55. bool directionForward = true;       // Rotation direction
  56. unsigned long lastBtnTime = 0;      // Debounce timer for button
  57. int lastBtnState = HIGH;              // Previous button state
  58. unsigned long lastUpdateMs = 0;       // Timing for integration step
  59.  
  60. /****** SETUP: initialize pins & states *****/
  61. void setup(void)
  62. {
  63.     // Initialize PWM outputs for all six transistors as outputs
  64.     pinMode(U_H, OUTPUT);
  65.     pinMode(V_H, OUTPUT);
  66.     pinMode(W_H, OUTPUT);
  67.     pinMode(U_L, OUTPUT);
  68.     pinMode(V_L, OUTPUT);
  69.     pinMode(W_L, OUTPUT);
  70.  
  71.     // Direction button with internal pull-up
  72.     pinMode(DIR_PIN, INPUT_PULLUP);
  73.  
  74.     // Ensure all outputs start in a safe OFF state
  75.     analogWrite(U_H, 0);
  76.     analogWrite(V_H, 0);
  77.     analogWrite(W_H, 0);
  78.     analogWrite(U_L, 0);
  79.     analogWrite(V_L, 0);
  80.     analogWrite(W_L, 0);
  81.  
  82.     // Initialize timing
  83.     lastUpdateMs = millis();
  84.     lastBtnTime = millis();
  85. }
  86.  
  87. /****** MAIN LOOP: update PWM based on pot and direction *****/
  88. void loop(void)
  89. {
  90.     // Read direction button (active low due to pull-up)
  91.     int btnState = digitalRead(DIR_PIN);
  92.     unsigned long now = millis();
  93.  
  94.     // Debounce: toggle direction on rising edge of a press
  95.     if (btnState == LOW && lastBtnState == HIGH) {
  96.         if ((now - lastBtnTime) >= DEBOUNCE_MS) {
  97.             directionForward = !directionForward;
  98.             lastBtnTime = now;
  99.         }
  100.     }
  101.     lastBtnState = btnState;
  102.  
  103.     // Speed reference from potentiometer (A0)
  104.     int potVal = analogRead(POT_PIN); // 0 - 1023
  105.     float potNorm = (float)potVal / 1023.0f; // 0.0 - 1.0
  106.  
  107.     // Map pot to an angular velocity (0 - ~2.0 rad/s). This is arbitrary for simulation.
  108.     float omega = potNorm * 2.0f; // rad/s
  109.  
  110.     // Time delta (seconds)
  111.     unsigned long currentMs = millis();
  112.     float dt = (currentMs - lastUpdateMs) / 1000.0f;
  113.     if (dt <= 0.0f) dt = 0.001f; // guard against division by zero / no time passage
  114.     lastUpdateMs = currentMs;
  115.  
  116.     // Update electrical angle according to direction
  117.     if (directionForward) angleRad += omega * dt;
  118.     else                  angleRad -= omega * dt;
  119.  
  120.     // Keep angle bounded
  121.     if (angleRad > TWO_PI) angleRad -= TWO_PI;
  122.     if (angleRad < 0.0f)   angleRad += TWO_PI;
  123.  
  124.     // Compute phase values for a 3-phase system (ABC) with 120-degree phase shifts
  125.     float phaseA = sin(angleRad);                                  // A
  126.     float phaseB = sin(angleRad - (2.0f * 3.14159265359f / 3.0f)); // B (-120 deg)
  127.     float phaseC = sin(angleRad - (4.0f * 3.14159265359f / 3.0f)); // C (-240 deg)
  128.  
  129.     // Convert phases (-1..1) to PWM for each leg: positive -> high-side PWM; negative -> low-side PWM
  130.     int dutyA_high = (phaseA >= 0.0f) ? (int)(phaseA * 255.0f) : 0;
  131.     int dutyA_low  = (phaseA < 0.0f)  ? (int)((-phaseA) * 255.0f) : 0;
  132.  
  133.     int dutyB_high = (phaseB >= 0.0f) ? (int)(phaseB * 255.0f) : 0;
  134.     int dutyB_low  = (phaseB < 0.0f)  ? (int)((-phaseB) * 255.0f) : 0;
  135.  
  136.     int dutyC_high = (phaseC >= 0.0f) ? (int)(phaseC * 255.0f) : 0;
  137.     int dutyC_low  = (phaseC < 0.0f)  ? (int)((-phaseC) * 255.0f) : 0;
  138.  
  139.     // Write PWM values to outputs
  140.     analogWrite(U_H,   constrain(dutyA_high, 0, 255));
  141.     analogWrite(U_L,   constrain(dutyA_low,  0, 255));
  142.  
  143.     analogWrite(V_H,   constrain(dutyB_high, 0, 255));
  144.     analogWrite(V_L,   constrain(dutyB_low,  0, 255));
  145.  
  146.     analogWrite(W_H,   constrain(dutyC_high, 0, 255));
  147.     analogWrite(W_L,   constrain(dutyC_low,  0, 255));
  148.  
  149.     // Loop pacing: base update happens continuously; keep CPU busy-lightly.
  150. }
  151.  
  152. /* END CODE */
  153.  
Advertisement
Add Comment
Please, Sign In to add comment