Friday, October 31, 2014

Three Phase BLDC Motor Bridge

To control a three phase bldc motor you must have a bridge to switch the three phases on and off. This bridge consists of switches and some other electronic components. I used in my three phase bridge 6 mosfets, 3 N-type and 3 P-type. I used some other transistors and resistors to turn these mosfets on and off.
The schematic diagram of the three phase circuit is shown below:


We will use this circuit to control a cd-rom sensored three phase bldc motor speed and direction using arduino uno board.

Wednesday, October 29, 2014

Arduino DC Motor Control 2

After controlling a dc motor speed and direction using arduino uno board at this link Arduino DC Motor Control 1 I am going to remove the potentiometer and add two push buttons to control the speed up and down.
In the arduino uno board there are two external interrupts: int0 on digital pin 2 and int1 on digital pin 3. We will use int0 to speed up the motor and int1 to reduce the speed of our motor. I added an led to show that the speed is at maximum value (at pin 12).
The code of the interrupt is as:
  attachInterrupt(0, speed_up, FALLING);
  attachInterrupt(1, speed_down, FALLING);
 
 When the interrupt at pin 2 occurs a function called speed_up will be called and the 
 speed of our motor will be raised. When the interrupt at pin 3 occurs a function called
 speed_down will be called and the speed is going to go down.
 The circuit is shown below: 



The arduino code is below:


#include <PWM.h>
int32_t frequency = 20000; //pwm frequency in Hz
  int s = 0;
   void setup(){
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  attachInterrupt(0, speed_up, FALLING);
  attachInterrupt(1, speed_down, FALLING);
   InitTimersSafe();
  bool success = SetPinFrequencySafe(9, frequency);
  }
 int fwd(){
   while(1){
   digitalWrite(0,HIGH);
   digitalWrite(1,LOW);
   if (digitalRead(6)==0) break;}}
  
 int pwd(){
   while(1){
   digitalWrite(0,LOW);
   digitalWrite(1,HIGH);
   if (digitalRead(6)==0) break;}}
   
  void loop()
 {
  digitalWrite(0,LOW);
  digitalWrite(1,LOW);
  digitalWrite (12,LOW);
  digitalWrite(13, LOW);
  pwmWrite(9,s);
  if (digitalRead(4)==0) {digitalWrite(13, HIGH);fwd();}
  if (digitalRead(5)==0) {digitalWrite(13, HIGH);pwd();}
 }
 void speed_up(){
   s = s + 5;
   if (s >255){
    s = 255;
    digitalWrite (12,HIGH);}
    else digitalWrite (12,LOW);
 pwmWrite(9,s);
  }
 void speed_down(){
   s = s - 5;
   if (s <5) s = 0;
   pwmWrite(9,s);
   digitalWrite (12,LOW);
  }

Last this video has some explanations:


Saturday, October 25, 2014

Arduino DC Motor Control 1

I have finished with constructing the circuit of controlling the speed and direction of 12V dc motor with arduino uno board via 3 wires, 1 for the speed and the 2 others for the direction. This circuit uses pwm technique to control the speed, and the direction is selected by turning 4 mosfets on and off, these 4 mosfets makes our H bridge circuit (For the H-bridge circuit I used visit this TOPIC).
Also the pwm frequency is 20KHz through pin 9. (about arduino pwm: Arduino PWM)
In the circuit the are 7 LEDs connected to shift register 74HC164N to show the speed level of our motor.
In the program I used timer 2 interrupt every about 300ms to read the analog value at A0 to generate the pwm signal and display the speed level.
The H-bridge circuit is shown below:

The complete circuit is:


And the code is below:


