// Define the Arduino pins for controlling the 6 transistors (2 per phase)
#define U1 3 // High side of phase U
#define U2 4 // Low side of phase U
#define V1 5 // High side of phase V
#define V2 6 // Low side of phase V
#define W1 9 // High side of phase W
#define W2 10 // Low side of phase W
// Define the PWM frequency range
#define MAX_FREQUENCY 1000 // Max PWM frequency (Hz)
#define MIN_FREQUENCY 10 // Min PWM frequency (Hz)
int potPin = A0; // Pin for the potentiometer (used for speed control)
int potValue = 0; // Variable to hold the potentiometer value
unsigned long lastTime = 0;
unsigned long period = 1000; // Default period (1 second)
void setup() {
// Set the transistor pins as OUTPUT
pinMode(U1, OUTPUT);
pinMode(U2, OUTPUT);
pinMode(V1, OUTPUT);
pinMode(V2, OUTPUT);
pinMode(W1, OUTPUT);
pinMode(W2, OUTPUT);
// Initialize serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the potentiometer value (0 to 1023)
potValue = analogRead(potPin);
// Map the potentiometer value to a frequency range (MIN_FREQUENCY to MAX_FREQUENCY)
int frequency = map(potValue, 0, 1023, MIN_FREQUENCY, MAX_FREQUENCY);
// Calculate the period based on the frequency (period = 1/frequency)
period = 1000 / frequency; // Convert frequency to period in milliseconds
// Print the frequency for debugging
Serial.print("Frequency: ");
Serial.println(frequency);
// Generate the 3-phase control signal
unsigned long currentTime = millis();
if (currentTime - lastTime >= period) {
lastTime = currentTime;
// First phase: U
digitalWrite(U1, HIGH); // High side of phase U
digitalWrite(U2, LOW); // Low side of phase U
digitalWrite(V1, LOW); // Low side of phase V
digitalWrite(V2, HIGH); // High side of phase V
digitalWrite(W1, LOW); // Low side of phase W
digitalWrite(W2, HIGH); // High side of phase W
delay(period / 3); // Wait for 1/3rd of the period for the next phase
// Second phase: U
digitalWrite(U1, LOW); // Low side of phase U
digitalWrite(U2, HIGH); // High side of phase U
digitalWrite(V1, HIGH); // High side of phase V
digitalWrite(V2, LOW); // Low side of phase V
digitalWrite(W1, LOW); // Low side of phase W
digitalWrite(W2, HIGH); // High side of phase W
delay(period / 3); // Wait for 1/3rd of the period for the next phase
// Third phase: U
digitalWrite(U1, LOW); // Low side of phase U
digitalWrite(U2, HIGH); // High side of phase U
digitalWrite(V1, LOW); // Low side of phase V
digitalWrite(V2, HIGH); // High side of phase V
digitalWrite(W1, HIGH); // High side of phase W
digitalWrite(W2, LOW); // Low side of phase W
delay(period / 3); // Wait for 1/3rd of the period for the next phase
}
}