Path: Home => AVR-EN => Applications => ATtiny24 stepper control   Diese Seite in Deutsch: Flag DE Logo
stepper_tn24 small Applications of
AVR Single chip controllers AT90S, ATtiny, ATmega and ATxmega
Stepper motor controller with an ATtiny24

Stepper motor 28BYJ-48 controller with an ATtiny24

Zipped version of these pages (310 kB) These pages as PDF (764 kB)


Stepper motor controller with an ATtiny24 Those who need a level-crossing or a gate closure for model-building are best suited with this stepper motor 48BYJ-24 and the ATtiny24 controller described here. It has the following properties: For a voltage-controlled positioning system for a stepper motor with 12 V operating voltage that uses an ATtiny13 see this page.

0 Overview

  1. Hardware
  2. mounting
  3. operation
  4. Software
  5. Trouble shooting

1 Hardware

1.1 Schematic

Stepper motor control schematic The stepper motor controller works with an ATtiny24. The motor control signals are outputted on the port bits PA4 to PA7 and drive the input pins of a darlington driver ULN2003A, which has protection diodes on its outputs for magnet applications. The output pins of the ULN2003A drive the magnets of the stepper motor 28BYJ-48 (see the schematic of the motor below). Software controls if only one single magnet is driven at a time (full step mode) or if one and two magnets are driven alternately (half step mode).

The AD converter input channels ADC0 and ADC1 are connected to trim potentiometers which determine the deflection in lower (closing) and upper (opening) direction.

AD converter input ADC2 is connected with a trim potentiometer that controls the motor speed.

On the port outputs PB0 and PB1 a duo LED is driven. This LED blinks in red (closing) or yellow (opening) as long as the motor moves. If the motor reaches its end position the LED is permanently on and is terminated when an additional delay time has elapsed and the motor magnets are switched off (currently one second, can be up to 6.7 seconds).

On port pin PA3 a green LED is attached that is illuminated when the motor is on its end position and the delay time is elapsed. This also signals that the current position is stored in the EEPROM.

The Open/Close switch on PB2 (INT0) opens or closes the level-crossing or gate.

The controller can be programmed in the final application by In-System-Programming via the standard six-pin plug.

1.2 Schematic of the stepper motor

Stepper motor 28BYJ-48 This is the schematic of the motor. Only four of the 16 magnets are displayed. Shown is the motor from the upper side where the axle is seen, magnet numbers are for movement in the right direction of the axle.

Together with the ULN2003A driver connections and the connections with the port pins of the ATtiny24 the magnet control for movement in right direction is shown in hexadecimal format, with which the upper port nibble has to be controlled.

The motor needs 64 single steps per round and has a built-in gear 1:64, so per round of the gear 64 * 64 = 2,048 single steps are required in full step mode. In half step mode 4,096 steps are required.

The motor can be driven in full step mode with a maximum speed of 4.2&s per gear round (488 Hz) without any load. At frequencies above the motor or at higher load it is not rotating smoothly any more or is even not moving at all. In half step mode the frequency can be slightly higher than doubled because of the 50% higher power. But slightly above 1,000 Hz it also reaches the end of correct rotation.

1.3 Component list

Stepper tn24 component list The necessary electronic parts are listed here. Prices are, of course, subject to changes.

The stepper motor 28BYJ-48 is not included in the list. This part can be ordered from traders on the internet quite cheaply for less than 5€ per piece. The driver board that is delivered with that has a plugged ULN2003A on board that can be removed and used here.



Page top 1 Hardware 2 Mounting 3 Operation 4 Software 5 Trouble shooting


2 Mounting

2.1 PCB

Stepper tn24 PCB The PCB that takes all the components is seen here. It is of a size of 50-by-40 mm and single sided and includes four mounting holes with a diameter of 2.5 mm.

2.2 Component placement

Stepper tn24 PCB component side Two wired bridges have to be soldered first. As trim potentiometer round as well as squared types can be used.

When adding the two plugs J1 and J2 please be aware of the spares, when mounting the IC sockets for the PDIP packages the mark for pin 1 is relevant.

The 6 pin box connector J2 is only to be mounted if you want to program the ATtiny24 in ISP mode.



Page top 1 Hardware 2 Mounting 3 Operation 4 Software 5 Trouble shooting



3 Operation

3.1 Start-up

The stepper motor should be mounted so that he is roughly in the middle position (half open). Slight deviations from the middle position are tolerable. If that is impossible, consult chapter 3.3 on how to adjust the motor's middle position.

With the switch open the upper point is adjusted with the upper position trim potentiometer. After closing the switch adjust the lower position. If the motor keeps moving one step up and one step down again, choose a slightly different position to ensure that the position is stored in the EEPROM and that the motor consumes not more power than necessary.

The rotating speed can be adjusted as desired. At higher loads, select a lower speed that ensures that the motor is running smoothly.

3.2 Normal operation

Under normal operation the switch opens and closes the level-crossing or the gate. If closing shall be initiated by an external signal, tie pin P5 to ground. Ensure that the signal duration is long enough to reach the green LED phase to ensure position storage.

Switch the operating voltage off, when the green LED is on, otherwise correct positioning does not work correct and re-adjustment might become necessary. This might become necessary if the upper or lower position cannot be adjusted any more. Consult chapter 3.3 in that case.

3.3 Re-adjusting middle position

The middle position is selected if both positioning trim potentiometers are in their left position (and switching does not result in motor movement any more). If, during mounting, the middle position is unreachable or if during operation this has moved too far up or down, re-adjust the middle position by following this procedure:

  1. Bring both trim potentiometers to their leftmost position.
  2. With the switch open (middle position to low) or closed (middle position to high) adjust the motor to the middle position with the respective trim potentiometer.
  3. Switch the supply voltage off and either remove the supply or wait until it has fallen to below 1.7 V.
  4. Connect the 270Ω resistor's lead that drives the green LED and is connected with pin 10 of the ATtiny24 with ground on pin P5.
    Pin 10 of the ATtiny24 to ground

  5. Switch the operating voltage on and then remove the connection between pin 10 of the ATtiny24 and ground. The motor should then move to a different position.
  6. Wait until the motor has reached its end position and until the green LED is on.
If the middle position could still not be reached with one re-adjustment, you can repeat this procedure.

Page top 1 Hardware 2 Mounting 3 Operation 4 Software 5 Trouble shooting


4 The software

The software is written in AVR assembler and is extensively commented. The source code can be downloaded here or viewed in the browser here. The code uses .if directives and can be assembled either with the gavrasm assembler or with ATMEL's assembler 2 (a how-to-page for assembling with gavrasm is available for Linux here and for Windows here).

Functioning of the software is elaborated in detail in the following chapters.

4.1 Positioning of the stepper motor

The current position of the stepper motor is held in the 16 bit register pair rCurrValH:rCurrValL. This value should be between 32,768 +/- 511 (full step mode) resp. 32,768 +/- 1,024 (half step mode). Change the mode in the head of the source code before assembling.

The desired target position of the step motor is in the register pair rSetValH:rSetValL. If CurrVal and SetVal differ, the motor moves in direction to SetVal (see the description in chapter 4.2 below).

The position in the open state is (32,768 - N) in register pair rOpenH:rOpenL, in closed position (32,768 + N) in rCloseH:rCloseL. Both positions are calculated from the respective trim potentiometer positions.

If the switch changes its state an INT0 interrupt is triggered and the T flag in the status register is set. Depending from the switch's position either the rOpen or the rClose value is written to rSetVal. The T flag is also set whenever new position values come in.

4.2 Stepper motor movement

Positioning of the stepper motor is performed by the 8 bit timer TC0. This timer runs with a prescaler of 64 (1 MHz / 64 = 15,625 Hz), each timer tick is 64 µ long. The timer runs in CTC mode (Clear Timer on Compare) and clears the counter after reaching and exceeding the Compare A value. Depending from the trimmed speed, this happens either after reaching 15 (half step mode, fastest speed) in 15*64 = 960 µs resp. 30 (full step mode, fastest speed) after 30*64 = 1,920 µs or after reaching 256 (0) (full step mode, lowest speed) after 256*64 = 16,384 µs.

Triggered by the TC0COMPA timer interrupt the interrupt service routine first checks if rCurrVal and rSetVal are equal. If that is the case, nothing else happens. If rCurrVal is smaller than rSetVal, rCurrVal is increased, otherwise it is decreased. In both cases the lowest two bits (full step mode) resp. three bits (half step mode) are used to read the magnets to be switched on from the respective table and to write those to the upper port nibble of port A of the ATtiny24. This also switches the green LED off (on pin PA3).

4.3 Switching the magnets off

The timing of the period after which the magnets of the motor are turned off following the last movement is performed by 16 bit timer TC1. This timer is clocked with a prescaler of 1,024.

