Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] [Moved] Need Help: AVR - L293D - Bipolar stepper motor

Status
Not open for further replies.
Re: Need Help: AVR - L293D - Bipolar stepper motor

Hi, thank you,

I use this table:
tab.png

to build the following code:

Code:
void main(void)
{
PORTB=0x07;
DDRB=0xFF;

while (1)
      {
      PORTB=0b00101000;
      delay_ms(100);
      PORTB=0b00110000;
      delay_ms(100);
      PORTB=0b01010000;
      delay_ms(100);
      PORTB=0b01001000;
      delay_ms(100);
      }
}


I connect the control pins to PORTB.3,4,5,6

Well is that right?
 

Attachments

  • tab.png
    tab.png
    31.3 KB · Views: 67

Re: Need Help: AVR - L293D - Bipolar stepper motor

Yes that is correct but unless you use a resistor or 5v for the motor you will damage either the motor or the driver.
 

Re: Need Help: AVR - L293D - Bipolar stepper motor

Yeah, I use 5V.

Next, What if I want it to rotate left and right, and also how can I determine the STEPS?
 

Re: Need Help: AVR - L293D - Bipolar stepper motor

If you reverse the steps then the rotation direction will change but for smooth action you should go back from the current state rather than a random state.

You can use an array with all four states and either count up 0,1,2,3,0,1,2,3... or down 3,2,1,0,3,2,1,0

Code:
#define F_CPU 1000000UL  // 1 MHz (set according to mcu clock)

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

int main(void)
{
    uint8_t step[4] = {0b00101000, 0b00110000, 0b01010000, 0b01001000};
    uint8_t i = 0;
	
    PORTB = 0x07;
    DDRB = 0xFF;

    while(1)
    {
        PORTB = step[i];
        _delay_ms(100);
        i++;  // increment step index

        if(i == 4)
        {
            i = 0;    // restart from 0
        }
    }
}

- - - Updated - - -

Are you using codevision or gcc?
 

Re: Need Help: AVR - L293D - Bipolar stepper motor

Well, just one thing, If my motor is 3.75 degree/step, then I want to make it to rotate 360 degree, which part of that code I should change?

I use codevision, but I can rewrite it anyway.
 
Last edited:

Re: Need Help: AVR - L293D - Bipolar stepper motor

You have to count the steps some way so you can use a second counter variable and step the motor until the variable reaches 96
 

Re: Need Help: AVR - L293D - Bipolar stepper motor

Ok I got it. I did something like this:

Code:
#include <delay.h>
unsigned int step_left[4]={0b00101000, 0b00110000, 0b01010000, 0b01001000};
unsigned int step_right[4]={0b01001000, 0b01010000, 0b00110000, 0b00101000};

void turn_left(int step) 
    {
        int i=0;
        int j=0;
        while (j<=step) 
        {
            j++;
            i++;
            if (i==4) 
            {
                i=0;
            }
            
            PORTB = step_left[i];
            delay_ms(100);
        }
    }
    
void turn_right(int step) 
    {
        int i=0;
        int j=0;
        while (j<=step) 
        {
            j++;
            i++;
            if (i==4) 
            {
                i=0;
            }
            
            PORTB = step_right[i];
            delay_ms(100);
        }
    }


void main(void)
 {
 PORTB=0x00;
 DDRB=0xF8;

 while (1) 
    {
        turn_right(96);
        delay_ms(2000);
        turn_left(96);
        delay_ms(2000);
    }
 }

I works for now (is it right?)
And I have some questions:

1. What is the effect of "delay_ms(100);" and its value? I removed it previously and the motor does not rotate.
2. The above code is used for motor in one port only right?, but now I'm running out of I/O pins. I need to drive another motor and I just have 4 more pins left in different port (PORTD.0,2 and PORTA.6,7). Is there any tricks to manipulate it?
 

Re: Need Help: AVR - L293D - Bipolar stepper motor

It is better to use one array abc one function txt takes a direction parameter so that when the direction is inverted it goes to the correct step in the given sequence.

The delay controls the speed of the steps, if you go too low the motor will not move, after all it is a mechanic device.
Check the RPM in the darasheet and divide with the steps per revolution.
Note that hou are operating with s low voltage which limits the torque and will affect the speed too.
Also for high speed it is better to apply an acceleration curve.

There are tricks to drive the two pins of each half with one pin but I'm currently on android so I can't do much, check in google you should find examples with transiistors or logic gates.
 

Re: Need Help: AVR - L293D - Bipolar stepper motor

Alright, you mean like this:

Code:
#include <delay.h>
unsigned int motor_step[4]={0b00101000, 0b00110000, 0b01010000, 0b01001000};

void turn_left(int step) 
    {
        int i=0;
        int j=0;
        while (j<=step) 
        {
            j++;
            i++;
            if (i==4) 
            {
                i=0;
            }
            
            PORTB = motor_step[i];
            delay_ms(50);
        }
    }
    
void turn_right(int step) 
    {
        int i=0;
        int j=0;
        while (j<=step) 
        {
            j++;
            i--;
            if (i==-1) 
            {
                i=3;
            }
            
            PORTB = motor_step[i];
            delay_ms(50);
        }
    }


void main(void)
 {
 PORTB=0x00;
 DDRB=0xF8;

 while (1) 
    {
        turn_left(95);
        delay_ms(1000);
        turn_right(95);
        delay_ms(1000);
    }
 }


I just tested it in the hardware. Motors work well but I found that they disturb the other system. I think I need to make a separated power supply for the motors. Well, I had only 7812 (regulator) now and if I make new 12V supply, which pins I should put resistor to limit the current? pin 8 of the motor driver or pin3,6,11,14 ?
 

Re: Need Help: AVR - L293D - Bipolar stepper motor

The motor has two coils with a resistance of 10 ohm each, connect a resistor in series with each one to increase the resistance and limit the current.

What I meant was more like
Code:
#include <delay.h>
unsigned int motor_step[4] = {0b00101000, 0b00110000, 0b01010000, 0b01001000};

void turn(unsigned char direction, int step)
{
    static unsigned char i = 0;   // the index must be preserved so you can continue from where you left off
    unsigned int j = 0;

    while(j <= step)
    {
        j++;

        if(direction)
        {
            i++;
        }
        else
        {
            i--;
        }

        if(i == 255)
        {
            i = 3;
        }
        else
            if(i == 4)
            {
                i = 0;
            }

        PORTB = motor_step[i];   // this may need to e changed to affect only specific pins using bit masking
        delay_ms(50);
    }
}


void main(void)
{
    PORTB = 0x00;
    DDRB = 0xF8;

    while(1)
    {
        turn(1, 95); // I'm not sure about left or right direction, they may be inverted
        delay_ms(1000);
        turnt(0, 95);
        delay_ms(1000);
    }
}

You can also use defines for the directions for convenience

Code:
#define LEFT 1
#define RIGHT 0

//and use
turn(LEFT, 95);
turn(RIGHT, 95);
 

Sorry, I just didn't get what this part do:

Code:
        if(i == 255)
        {
            i = 3;
        }
        else
            if(i == 4)
            {
                i = 0;
            }
Can you please tell me?



About the resistor, so there will be only 2 resistors (24 Ohm) for that 4 wires right?
 

I have changed the i variable type from signed int to unsigned char so instead of 3,2,1,0,-1,-2 it goes 3,2,1,0,255,254... so I check for 255 to detect when it rolled over from 0

- - - Updated - - -

Yes, two resistors , one in series with each coil.
24ohm+10 of the coik will result in 34 ohm , for 12v/34= 0.35A
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top