pleasedontcode

SPWM Controller rev_01

Aug 20th, 2025
363
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: SPWM Controller
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2025-08-20 23:28:11
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* SPWM VFD OF A 3PH induction motor  in ISIS PROTEUS */
  21.     /* using ARDUINO MEGA 2560 , controlled by 4 Buttons */
  22.     /* (RUN FORWARD,RUN BACKWARD,STOP,RESET) , and there */
  23.     /* is Potontionmeter and HC-05 and lcd 16x2 I2C. From */
  24.     /* arduino mega 2560 to IR2110 to Inverter to moto */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27.  
  28. /* START CODE */
  29.  
  30. /****** DEFINITION OF LIBRARIES *****/
  31. #include <Wire.h>
  32. #include <LiquidCrystal_I2C.h>
  33.  
  34. // NOTE: System requirements removed from code per guidelines.
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. // Additional helpers (optional)
  41.  
  42. /***** GLOBALS *****/
  43. const int FORWARD_BTN_PIN = 22;
  44. const int BACKWARD_BTN_PIN = 23;
  45. const int STOP_BTN_PIN = 24;
  46. const int RESET_BTN_PIN = 25;
  47. const int POT_PIN = A0; // speed control
  48.  
  49. // Inverter bridge outputs driven by IR2110 (complementary PWM per leg)
  50. const int PH_A_H = 2;
  51. const int PH_A_L = 3;
  52. const int PH_B_H = 5;
  53. const int PH_B_L = 6;
  54. const int PH_C_H = 9;
  55. const int PH_C_L = 10;
  56.  
  57. // HC-05 connected via Serial1 on Mega (RX1=19, TX1=18)
  58. // 9600 baud for HC-05
  59.  
  60. // LCD I2C 16x2
  61. LiquidCrystal_I2C lcd(0x27, 16, 2);
  62.  
  63. enum MotorState { STATE_STOP, STATE_FORWARD, STATE_BACKWARD };
  64. MotorState motorState = STATE_STOP;
  65.  
  66. const float PI = 3.14159265f; // pi constant for phase calculations
  67. float angleRad = 0.0f; // rotor electrical angle estimate for SPWM
  68.  
  69. // Debounce tracking for buttons
  70. int lastForwardBtn = HIGH;
  71. int lastBackwardBtn = HIGH;
  72. int lastStopBtn = HIGH;
  73. int lastResetBtn = HIGH;
  74.  
  75. unsigned long lastPWMMillis = 0;
  76. const unsigned long PWM_UPDATE_INTERVAL_MS = 5; // ~200 Hz refresh rate for PWM
  77.  
  78. float speedFactor = 0.5f; // 0.0 - 1.0, from potentiometer
  79.  
  80. void updatePWM(float speedFactor, int direction);
  81.  
  82. void setup(void)
  83. {
  84.     // put your setup code here, to run once:
  85.     Serial.begin(115200);
  86.     Serial1.begin(9600); // HC-05
  87.  
  88.     lcd.init();
  89.     lcd.backlight();
  90.     lcd.clear();
  91.  
  92.     // Button inputs with pull-ups (pressed = LOW)
  93.     pinMode(FORWARD_BTN_PIN, INPUT_PULLUP);
  94.     pinMode(BACKWARD_BTN_PIN, INPUT_PULLUP);
  95.     pinMode(STOP_BTN_PIN, INPUT_PULLUP);
  96.     pinMode(RESET_BTN_PIN, INPUT_PULLUP);
  97.  
  98.     // PWM outputs to IR2110 inputs (six signals)
  99.     pinMode(PH_A_H, OUTPUT);
  100.     pinMode(PH_A_L, OUTPUT);
  101.     pinMode(PH_B_H, OUTPUT);
  102.     pinMode(PH_B_L, OUTPUT);
  103.     pinMode(PH_C_H, OUTPUT);
  104.     pinMode(PH_C_L, OUTPUT);
  105.  
  106.     // Initialize outputs neutral (no drive)
  107.     analogWrite(PH_A_H, 0);
  108.     analogWrite(PH_A_L, 255);
  109.     analogWrite(PH_B_H, 0);
  110.     analogWrite(PH_B_L, 255);
  111.     analogWrite(PH_C_H, 0);
  112.     analogWrite(PH_C_L, 255);
  113.  
  114.     // Initial status on LCD
  115.     lcd.setCursor(0, 0);
  116.     lcd.print("State: STOP  ");
  117.     lcd.setCursor(0, 1);
  118.     lcd.print("Speed: 0%     ");
  119.  
  120.     motorState = STATE_STOP;
  121.     angleRad = 0.0f;
  122.     lastPWMMillis = millis();
  123. }
  124.  
  125. void loop(void)
  126. {
  127.     // Read potentiometer to derive speed factor
  128.     int potValue = analogRead(POT_PIN);
  129.     speedFactor = (float)potValue / 1023.0f;
  130.  
  131.     // Simple edge-detect for RUN FORWARD button
  132.     int fState = digitalRead(FORWARD_BTN_PIN);
  133.     if (fState == LOW && lastForwardBtn == HIGH) {
  134.         motorState = STATE_FORWARD;
  135.     }
  136.     lastForwardBtn = fState;
  137.  
  138.     // BACKWARD button
  139.     int bState = digitalRead(BACKWARD_BTN_PIN);
  140.     if (bState == LOW && lastBackwardBtn == HIGH) {
  141.         motorState = STATE_BACKWARD;
  142.     }
  143.     lastBackwardBtn = bState;
  144.  
  145.     // STOP button
  146.     int sState = digitalRead(STOP_BTN_PIN);
  147.     if (sState == LOW && lastStopBtn == HIGH) {
  148.         motorState = STATE_STOP;
  149.     }
  150.     lastStopBtn = sState;
  151.  
  152.     // RESET button
  153.     int rState = digitalRead(RESET_BTN_PIN);
  154.     if (rState == LOW && lastResetBtn == HIGH) {
  155.         motorState = STATE_STOP;
  156.         angleRad = 0.0f;
  157.     }
  158.     lastResetBtn = rState;
  159.  
  160.     // HC-05: accept simple single-letter commands to control motor
  161.     if (Serial1.available()) {
  162.         String cmd = Serial1.readStringUntil('\n');
  163.         cmd.trim();
  164.         if (cmd.length() > 0) {
  165.             if (cmd.equalsIgnoreCase("F")) motorState = STATE_FORWARD;
  166.             else if (cmd.equalsIgnoreCase("B")) motorState = STATE_BACKWARD;
  167.             else if (cmd.equalsIgnoreCase("S")) motorState = STATE_STOP;
  168.             else if (cmd.equalsIgnoreCase("R")) { motorState = STATE_STOP; angleRad = 0.0f; }
  169.         }
  170.     }
  171.  
  172.     // PWM generation and update
  173.     unsigned long now = millis();
  174.     if (now - lastPWMMillis >= PWM_UPDATE_INTERVAL_MS) {
  175.         lastPWMMillis = now;
  176.  
  177.         // Update electrical angle based on direction and speed
  178.         float delta = (speedFactor * 0.15f) * (motorState == STATE_FORWARD ? 1.0f : (motorState == STATE_BACKWARD ? -1.0f : 0.0f));
  179.         angleRad += delta;
  180.         // wrap angle to [-PI, PI] range loosely
  181.         if (angleRad > 6.28318530718f) angleRad -= 6.28318530718f;
  182.         if (angleRad < -6.28318530718f) angleRad += 6.28318530718f;
  183.  
  184.         // 3-phase sine values with 120 degrees phase shift
  185.         float thetaA = angleRad;
  186.         float thetaB = angleRad + (2.0f * PI / 3.0f);
  187.         float thetaC = angleRad + (4.0f * PI / 3.0f);
  188.  
  189.         // PWM amplitude is bounded by maxPWM; scale with speed
  190.         int maxPWM = 60 + (int)(speedFactor * 195.0f); // range ~60..255
  191.         int dA = (int)((sin(thetaA) * 0.5f + 0.5f) * maxPWM);
  192.         int dB = (int)((sin(thetaB) * 0.5f + 0.5f) * maxPWM);
  193.         int dC = (int)((sin(thetaC) * 0.5f + 0.5f) * maxPWM);
  194.  
  195.         dA = constrain(dA, 0, 255);
  196.         dB = constrain(dB, 0, 255);
  197.         dC = constrain(dC, 0, 255);
  198.  
  199.         // Complementary PWM for each leg (high side = duty, low side = inverted)
  200.         analogWrite(PH_A_H, dA);
  201.         analogWrite(PH_A_L, 255 - dA);
  202.  
  203.         analogWrite(PH_B_H, dB);
  204.         analogWrite(PH_B_L, 255 - dB);
  205.  
  206.         analogWrite(PH_C_H, dC);
  207.         analogWrite(PH_C_L, 255 - dC);
  208.  
  209.         // Update LCD with state and speed
  210.         lcd.setCursor(0, 0);
  211.         lcd.print("State: ");
  212.         if (motorState == STATE_FORWARD) lcd.print("FORWARD  ");
  213.         else if (motorState == STATE_BACKWARD) lcd.print("BACKWARD ");
  214.         else lcd.print("STOP      ");
  215.  
  216.         lcd.setCursor(0, 1);
  217.         int sp = (int)(speedFactor * 100.0f);
  218.         lcd.print("Speed:");
  219.         if (sp < 10) lcd.print("  ");
  220.         else if (sp < 100) lcd.print(" ");
  221.         lcd.print(sp);
  222.         lcd.print("%     ");
  223.     }
  224.  
  225.     // Optional: echo any data received on Serial (USB) for debugging
  226.     if (Serial.available()) {
  227.         Serial.write(Serial.read());
  228.     }
  229. }
  230.  
  231. /* END CODE */
  232.  
Advertisement
Add Comment
Please, Sign In to add comment