I am making a driver for bldc motor using arduino uno. Here is the code I am using. Using this code, I can generate square pulse. the only error is in the ' int HallState ' to check whether it generate square pulse. I want to generate the square pulse having frequency of 10KHz in all PWM pins. But I couln't do it. I tried several code form different sites. But the Frequency in pins 5 and 6 are not becoming equal to other pins. Can somebody please help me with this code?
// Define Hall sensor pins
const int hallA = 2;
const int hallB = 4;
const int hallC = 7;
// Define motor driver pins
const int motorAL = 10;
const int motorAH = 11;
const int motorBL = 6;
const int motorBH = 9;
const int motorCL = 3;
const int motorCH = 5;
int hallState = 1;
void setup(){
// Hall sensor pins as inputs
pinMode(hallA, INPUT);
pinMode(hallB, INPUT);
pinMode(hallC, INPUT);
// Motor driver pins as outputs
pinMode(motorAL, OUTPUT);
pinMode(motorAH, OUTPUT);
pinMode(motorBL, OUTPUT);
pinMode(motorBH, OUTPUT);
pinMode(motorCL, OUTPUT);
pinMode(motorCH, OUTPUT);
}
void loop(){
switch (hallState) {
case 1: // Hall sequence: 001 - CBA
commutate(0, 1, 0, 0, 1, 0 );
break;
case 2: // Hall sequence: 010
commutate(1, 0, 0, 1, 0, 0);
break;
case 3: // Hall sequence: 011
commutate(0, 0, 0, 1, 1, 0);
break;
case 4: // Hall sequence: 100
commutate(0, 0, 1, 0, 0, 1);
break;
case 5: // Hall sequence: 101
commutate(0, 1, 1, 0, 0, 0);
break;
case 6: // Hall sequence: 110
commutate(1, 0, 0, 0, 0, 1);
break;
default:
// Handle error or unknown state
break;
}
}
void commutate(int in1State, int in2State, int in3State, int in4State, int in5State, int in6State) {
// Set motor driver inputs using PWM
analogWrite(motorAL, in1State ? 127 : 0); // Use PWM if in1State is HIGH (1)
analogWrite(motorAH, in2State ? 127 : 0); // Use PWM if in2State is HIGH (1)
analogWrite(motorBL, in3State ? 127 : 0); // Use PWM if in3State is HIGH (1)
analogWrite(motorBH, in4State ? 127 : 0); // Use PWM if in4State is HIGH (1)
analogWrite(motorCL, in5State ? 127 : 0); // Use PWM if in5State is HIGH (1)
analogWrite(motorCH, in6State ? 127 : 0); // Use PWM if in6State is HIGH (1)
}