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: SPWM Controller
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2025-08-20 23:28:11
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* SPWM VFD OF A 3PH induction motor in ISIS PROTEUS */
- /* using ARDUINO MEGA 2560 , controlled by 4 Buttons */
- /* (RUN FORWARD,RUN BACKWARD,STOP,RESET) , and there */
- /* is Potontionmeter and HC-05 and lcd 16x2 I2C. From */
- /* arduino mega 2560 to IR2110 to Inverter to moto */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- // NOTE: System requirements removed from code per guidelines.
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- // Additional helpers (optional)
- /***** GLOBALS *****/
- const int FORWARD_BTN_PIN = 22;
- const int BACKWARD_BTN_PIN = 23;
- const int STOP_BTN_PIN = 24;
- const int RESET_BTN_PIN = 25;
- const int POT_PIN = A0; // speed control
- // Inverter bridge outputs driven by IR2110 (complementary PWM per leg)
- const int PH_A_H = 2;
- const int PH_A_L = 3;
- const int PH_B_H = 5;
- const int PH_B_L = 6;
- const int PH_C_H = 9;
- const int PH_C_L = 10;
- // HC-05 connected via Serial1 on Mega (RX1=19, TX1=18)
- // 9600 baud for HC-05
- // LCD I2C 16x2
- LiquidCrystal_I2C lcd(0x27, 16, 2);
- enum MotorState { STATE_STOP, STATE_FORWARD, STATE_BACKWARD };
- MotorState motorState = STATE_STOP;
- const float PI = 3.14159265f; // pi constant for phase calculations
- float angleRad = 0.0f; // rotor electrical angle estimate for SPWM
- // Debounce tracking for buttons
- int lastForwardBtn = HIGH;
- int lastBackwardBtn = HIGH;
- int lastStopBtn = HIGH;
- int lastResetBtn = HIGH;
- unsigned long lastPWMMillis = 0;
- const unsigned long PWM_UPDATE_INTERVAL_MS = 5; // ~200 Hz refresh rate for PWM
- float speedFactor = 0.5f; // 0.0 - 1.0, from potentiometer
- void updatePWM(float speedFactor, int direction);
- void setup(void)
- {
- // put your setup code here, to run once:
- Serial.begin(115200);
- Serial1.begin(9600); // HC-05
- lcd.init();
- lcd.backlight();
- lcd.clear();
- // Button inputs with pull-ups (pressed = LOW)
- pinMode(FORWARD_BTN_PIN, INPUT_PULLUP);
- pinMode(BACKWARD_BTN_PIN, INPUT_PULLUP);
- pinMode(STOP_BTN_PIN, INPUT_PULLUP);
- pinMode(RESET_BTN_PIN, INPUT_PULLUP);
- // PWM outputs to IR2110 inputs (six signals)
- pinMode(PH_A_H, OUTPUT);
- pinMode(PH_A_L, OUTPUT);
- pinMode(PH_B_H, OUTPUT);
- pinMode(PH_B_L, OUTPUT);
- pinMode(PH_C_H, OUTPUT);
- pinMode(PH_C_L, OUTPUT);
- // Initialize outputs neutral (no drive)
- analogWrite(PH_A_H, 0);
- analogWrite(PH_A_L, 255);
- analogWrite(PH_B_H, 0);
- analogWrite(PH_B_L, 255);
- analogWrite(PH_C_H, 0);
- analogWrite(PH_C_L, 255);
- // Initial status on LCD
- lcd.setCursor(0, 0);
- lcd.print("State: STOP ");
- lcd.setCursor(0, 1);
- lcd.print("Speed: 0% ");
- motorState = STATE_STOP;
- angleRad = 0.0f;
- lastPWMMillis = millis();
- }
- void loop(void)
- {
- // Read potentiometer to derive speed factor
- int potValue = analogRead(POT_PIN);
- speedFactor = (float)potValue / 1023.0f;
- // Simple edge-detect for RUN FORWARD button
- int fState = digitalRead(FORWARD_BTN_PIN);
- if (fState == LOW && lastForwardBtn == HIGH) {
- motorState = STATE_FORWARD;
- }
- lastForwardBtn = fState;
- // BACKWARD button
- int bState = digitalRead(BACKWARD_BTN_PIN);
- if (bState == LOW && lastBackwardBtn == HIGH) {
- motorState = STATE_BACKWARD;
- }
- lastBackwardBtn = bState;
- // STOP button
- int sState = digitalRead(STOP_BTN_PIN);
- if (sState == LOW && lastStopBtn == HIGH) {
- motorState = STATE_STOP;
- }
- lastStopBtn = sState;
- // RESET button
- int rState = digitalRead(RESET_BTN_PIN);
- if (rState == LOW && lastResetBtn == HIGH) {
- motorState = STATE_STOP;
- angleRad = 0.0f;
- }
- lastResetBtn = rState;
- // HC-05: accept simple single-letter commands to control motor
- if (Serial1.available()) {
- String cmd = Serial1.readStringUntil('\n');
- cmd.trim();
- if (cmd.length() > 0) {
- if (cmd.equalsIgnoreCase("F")) motorState = STATE_FORWARD;
- else if (cmd.equalsIgnoreCase("B")) motorState = STATE_BACKWARD;
- else if (cmd.equalsIgnoreCase("S")) motorState = STATE_STOP;
- else if (cmd.equalsIgnoreCase("R")) { motorState = STATE_STOP; angleRad = 0.0f; }
- }
- }
- // PWM generation and update
- unsigned long now = millis();
- if (now - lastPWMMillis >= PWM_UPDATE_INTERVAL_MS) {
- lastPWMMillis = now;
- // Update electrical angle based on direction and speed
- float delta = (speedFactor * 0.15f) * (motorState == STATE_FORWARD ? 1.0f : (motorState == STATE_BACKWARD ? -1.0f : 0.0f));
- angleRad += delta;
- // wrap angle to [-PI, PI] range loosely
- if (angleRad > 6.28318530718f) angleRad -= 6.28318530718f;
- if (angleRad < -6.28318530718f) angleRad += 6.28318530718f;
- // 3-phase sine values with 120 degrees phase shift
- float thetaA = angleRad;
- float thetaB = angleRad + (2.0f * PI / 3.0f);
- float thetaC = angleRad + (4.0f * PI / 3.0f);
- // PWM amplitude is bounded by maxPWM; scale with speed
- int maxPWM = 60 + (int)(speedFactor * 195.0f); // range ~60..255
- int dA = (int)((sin(thetaA) * 0.5f + 0.5f) * maxPWM);
- int dB = (int)((sin(thetaB) * 0.5f + 0.5f) * maxPWM);
- int dC = (int)((sin(thetaC) * 0.5f + 0.5f) * maxPWM);
- dA = constrain(dA, 0, 255);
- dB = constrain(dB, 0, 255);
- dC = constrain(dC, 0, 255);
- // Complementary PWM for each leg (high side = duty, low side = inverted)
- analogWrite(PH_A_H, dA);
- analogWrite(PH_A_L, 255 - dA);
- analogWrite(PH_B_H, dB);
- analogWrite(PH_B_L, 255 - dB);
- analogWrite(PH_C_H, dC);
- analogWrite(PH_C_L, 255 - dC);
- // Update LCD with state and speed
- lcd.setCursor(0, 0);
- lcd.print("State: ");
- if (motorState == STATE_FORWARD) lcd.print("FORWARD ");
- else if (motorState == STATE_BACKWARD) lcd.print("BACKWARD ");
- else lcd.print("STOP ");
- lcd.setCursor(0, 1);
- int sp = (int)(speedFactor * 100.0f);
- lcd.print("Speed:");
- if (sp < 10) lcd.print(" ");
- else if (sp < 100) lcd.print(" ");
- lcd.print(sp);
- lcd.print("% ");
- }
- // Optional: echo any data received on Serial (USB) for debugging
- if (Serial.available()) {
- Serial.write(Serial.read());
- }
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment