Saturday, October 26, 2013

Interfacing D.C. Motor with ATmega32 using Mosfets


The main Drawback of interfacing D.C. motors using IC's like L293D is that the maximum current which it can give to the motor is limited to less than 1 Ampere. So, it is not suitable for running high torque motors which require higher amount of current. For that purpose we can create our own H - bridge circuit using mosfets.

Components Required

  • ATmega32 microcontroller
  • AVR programmer board
  • Crystal
  • Capacitors
  • Motors
  • Mosfets (2 N-channel and 2 P-channel)
  • +5 V Supply
  • Battery Equivalent to the voltage rating of the motor
  • Breadboard
  • Connecting wires 

Instead of  2 N-channel Mosfets and 2 - P channel Mosfets all the 4 Mosfets can be of same type i.e. all 4 can be N - type or all four can be of P-type, but you have to make corresponding changes in the source code.

Control Logic

  • To make the motor move in the forward direction, +12 V and 0 V must be applied to the ends of the D.C. motor which will be controlled by controlling the Gates of the mosfet through the Source Code in the ATmega32.
  • The dark lines show the flow of current in the circuit.

For forward rotation Gate of Q1 and Q3 are high and Q2 and Q4 are low.So Q1 will conduct because of positive current being a N - channel Mosfet and Q4 will conduct without a positive current being a P - channel Mosfet making the terminals +12 V and 0 V.


For forward rotation Gate of Q1 and Q3 are low and Q2 and Q4 are high.So Q2 will conduct because of positive current being a N - channel Mosfet and Q3 will conduct without a positive current being a P - channel Mosfet making the terminals +12 V and 0 V.



Circuit Diagram


Description

  • PC0 to PC3 are connected to the four gates of the mosfet.
  • A crystal is connected to XTAL1 and XTAL2 pins to provide clock pulse.
  • Reset is connected to +5V.

Source Code

/* The following Source code will make the motor to run forward two seconds and then backward two seconds for infinite time*/

#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 1000000UL        //Frequency of the external crystal

int main()
 { 
         DDRC = 0xFF;                   // PORTC as output port
PORTC = 0x00;                  // Initially all pins of PORTC as output low
 
   while (1)                                     // Infinte Loop
   {
  PORTC = 0b00000101;     //forward rotation 
  _delay_ms(2000);
  PORTC = 0b00001010;     //backward rotation
  _delay_ms(2000);
   }
   return 0;
 }

Circuit Diagram (if reverse rotation is not required)


Description

  • PC0 to PC1 are connected to the two gates of the mosfet.
  • A crystal is connected to XTAL1 and XTAL2 pins to provide clock pulse.
  • Reset is connected to +5V.

Source Code for Normal Run of D.C. Motor

#include <avr/io.h>

int main()
 { 
         DDRC = 0xFF;                              // PORTC as output port
   PORTC = 0b00000001;              //forward rotation 
            while (1);                                     // Infinte Loop
   return 0;
 }


Source code for Speed Control Using PWM

/***
*In order to provide PWM the output of the gates must be fluctuating
*In the on time of the duty cycle the required mosfets must be on
* In the off time of the duty cycle all the mostets must be of which is done by applying logic high to P - mosfets and logic low to N - mosfets
***/
#include <avr/io.h>
#include <util/delay.h>

#define F_CPU 1000000UL        //Frequency of the external crystal

int main()
 { 
         DDRC = 0xFF;                   // PORTC as output port
PORTC = 0x00;                  // Initially all pins of PORTC as output low
 
   while (1)
   {
  PORTC = 0b00000001;    //on time
  _delay_ms(1);
  PORTC = 0b00000010;   //off time
  _delay_ms(1);

   }
   return 0;
 }


For more tutorial and information like the Facebook Page 

Thank You for Reading


1 comment:

  1. in second case you put again forward direction instead of backward direction

    ReplyDelete