Saturday, April 19, 2014

Running a stepper motor through ATMEGA16

When it comes to position control, and if we are talking about precision in the range of micro meter or nano meter, there is strictly no substitute to servo motors and linear motors. Here are videos of some of the projects we have done at Parker.


This is the link to what a linear motor means.

So, why don't we just start using servo motors for all applications? Its fast, precise and compact right? But, if I tell you the cost of a servo motor-drive package, you would probably be baffled! You can buy hundreds of stepper systems for the cost of just ONE servo system. So, does that mean servo motors are seldom used because of their prohibitively high costs? The answer is a big NO. "It is the application that demands which product to select and then comes the cost" - as my boss usually says!

Ok. Now that we understand the urge to go for a cheaper alternative, we end up with stepper motors and DC geared motors for most/all of educational and hobby projects. These products are easy to setup and economical. They can be run by a simple microprocessor like 8085.

Now we'll see how to interface and run a stepper motor from ATMEGA16 microcontroller. The same procedure applies to all controllers. Here is the link to download its datasheet.

ATMEGA16 is a 8-bit high performance microcontroller of Atmel's Mega AVR family with low power consumption. It is based on enhanced Reduced Instruction Set Computing (RISC) architecture, and supports 131 powerful instructions (most of which execute in one machine cycle). It can be clocked at a maximum frequency of 16MHz.

ATMEGA16 had 16 KB of programmable flash memory, 1 KB of static RAM and 512 B of EEPROM, with an endurance cycle of 10000 and 100000 for RAM and EEPROM respectively. It is a 40 pin IC with 32 digital I/O divided uniformly across 4 ports, namely, PORT A, PORT B, PORT C and PORT D. Mentioned below is a brief description of the individual pins.

Pin No.
Pin name
Description
Alternate Function
1
(XCK/T0) PB0
I/O PORTB, Pin 0
T0: Timer0 External Counter Input.
XCK : USART External Clock I/O
2
(T1) PB1
I/O PORTB, Pin 1
T1:Timer1 External Counter Input
3
(INT2/AIN0) PB2
I/O PORTB, Pin 2
AIN0: Analog Comparator Positive I/P
INT2: External Interrupt 2 Input
4
(OC0/AIN1) PB3
I/O PORTB, Pin 3
AIN1: Analog Comparator Negative I/P
OC0 : Timer0 Output Compare Match Output
5
(SS) PB4
I/O PORTB, Pin 4
In System Programmer (ISP)
Serial Peripheral Interface (SPI)
6
(MOSI) PB5
I/O PORTB, Pin 5
7
(MISO) PB6
I/O PORTB, Pin 6
8
(SCK) PB7
I/O PORTB, Pin 7
9
RESET
Reset Pin, Active Low Reset

10
Vcc
Vcc = +5V

11
GND
GROUND
12
XTAL2
Output to Inverting Oscillator Amplifier
13
XTAL1
Input to Inverting Oscillator Amplifier
14
(RXD) PD0
I/O PORTD, Pin 0
USART Serial Communication Interface
15
(TXD) PD1
I/O PORTD, Pin 1
16
(INT0) PD2
I/O PORTD, Pin 2
External Interrupt INT0
17
(INT1) PD3
I/O PORTD, Pin 3
External Interrupt INT1
18
(OC1B) PD4
I/O PORTD, Pin 4
PWM Channel Outputs
19
(OC1A) PD5
I/O PORTD, Pin 5
20
(ICP) PD6
I/O PORTD, Pin 6
Timer/Counter1 Input Capture Pin
21
PD7 (OC2)
I/O PORTD, Pin 7
Timer/Counter2 Output Compare Match Output
22
PC0 (SCL)
I/O PORTC, Pin 0
TWI Interface
23
PC1 (SDA)
I/O PORTC, Pin 1
24
PC2 (TCK)
I/O PORTC, Pin 2
JTAG Interface
25
PC3 (TMS)
I/O PORTC, Pin 3
26
PC4 (TDO)
I/O PORTC, Pin 4
27
PC5 (TDI)
I/O PORTC, Pin 5
28
PC6 (TOSC1)
I/O PORTC, Pin 6
Timer Oscillator Pin 1
29
PC7 (TOSC2)
I/O PORTC, Pin 7
Timer Oscillator Pin 2
30
AVcc
Voltage Supply = Vcc for ADC
31
GND
GROUND
32
AREF
Analog Reference Pin for ADC
33
PA7 (ADC7)
I/O PORTA, Pin 7
ADC Channel 7
34
PA6 (ADC6)
I/O PORTA, Pin 6
ADC Channel 6
35
PA5 (ADC5)
I/O PORTA, Pin 5
ADC Channel 5
36
PA4 (ADC4)
I/O PORTA, Pin 4
ADC Channel 4
37
PA3 (ADC3)
I/O PORTA, Pin 3
ADC Channel 3
38
PA2 (ADC2)
I/O PORTA, Pin 2
ADC Channel 2
39
PA1 (ADC1)
I/O PORTA, Pin 1
ADC Channel 1
40
PA0 (ADC0)
I/O PORTA, Pin 0
ADC Channel 0
Now for some brief introduction to stepper motors. I had a 4 lead motor available with me. But, 6 leads and 8 leads also come as a standard design. These leads are nothing but wires of the individual winding. Other classification is unipolar or bipolar. But this depends on how you connect the wires, whether in series, or in parallel. Another factor to choose a stepper motor is the accuracy required. We have standard industrial stepper motors available upto 50,000 steps per rotation. i.e. the shaft rotates once, if 50,000 pulses are given. If you are perplexed as to why we are talking about steps here, consider that a stepper motor has 50,000 ppr (pulses per rotation). Its minimum angle of rotation will be 360/50000 = 0.0072 degrees! See that? It becomes very precise as the number of steps increases. We can also implement micro-stepping to further enhance the accuracy. Higher number of steps leads to another hidden advantage - the motor performs a very smooth motion without any jerking. 

