Please help me with stepper motor

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I tried to control the direction of the stepper motor by using switch like if switch ON stepper motor will rotate clockwise and if switch OFF stepper motor will rotate anticlockwise.

I tried to write the code, please help me and correct the code according to the circuit.

please help me I will be thankful

 

#define F_CPU 8000000UL       
#include <avr/io.h>        
#include <util/delay.h>       

int main(void)
{
    int period;
    DDRC = 0x0F;        
    DDRA = 0x00;        
    period = 100;        

    while (1)
    {
        m=PINA;
        
        if (m==0x01)
        for(int i=0;i<12;i++)
        {
            PORTC = 0x09;
            _delay_ms(period);
            PORTC = 0x08;
            _delay_ms(period);
            PORTC = 0x0C;
            _delay_ms(period);
            PORTC = 0x04;
            _delay_ms(period);
            PORTC = 0x06;
            _delay_ms(period);
            PORTC = 0x02;
            _delay_ms(period);
            PORTC = 0x03;
            _delay_ms(period);
            PORTC = 0x01;
            _delay_ms(period);
        }
                
        else if (m==0x00)
        for(int i=0;i<12;i++)
        {
            PORTC = 0x01;
            _delay_ms(period);
            PORTC = 0x03;
            _delay_ms(period);
            PORTC = 0x02;
            _delay_ms(period);
            PORTC = 0x06;
            _delay_ms(period);
            PORTC = 0x04;
            _delay_ms(period);
            PORTC = 0x0C;
            _delay_ms(period);
            PORTC = 0x08;
            _delay_ms(period);
            PORTC = 0x09;
            _delay_ms(period);
        }
        PORTC = 0x09;       
        _delay_ms(period);
        _delay_ms(1000);
    }
}

 

Last Edited: Fri. May 15, 2020 - 10:22 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Without knowing...

 

1) The connections to your motor and

2) The drive waveform you expect

 

...it is impossible to give an answer.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Uhhhh...the ULN2003 connects 7 different outputs to ground!

How are you expecting to drive the motor when it has no source of voltage?

 

At least in your code use names for your bit patterns coil1, coil23, coil89, whatever,  to show what coils your are activating.  That is MUCH clearer than 0x06 !!      

 

  DDRC = 0x0F;        /* Make PORTD lower pins as output */
    DDRA = 0x00;        /* Make PORTC lower pins as input */

 

your comment names don't match the port names!

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mr  Brian Fairchild  the connections are right and the circuit is running but I found difficulties in the code not in the circuit

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The simple answer is : you invert the output to one of the coils the motor will change direction.

 

But in real life you want to ramp down to 0 and then ramp up in the other direction.

Last Edited: Fri. May 15, 2020 - 10:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

intelmen wrote:
I tried to write the code

before writing any code, did you spend time studying how stepper motors work, and looking at examples of how to drive them from microcontrollers ?

 

http://homepage.divms.uiowa.edu/~jones/step/

 

https://www.google.com/search?q=arduino+stepper+motor

 

https://www.google.com/search?q=AVR+stepper+motor

 

Your schematic looks like Proteus, so see: https://www.avrfreaks.net/commen...

 

See Tip #1 (in my signature, below) for how to properly post source code:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

intelmen wrote:
I found difficulties in the code not in the circuit

So what "difficulties", exactly, did you find? 

 

And what have you tried to resolve them ?

 

It's not like there's a shortage of prior art here ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. May 15, 2020 - 10:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear mr.awneil

did you spend time to understand the question before putting a comment

 

It's ok that you do not know how to correct it and you do not have any idea

Last Edited: Fri. May 15, 2020 - 10:34 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd suggest you be a bit more respectful.

 

 m=PINA;
  if (m==0x01)

 

You assume the other pins in PORTA are going to be 0. Unfortunately in the real world that won't hsppen unless you do something specific to make that true.

 

What you probably want is:

 

if (PINA & 1)

{

.....

}

 

Have you read the documentation for _delay_ms()? I think it might tell you it doesn't like a variable time value - it wants a constant.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you are seriously putting AC (Alternating Current?!) into the center taps of your stepper motor windings, it's a miracle you haven't had a cheerful 'boom!' yet.

 

COM on the ULN2003 should be taken to Ground, and the center taps of the stepper motor windings to 5V DC.

 

  S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


In the real world this:

would not work at all due to the missing power for half of your AVR provided by the AVCC pin, I would also want a 100nf cap connected between AREF and GND!

intelmen wrote:

Dear mr.awneil

did you spend time to understand the question before putting a comment

 

It's ok that you do not know how to correct it and you do not have any idea

disrespectful comments like that will get you no where, You are asking for help, Andy asked you clarification questions which you ignored.

Good luck with your project!

Jim

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Scroungre wrote:
COM on the ULN2003 should be taken to Ground,

On these parts, COM usually refers to 'common free wheeling diodes' and would be connected to the load Vcc ie. it's the connection point for the in built protection diodes in parallel with the load

GND would be connected to ground :)

But the OP's schemtaic doesn't show GND pin (pin 8)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Need to have a more detail view for showing the motor and the power source for it.  The ULN2003 only provides grounding.

 

With just a few modifications your code will be better than ever. 

 

Remember, every bit is important!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You still haven't told us what is wrong with your code.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MrKendo wrote:

Scroungre wrote:
COM on the ULN2003 should be taken to Ground,

On these parts, COM usually refers to 'common free wheeling diodes' and would be connected to the load Vcc ie. it's the connection point for the in built protection diodes in parallel with the load

GND would be connected to ground :)

But the OP's schemtaic doesn't show GND pin (pin 8)

 

Correct.  My bad.  S.