Each TC0 timer interrupt, by which the position of the motor is changed, clears the timer TC1. Following the last movement TC1 runs and after the preselected time reaches its Compare A value. One second is selected by default, can be up to 6.7 seconds. The triggered TC1COMPA interrupt

4.4 Reading positions from the EEPROM

At start-up of the controller the following bytes from address 0x0010 of the EEPROM are read and written to the respective registers:
  1. a first mark Mark1 (should be 0xAA),
  2. the currently desired close value (rCloseL, rCloseH),
  3. the currently desired open value (rOpenL, rOpenH),
  4. the current motor position (rCurrValL, rCurrValH),
  5. a second mark Mark2 (should be 0x55).
If an exclusive or of Mark1 and Mark2 yields 0xFF the EEPROM data read is correct and initiation continues. If not motor position and open and close value are set to 32,768 (0x8000).

4.5 Writing positions to the EEPROM

If the stepper motor reaches its end position and the additional delay time has elapsed, the same bytes are copied from the registers to the EEPROM. To do this, the first data byte, Mark1, is written to the EEPROM data port and the address port is set to 0x0010. The first EEPROM write is the started and the EERDY interrupt enable bit is set. Further EEPROM writes are performed in the interrupt service routine until all bytes are written.

4.6 Measuring trim potentiometer values and calculating positions/speed

Potentiometer control and stepper motor This demonstrates the functions of the three potentiometers. Potentiometer 1 and 2 control the excursion from the middle position in upper and lower direction. Potentiometer 3 controls the speed of the stepper movement.

All three potentiometer positions are determined in cycles in which 64 single measurements are performed and summed up (10 bit ADC result, summed up to 16 bit sum values).

4.6.1 Calculating position values

In full step mode, the inverted MSB of the summed up result is multiplied by two, in half step mode by four. This value is subtracted from 32,768 to yield the rOpen value resp. added to 32,768 to yield the rClose value. The result is copied to the respective registers and the T flag is set, triggering update of the rSetVal.

4.6.2 Calculating speed and CTC values

The MSB of the summed up measurements is multiplied by (255-30)=225 (full step mode) resp. (255-14)=241 (half step mode) and 30 (full step mode) resp. 14 (half step mode) is added. The resulting value is written to the compare port OC0A of the 8 bit timer TC0 and controls the speed of motor movements.

4.7 Re-adjust

On each start-up of the controller the pull-up resistor on PA3 (cathode green LED, 270Ω resistor) is switched on and it is checked if the PA3 input is clear. If that is the case, the current motor position rCurrVal, the target position rSetVal as well as the rClose and rOpen registers are all set to 32,768 and reading of the EEPROM data is skipped.

If the PA3 input pin reaches high, a period of 100 ms is started over which any low on this pin restarts this period (toggle protection). Only after this period normal processing continues.

Page top 1 Hardware 2 Mounting 3 Operation 4 Software 5 Trouble shooting


5 Trouble-Shooting

5.1 Motor runs over and over

If the motor runs permanently in the same direction and does not stop, this can be caused by spikes on the operating voltage. Those spikes restart the ATtiny24 and will occur if you did not mount the 10nF capacitor near the ULN2003A. Make sure that this capacitor is attached and is functioning.

This can also occur if your supply is overloaded by the magnet current, which is slightly above 100 mA in full step mode or 200 mA in half step mode. In that case use a higher rated supply.

5.2 ISP programming of the ATtiny24 fails

If you program the ATtiny24 in the system with the ISP6 plug and if the motor is attached, the programming pulses cause a rapid switching of up to three magnets. If your power supply is only rated for 300 mA or less (e.g. in case of a supply over an USB interface), this can cause power spikes and programming fails. Use a higher rated supply for programming in that case.

Rapid switching of up to three magnets can also result in high back-currents, which can cause large spikes when short-circuited by the built-in diodes in the ULN2003A. Programming failure occurs also if the 10nF capacitor near the ULN2003A is not in place or not working correct. Increase its capacity to 100nF or tempoarily attach an electrolytical capacitor of 10µF to the power supply pins to cope with that.

Page top 1 Hardware 2 Mounting 3 Operation 4 Software 5 Trouble shooting




Praise, error reports, scolding and spam please via the comment page to me. Spammers: please note that behind this is not a dump standard script but I personally select the entries to be published. So, even if you try it five times (some spammers indeed do that) it will fail and ads for pharmacy will not occur here.

©2018 by http://www.avr-asm-tutorial.net