Setting same Frequency in all pins

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)
}

On the Uno T0 is used for system timing and should not be changed. For some strange reason the 8 bit counters T0/T2 use different prescalers which can make it hard to bring them to the same frequency. Depending on your timer mode it may be impossible to get exactly 20kHz for 10kHz PWM.

I'd use a Mega with 6 timers and 3 channels per timer.

i want frequency greater than 5 KHz in all pins for the switching of mosfet. But the pins in 5 and 6 are not becoming equal to pins 3,11,10,9

you use analogWrite

look at the documentation

BOARD PWM PINS * PWM FREQUENCY
UNO (R3 and earlier), Nano, Mini 3, 5, 6, 9, 10, 11 490 Hz (pins 5 and 6: 980 Hz)

On which pins did you get a frequency greater than 5kHz?
And if so, how did you do that?

I used this code to generate frequency higher than 490 Hz and 980 Hz. by using this code in 3,9,10,11 I can get a frequency of 31.37Khz and in pins 5 and 6 I can get frequency of 62.5 Khz. I tried changing the values still it is not becoming equal

void setup()
 {
    pinMode(3,155);
    TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
}
void loop() 
{
    analogWrite(3,155);
    TCCR2B = TCCR2B & B11111000 | B00000001; // for PWM frequency of 31372.55 Hz
}

Here it is only showing the frequency change in pin 3 and if we change the value 3 to 11 we get the 31.37 KHz in pin 11 and if we change this timer TCCR2B to TCCR1B and changing the pin values, we can change the frequency of pins 9 and 10 to 31.3 KHz. But by changing the variable to TCCR0B and pin value to 5 or 6, I get 62.5 KHz. also changing the values of the code like TCCR2B = TCCR2B & B11111000 | B00000010; I get frequency of 7.81 KHz in pin 5 or 6.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.