#include <PWM.h>
unsigned char k=0, l=0, m=0, n=0;
int32_t frequency = 20000; //pwm frequency in Hz
  int timer2_initial_value,s;
   void setup(){
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
   InitTimersSafe();
  bool success = SetPinFrequencySafe(9, frequency);
      
  // initialize timer2 interrupt for adc reading 
  noInterrupts();           // disable all interrupts
  TCCR2A = 0;
  TCCR2B = 0;
  timer2_initial_value = 0;  
  TCNT2 = timer2_initial_value;   // preload timer
  TCCR2B |= (1 << CS22) |(1 << CS21) | (1 << CS20); // 1024 prescaler 
  TIMSK2 |= (1 << TOIE2);   // enable timer overflow interrupt
  interrupts();             // enable all interrupts
 }

 ISR(TIMER2_OVF_vect)        // interrupt service routine 
 {
  TCNT2 = timer2_initial_value;   // preload timer
  n++;
  if (n>20){
    n = 0;
    k = 0;
    if (s != analogRead(A0)){
   s = analogRead(A0); 
  pwmWrite(9,s/4);
  if (s > 950) l = 0;
  else{ if (s > 850) l = 1;
        else{ if (s > 720) l = 3;
              else{ if (s > 600) l = 7;
                    else{ if (s > 480) l = 15;
                          else{ if (s > 350) l = 31;
                                else{ if (s > 150) l = 63;
                                      else l = 127;}}}}}}
 for(m=0;m<7;m++){  //send data serially to speed level via shift register
   digitalWrite (12,bitRead(l,k));
   digitalWrite (11,1);
   digitalWrite (11,0);k++;}
  }
 }
 }
 int fwd(){
   while(1){
   digitalWrite(0,HIGH);
   digitalWrite(1,LOW);
   if (digitalRead(4)==0) break;}}
  
 int pwd(){
   while(1){
   digitalWrite(0,LOW);
   digitalWrite(1,HIGH);
   if (digitalRead(4)==0) break;}}
   
  void loop()
 {
   k = 0;
   l = 127;   //all 7 leds are off
  digitalWrite(0,LOW);
  digitalWrite(1,LOW);
  digitalWrite(13, LOW);
  if (digitalRead(2)==0) {digitalWrite(13, HIGH);fwd();}
  if (digitalRead(3)==0) {digitalWrite(13, HIGH);pwd();}
  
  for(m=0;m<7;m++){
   digitalWrite (12,bitRead(l,k));
   digitalWrite (11,1);
   digitalWrite (11,0);k++;}
 }


Monday, October 20, 2014

H Bridge Circuit

I have constructed my own h bridge circuit to control the speed and direction of 12V dc motor. With the H-bridge I can control dc motor forward and backward using two wires, and the speed is controlled by a third wire witch is connected to a pwm signal.
Basic Operation Of The H-Bridge:
The basic circuit of the H-bridge is simply contains a dc voltage source,4 switches and load as shown in the following figure:



Now, if S1 and S4 are ON (S2 and S3 must be OFF) the current will flow from positive to negative across the motor and the motor will run at the forward direction.



If S2 and S3 are ON (S1 and S4must be OFF) the current will flow according to the following diagram and our motor will have backward direction:


In our circuit we are going to use mosfets as switches.
2 X IRF4905 (P-type)
2 X IRF3205 (N-type)
The full circuit is shown in the following figure:
IN1 & IN2 to control the direction of the motor
If IN1 = IN2 = 0 the motor is at OFF state (1 means 5V and 0 gnd)
If IN1 =1 & IN2 = 0 forward direction
If IN1 =0 & IN2 = 1 backward direction
Note that IN1 & IN2 must not equal to 1 at the same time.
PWM pin is connected to a pwm signal. If pwm = 1 means full speed.






Sunday, October 5, 2014

Arduino PWM

Arduino uno board uses timers to generate pwm signals on pins 3,5,6,9,10 and 11 using analogwrite() command. These pwm signals have fixed frequency of 490Hz unless pins 5 and 6 have frequency equals to 980Hz. if we need different frequency we will use the pwm library.
Using pwm library for arduino we can adjust our pwm frequency in easy way, this library works with some pins in the arduino uno board, and the command used is:
  pwmWrite(pin_number,duty_cycle);
 Here is an example which generate a pwm signal on pin 9 with a frequency of 20KHz:
 
#include <PWM.h>

int32_t frequency = 20000;

void setup(){
  InitTimersSafe();
  bool success = SetPinFrequencySafe(9, frequency);
  if(success) {
    pinMode(13, OUTPUT);
    digitalWrite(13, HIGH);    
  }
}
void loop(){
  int s = analogRead(A0);
  pwmWrite(9,s/4);
  delay(30);
 
}