This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:iot-open:practical:hardware:itt:avr:stepper [2025/09/02 14:18] – raivo.sell | en:iot-open:practical:hardware:itt:avr:stepper [2025/09/02 14:30] (current) – raivo.sell | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | < | ||
| + | ====== Stepper motor ====== | ||
| + | ===== Theory ===== | ||
| + | [{{ : | ||
| + | |||
| + | Stepper motors are widely used in applications that demand accuracy. Unlike DC motors, stepper motors do not have brushes or a commutator – they have several independent coils, which are commutated with exterior electronics (drivers). Rotating the rotor is done by commutating coils step by step, without feedback. This is one of the faults in stepper motors – in case of mechanical overloading, | ||
| + | |||
| + | Depending on the model of stepper motor, performing one full rotation (360 degrees) of the rotor demands hundreds of steps of commutations. For stable and smooth movement, appropriate control electronics are used, which control the motor according to its parameters (inertia of the rotor, torque, resonance, etc.). In addition to control electronics, | ||
| + | |||
| + | ===== Practice ===== | ||
| + | The Combo Module has an H-bridge to control bipolar stepper motors and a transistor matrix for unipolar stepper motors. | ||
| + | |||
| + | There are functions // | ||
| + | |||
| + | <code c> | ||
| + | // Preparing for controlling the bipolar stepper motor | ||
| + | void bipolar_init(void) | ||
| + | { | ||
| + | DDRB |= 0x0F; | ||
| + | PORTB &= 0xF0; | ||
| + | } | ||
| + | |||
| + | // Moving the bipolar stepper motor by half steps | ||
| + | void bipolar_halfstep(signed char dir, | ||
| + | unsigned short num_steps, unsigned char speed) | ||
| + | { | ||
| + | unsigned short i; | ||
| + | unsigned char pattern, state1 = 0, state2 = 1; | ||
| + | |||
| + | // Insuring the direction +- 1 | ||
| + | dir = ((dir < 0) ? -1 : +1); | ||
| + | |||
| + | // Execution of half-steps. | ||
| + | for (i = 0; i < num_steps; i++) | ||
| + | { | ||
| + | state1 += dir; | ||
| + | state2 += dir; | ||
| + | |||
| + | // Creating the pattern | ||
| + | pattern = (1 << ( (state1 % 8) >> 1) ) | | ||
| + | (1 << ( (state2 % 8) >> 1) ); | ||
| + | |||
| + | // Setting the output. | ||
| + | PORTB = (PORTB & 0xF0) | (pattern & 0x0F); | ||
| + | |||
| + | // Taking a break to wait for executing the step | ||
| + | sw_delay_ms(speed); | ||
| + | } | ||
| + | |||
| + | // Stopping the motor | ||
| + | PORTB &= 0xF0; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Usage of the functions is demonstrated by the example program, which rotates the motor alternately to one direction and then to the other direction 200 half steps. The speed of rotating the motor is determined by the length of the breaks made between the steps. If the break is set to be too short, the motor can not accomplish the turn due to the inertia of the rotor, and the shaft does not move. | ||
| + | |||
| + | <code c> | ||
| + | // The test program for the stepper motor of the HomeLab | ||
| + | #include < | ||
| + | |||
| + | // Main program | ||
| + | int main(void) | ||
| + | { | ||
| + | // Set up of the motor | ||
| + | unipolar_init(0); | ||
| + | |||
| + | // Endless loop | ||
| + | while (true) | ||
| + | { | ||
| + | // Turning the rotor 200 half steps to one direction | ||
| + | // at speed of 30 ms/step. | ||
| + | unipolar_halfstep(0, | ||
| + | |||
| + | // Turning 200 half steps to the other direction | ||
| + | // at speed 30 ms/step. | ||
| + | unipolar_halfstep(0, | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Task to be implemented ==== | ||
| + | |||
| + | - Drive the stepper as a clock’s second hand: rotate 6° (one step chunk) every second to complete one full revolution per minute. | ||
| + | - Simulate an analog thermometer: | ||