Saturday, October 26, 2013

Interfacing Bipolar Stepper motor using L293D


Bipolar Stepper motor

Bipolar motors have a single winding per phase. The current in a winding needs to be reversed in order to reverse a magnetic pole, so the driving circuit must be more complicated, typically with an H - bridge arrangement (however there are several off-the-shelf driver chips available to make this a simple affair). There are two leads per phase, none are common.Dithering the stepper signal at a higher frequency than the motor can respond to will reduce this "static friction" effect.
Because windings are better utilized, they are more powerful than a unipolar motor of the same weight. This is due to the physical space occupied by the windings. A unipolar motor has twice the amount of wire in the same space, but only half used at any point in time, hence is 50% efficient (or approximately 70% of the torque output available). Though a bipolar stepper motor is more complicated to drive, the abundance of driver chips means this is much less difficult to achieve.


Components Required

  • ATmega32 microcontroller
  • AVR programmer board
  • Crystal
  • Capacitors
  • Bipolar Stepper Motor
  • L293D
  • +5 V Supply
  • Battery Equivalent to the voltage rating of the motor
  • Breadboard
  • Connecting wires 

Circuit Diagram


For details on L293D IC go to 

Description

  • PB0 to PB3 are connected to the four inputs of the L293D IC.
  • Both the enable pins are separately connected to Vcc.
  • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
  • Reset is connected to +5 volt

Control Logic

To control the bipolar motor,apply voltage to each of the coils in a specific sequence. The sequence would go like this:


Source Code

/* This code will make the stepper motor to run continously */ 

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

#ifndef F_CPU
#define F_CPU 1000000UL         // frequency of external crystal
#endif

int main()
 { 
DDRB = 0xFF;                   //All pins of PORTB as output
PORTB = 0x00;                 //Initially all pins as output high
while(1)
{
PORTB = 0x01;          //0001
_delay_ms(10);
PORTB = 0x04;          //0100
_delay_ms(10);
PORTB = 0x02;          //0010
_delay_ms(10);
PORTB = 0x08;          //1000
_delay_ms(10);
}
return 0;
}


Source Code for controlled Rotation

/* This code will make the stepper motor to run for a specific angle */ 

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

#ifndef F_CPU
#define F_CPU 1000000UL         // frequency of external crystal
#endif

int main()
 {
         /*Calculate x by the minimum step angle of your motor and required rotation angle*/
         int x = 200;
         DDRB = 0xFF;                   //All pins of PORTB as output
 PORTB = 0x00;                 //Initially all pins as output high
while(x)                              //will loop till x is not equal to zero
{
PORTB = 0x01;          //0001
_delay_ms(10);
PORTB = 0x04;          //0100
_delay_ms(10);
PORTB = 0x02;          //0010
_delay_ms(10);
PORTB = 0x08;          //1000
_delay_ms(10);
                x--;                             //decrement x by 1
}
return 0;
}

For more tutorial and information, like the Facebook Page 


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


Friday, October 25, 2013

Speed Control of DC Motor using PWM

Pulse-width modulation is an effective method for adjusting the speed and the amount of power delivered to an electrical load.

Components Required




  • ATmega32 microcontroller
  • AVR Programmer Board
  • Crystal
  • Capacitors
  • Motors
  • L293D IC
  • +5V supply
  • Battery equivalent to voltage rating of motor 
  • Breadboard
  • Connecting Wires

  • To know about the L293D motor controller go through the previous blog on Interfacing D.C. Motor with ATmega32 using L293D.
    http://rickruling.blogspot.in/2013/10/interfacing-dc-motor-with-atmega32.html

    Circuit Diagram


    Description

    • PC0 and PC3 are connected to Enable 1 and Enable 2 respectively.
    • PC1,PC2,PC4 and PC5 are connected to Input 1-4.
    • OUT1 and OUT2 are connected to the terminals of first motor and OUT3 and OUT4 are connected to the terminals of the second motor.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5V.

    Source Code

    /*The following code will make the motor to run at 50% duty cycle*/
    #include <avr/io.h>
    #include <util/delay.h>

    #define F_CPU 1000000UL

    int main()
     { 
    DDRC = 0xFF;                                       //All pins of PORT C as output 
    PORTC = 0x00;                                     //Initially all pins at low output
    PORTC|=(1<<PC1)|(1<<PC4);            //PORT C 1 and 4 as high
    PORTC&=~((1<<PC2)|(1<<PC5));     //PORT C 2 and 5 as low
      /*In order to make the motor to rotate in opposite direction interchange the terminals of the motors (PINC1 and PINC2 for motor 1 and PINC4 and PINC5 for motor 2)*/
       while (1)                                                 //Infinite Loop
       {
          /*ON TIME*/
      PORTC|=(1<<PC0)|(1<<PC3);
          _delay_us(5);
          /*OFF TIME*/
          PORTC&=~((1<<PC0)|(1<<PC3));
          _delay_us(5);
               
       }
       return 0;
    }


    duty cycle = (on_time)/(on_time+off_time)
    duty cycle = 5/10 = 0.5
    To increase the duty cycle increase the delay of on_time and to decrease the duty cycle increase the delay of off_time.

    Practical Limitations of Pulse Width Modulation Motor Control

    Pulse-width modulation is difficult below 25% for motors because they don’t gain the same rotational inertia in comparison to the static resistances of the grease, gearing, and gaps between commutators. A weird aspect of PWM on motors is that it can create audible whining. Basically, if you select a PWM frequency in a human-audible range, the mechanical device will likely oscillate audibly. Increasing the frequency above 20 kHz may silence the motor whining. But, some motors, transistors, or motor driver chips are unable to switch on and off that quickly.


    For more tutorial and information like the Facebook Page 

    Thank You for Reading

    Thursday, October 24, 2013

    Interfacing D.C. Motor with ATmega32 using L293D

    Components Required

    • ATmega32 microcontroller
    • AVR Programmer Board
    • Crystal
    • Capacitors
    • Motors
    • L293D IC
    • +5V supply
    • Battery equivalent to voltage rating of motor 
    • Breadboard
    • Connecting Wires

    Motor Controller


    L293D - Dual D.C. Motor Controller
    • Enable-1 controls the output of output1 and output2.
    • Enable-2 controls the output of output3 and output4.
    • Vss is connected to +5V.
    • Vs is connected to a voltage equal to the voltage rating of the motor.
    • Enable 1-2 and Input 1-4 comes from the microcontroller.
    • If speed control using PWM is not needed, the Enable Pins can be directly connected to +5V.
    • All 4 grounds are shorted to a common ground.
    • Output 1-4 goes to the motor.

    Circuit Diagram


    Description

    • PC0 and PC3 are connected to Enable 1 and Enable 2 respectively.
    • PC1,PC2,PC4 and PC5 are connected to Input 1-4.
    • OUT1 and OUT2 are connected to the terminals of first motor and OUT3 and OUT4 are connected to the terminals of the second motor.
    • A crystal is connected to XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5V.

    Source Code

    /*The following code will make the motor to run forward for 2 seconds 
    and then backward for 2 seconds for infinite time*/
    #include <avr/io.h>
    #include <util/delay.h>

    #define F_CPU 1000000UL

    int main()
     { 
    DDRC = 0xFF;                                  // PORTC as output port
    PORTC = 0x00;                                //Initially all pins as output low
    PORTC|=(1<<PC0)|(1<<PC3);        //C0 and C3 are connected to enable
             while (1)                                            // Infinite Loop
             {
                       /*Forward Motion*/
      PORTC|=(1<<PC1)|(1<<PC4);              //#1
               PORTC&=~((1<<PC2)|(1<<PC5));       //#2
                       _delay_ms(2000);                                    //#3
                      /*Backward Motion*/
          PORTC&=~((1<<PC1)|(1<<PC4));       //#4
               PORTC|=(1<<PC2)|(1<<PC5);              //#5
                _delay_ms(2000);                                   //#6
             }
             return 0;
      }

       # 1 .  PORTC|=(1<<PC1)|(1<<PC4);  
                1<<PC1 will generate a character 00000010 and 1<<PC4 will genarate a character 00010000.
                Taking OR of this values will generate 00010010. Now OR of this value and the initial condition of               PORTC will make the Bits 1 and 4 as high independent of all other pins of  PORTC.

        # 2 .  PORTC&=~((1<<PC2)|(1<<PC5));  
                1<<PC2 will generate a character 00000100 and 1<<PC5 will genarate a character 00100000.
                Taking OR of this values will generate 00100100 and NOT of this value will generate 11011011.                 Now AND of this value and the initial condition of PORTC will make the Bits 2 and 5 as low                       independent of all other pins of  PORTC.

        # 3. _delay_ms(2000);  
                A delay of 2 seconds after which the motor will change its direction.

        # 4 . PORTC&=~((1<<PC1)|(1<<PC4));  
                1<<PC1 will generate a character 00000010 and 1<<PC4 will genarate a character 00010000.
                Taking OR of this values will generate 00010010 and NOT of this value will generate 11101101.                 Now AND of this value and the initial condition of PORTC will make the Bits 1 and 4 as low                       independent of all other pins of  PORTC.

        # 5 .  PORTC|=(1<<PC2)|(1<<PC5);  
                1<<PC2 will generate a character 00000100 and 1<<PC5 will genarate a character 00100000.
                Taking OR of this values will generate 00100100. Now OR of this value and the initial condition of               PORTC will make the Bits 2 and 5 as high independent of all other pins of  PORTC.

        # 6. _delay_ms(2000);  
                A delay of 2 seconds after which the motor will change its direction.

    Source Code for Normal Run of DC motor

    #include <avr/io.h>

    #define F_CPU 1000000UL

    int main()
     { 
    DDRC = 0xFF;
    PORTC = 0x00;
    PORTC|=(1<<PC0)|(1<<PC3);               //C0 and C3 are connected to enable
             PORTC|=(1<<PC1)|(1<<PC4);               //C1 and C4 as output high
    PORTC&=~((1<<PC2)|(1<<PC5));        //C2 and C5 as output low
             while(1)                                                    // Infinite Loop
               ;
       return 0;
    }

    ThankYou for Reading.

                         

    ATmega32 LED blinking Experiment

    The first and simplest project for any microcontroller is LED Blinking.

    Circuit Diagram


    Components Required

    • ATmega32 microcontroller
    • Programmer Board
    • LEDs
    • Crystal - less than 16 MHz.
    • Capacitors - 2 
    • +5 V supply
    • Breadboard
    • Connecting Wires
    Optional
    • Resistors - 330 Ohms to be connected in series with the LEDs(not shown in the circuit diagram) 

    Description

    • Here, all pins of PORT B are declared as output ports as all the LEDs are connected to the pins of PORTB.
    • A crystal is connected to the XTAL1 and XTAL2 pins to provide the clock pulse.
    • Reset is connected to +5V.
    Source Code

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

    #define F_CPU 1000000UL    // Defines the frequency of external crystal
        
    int main()
     { 
              /* The Data direction register of all Pins of Port B is Set as Output*/
             DDRB = 0xFF;                
    PORTB = 0x00;               // Initially all pins of Port B are set as Output Low
       while (1)                               // Infinite Loop
       {
      PORTB = 0b11111110;   // all LEDs will glow except the first LED 
      _delay_ms(500);            // delay of 500 milliseconds 
      PORTB = 0b11111101;   // all LEDs will glow except the second LED 
      _delay_ms(500);            // delay of 500 milliseconds 
      PORTB = 0b11111011;   // all LEDs will glow except the third LED 
      _delay_ms(500);            // delay of 500 milliseconds 
      PORTB = 0b11110111;   // all LEDs will glow except the fourth LED 
      _delay_ms(500);            // delay of 500 milliseconds 
      PORTB = 0b11101111;   // all LEDs will glow except the fifth LED 
      _delay_ms(500);            // delay of 500 milliseconds 
      PORTB = 0b11011111;   // all LEDs will glow except the sixth LED 
      _delay_ms(500);            // delay of 500 milliseconds 
      PORTB = 0b10111111;   // all LEDs will glow except the seventh LED 
      _delay_ms(500);            // delay of 500 milliseconds 
      PORTB = 0b01111111;   // all LEDs will glow except the eight LED 
      _delay_ms(500);            // delay of 500 milliseconds 
       }
       return 0;
     }



    Thankyou for Reading

    Status Register of ATmega32

    The Status Register contains information about the result of the most recently executed arithmetic
    instruction. This information can be used for altering program flow in order to perform
    conditional operations. Note that the Status Register is updated after all ALU operations, as
    specified in the Instruction Set Reference. This will in many cases remove the need for using the
    dedicated compare instructions, resulting in faster and more compact code.
    The Status Register is not automatically stored when entering an interrupt routine and restored
    when returning from an interrupt. This must be handled by software.

    The AVR Status Register – SREG – is defined as:



    • Bit 7 – I: Global Interrupt Enable
    The Global Interrupt Enable bit must be set for the interrupts to be enabled. The individual interrupt
    enable control is then performed in separate control registers. If the Global Interrupt Enable
    Register is cleared, none of the interrupts are enabled independent of the individual interrupt
    enable settings. The I-bit is cleared by hardware after an interrupt has occurred, and is set by
    the RETI instruction to enable subsequent interrupts. The I-bit can also be set and cleared by
    the application with the SEI and CLI instructions, as described in the instruction set reference.

    • Bit 6 – T: Bit Copy Storage
    The Bit Copy instructions BLD (Bit LoaD) and BST (Bit STore) use the T-bit as source or destination
    for the operated bit. A bit from a register in the Register File can be copied into T by the
    BST instruction, and a bit in T can be copied into a bit in a register in the Register File by the
    BLD instruction.

    • Bit 5 – H: Half Carry Flag
    The Half Carry Flag H indicates a half carry in some arithmetic operations. Half Carry is useful in
    BCD arithmetic.

    • Bit 4 – S: Sign Bit, S = N ⊕ V
    The S-bit is always an exclusive or between the Negative Flag N and the Two’s Complement
    Overflow Flag V.

    • Bit 3 – V: Two’s Complement Overflow Flag
    The Two’s Complement Overflow Flag V supports two’s complement arithmetics.

    • Bit 2 – N: Negative Flag
    The Negative Flag N indicates a negative result in an arithmetic or logic operation.

    • Bit 1 – Z: Zero Flag
    The Zero Flag Z indicates a zero result in an arithmetic or logic operation.

    • Bit 0 – C: Carry Flag
    The Carry Flag C indicates a carry in an arithmetic or logic operation. 

    Pin Descriptions of ATMEGA32


    Pin Diagram

    Description

    VCC Digital supply voltage.

    GND Ground.

    Port A (PA7..PA0) Port A serves as the analog inputs to the A/D Converter or 8-bit bi-directional I/O port(if the A/D Converter is not used). Port pins can provide internal pull-up resistors (selected for each bit). The Port A output buffers have symmetrical drive characteristics with both high sink and source capability. When pins PA0 to PA7 are used as inputs and are externally pulled low, they will source current if the internal pull-up resistors are activated. The Port A pins are tri-stated when a reset condition becomes active, even if the clock is not running.

    Port B (PB7..PB0)         Port B is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port B output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port B pins that are externally pulled low will source current if the pull-up resistors are activated. The Port B pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port B also serves the functions of various special features.

    Port C (PC7..PC0)  Port C is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port C output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port C pins that are externally pulled low will source current if the pull-up resistors are activated. The Port C pins are tri-stated when a reset condition becomes active, even if the clock is not running. If the JTAG interface is enabled, the pull-up resistors on pins PC5(TDI), PC3(TMS) and PC2(TCK) will be activated even if a reset occurs. The TD0 pin is tri-stated unless TAP states that shift out data are entered. Port C also serves the functions of the JTAG interface and other special features of the ATmega32.

    Port D (PD7..PD0)  Port D is an 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit). The Port D output buffers have symmetrical drive characteristics with both high sink and source capability. As inputs, Port D pins that are externally pulled low will source current if the pull-up resistors are activated. The Port D pins are tri-stated when a reset condition becomes active, even if the clock is not running. Port D also serves the functions of various special features of the ATmega32.

    RESET     Reset Input. A low level on this pin for longer than the minimum pulse length will generate a reset, even if the clock is not running. Pulses shorter than the minimum pulse length are not guaranteed to generate a reset.

    XTAL1    Input to the inverting Oscillator amplifier and input to the internal clock operating circuit.

    XTAL2    Output from the inverting Oscillator amplifier.

    AVCC    AVCC is the supply voltage pin for Port A and the A/D Converter. It should be externally connected to VCC, even if the ADC is not used. If the ADC is used, it should be connected to VCC through a low-pass filter.

    AREF    AREF is the analog reference pin for the A/D Converter.

    Thankyou for Reading