Controlling a small stepper motor


As you know there are a lot of shields and circuits to connect a stepper motor to a microcontroller.

But, using a small motor like this one

providing a pair of 8 Ω coils, you can connect it directly to the pins of the ATmega controller, and it works.

The math tells you 5 volts divided by 8 Ω gives a current of 625 mA, which is much above the limit of 40 mA, but the outputs give you only some 60 mA and experience shows that it will work for hours and hours without failure.


That's all.

/* run stepper with increasing speed */
const byte a1 = 5;
const byte a2 = 8;
const byte b1 = 6;
const byte b2 = 7;
const byte l1 = A2;
const byte l2 = A3;
const byte l3 = A4;
const byte l4 = A5;
const int tMin = 20; // don't go faster
const int tMax = 1000;
int dt = tMax; 
const int N = tMax-tMin;
int counter = N;
int inc;

void setup() {                
  Serial.begin(9600);
  Serial.println(F(__FILE__));
  for (byte i = a1; i <= a2; i++) pinMode(i, OUTPUT);
  for (byte i = l1; i <= l4; i++) pinMode(i, OUTPUT);
}

void loop() {
  if (counter >= N) inc = -1;
  if (counter <= 0) inc =  1;
  counter = counter + inc;
  Serial.print(counter);
  Serial.print(" ");
  Serial.println(dt);
  switch (counter % 4) {
    case 0: dw(l1,a1,LOW); dw(l2,a2,HIGH); dw(l3,b1,LOW); dw(l4,b2,HIGH); break;
    case 1: dw(l1,a1,LOW); dw(l2,a2,HIGH); dw(l3,b1,HIGH); dw(l4,b2,LOW); break;
    case 2: dw(l1,a1,HIGH); dw(l2,a2,LOW); dw(l3,b1,HIGH); dw(l4,b2,LOW); break;
    case 3: dw(l1,a1,HIGH); dw(l2,a2,LOW); dw(l3,b1,LOW); dw(l4,b2,HIGH); break;    
  }
  delay(dt);
  if (dt < tMin) dt = tMax;
  else if (dt < 100) dt = dt - abs(inc);
  else dt = dt - 35 * abs(inc);
}

void dw(byte Led, byte Motor, boolean b) {
  digitalWrite(Motor,b);
  digitalWrite(Led,b);
}



contact: nji(at)gmx.de