We know that a microcontroller can source only a few tens of milli amperes at the outputs. How do we get to drive such high currents required to energize the motor windings? Well we use a motor driver to achieve this task. I prefer L293D, which is a simple H bridge motor driver. What it does is simply takes the input from the microcontroller, amplifies it and gives this amplified output to run the motor.

Now that we know a brief background of our controller and motor, we will proceed to interfacing the products. As with any microcontroller, we need to provide 5 volt regulated supply to the IC. I use a IC7805 to regulate 12 VDC produced from a RPS. Then, to generate the clock frequency, I use a crystal oscillator. Next, I rig up the USART interfacing set-up to burn the code. We need ports to plug in motor cables and sensors, right? So, I solder ports to help me connect the cables. Since many cables are required to be soldered, its better to go for a printed circuit board (PCB). You could get an evaluation version of eagle or proteus (I used proteus) and do the PCB designing. Trust me, after an hour of watching online youtube videos, PCB designing using these softwares is a cakewalk. You could print the inverted image of this Gerber on a A4 sheet using an inkjet printer (preferably print the design several times on the same sheet) and reverse iron it on the PCB base. Now, dip this PCB into ferric chloride solution and rub off the ink transferred onto the PCB (while you pressed the paper) with some thinner or nail polish remover.

(OR) simply get one these cool development kits from an online store, as I did :p Trust me, it saves hell a lot of time and efforts, let alone the money. I bought one from Robosapiens India. The kit included a development board, a programming interface, an USB cable and an instruction CD. The CD contained the programming software, AVR View, the program transferring tool, Burn-O-Mat and Win AVR. The language used to program was embedded C, which when compiled was converted into RISC instructions and transferred to the controller.


This one's the burner.

We'll write a basic program to run a stepper motor now. This program will run the stepper motor one rotation forward and one rotation reverse.

#define F_CPU 12000000UL
#include
#include
int i;
int main(void)
{
DDRD=0xFF;
for(i=10;i!=0;i--)
{
PORTD=0b00110100;
_delay_ms(50);
PORTD=0b00111000;
_delay_ms(50);
PORTD=0b01110000;
_delay_ms(50);
PORTD=0b10110000;
_delay_ms(50);
}
_delay_ms(1000);
for(i=10;i!=0;i--)
{
PORTD=0b10110000;
_delay_ms(50);
PORTD=0b01110000;
_delay_ms(50);
PORTD=0b00111000;
_delay_ms(50);
PORTD=0b00110100;
_delay_ms(50);
}
}

As you may have spotted, I had connected the stepper motor leads to PORTD. With minor modifications in the code, you can run the motor, one rotation forward and one rotation reverse, indefinitely. Got it? Yes, put a "while(1)" instruction at the top of the code!

Happy playing with your stepper motor! :)

-Cheers,
Vijay.

3 comments: