DC Motor Speed Control Without PWM


Do everything at your own risk
Small toy DC-motors have resistances down to 5 Ω. But if you are a litte bit courageous you still can connect them to an Arduino pin. Actually, I would not dare doing this if I did not use the DIL-28 version and had some spare ATmega328s lying around.

So I connected all the pins of Port B to the negative of the motor and the positive to +5Vcc. To control the motor I set the pinMode of these pins to either INPUT or OUTPUT.

There is a pushbutton to increase the speed by adding one of the OUTPUTs. Also I have some LEDs at Port D to indicate the current state.

And further more, there is a wire to analog input A0 which shows the voltage the motor is provided with. You also see the spikes which are typical for DC motors.

Do everything at your own risk

Arduino and DC motor


Voltage at A0


Do everything at your own risk
/*
   Hardware:
   DC motor (low power)
   positive: connect to +5Vcc
   negative:
   connect to all d8, d9, d10, d11, d12, d13
   connect also to A0
*/

void setup() {
  Serial.begin(115200);
  // display LEDs at Port D:
  DDRD = 255;
  // pushbutton at A5:
  pinMode(A5, INPUT_PULLUP);
}

int speed;

void loop() {
  int v = analogRead(A0);
  Serial.print("0 1023 ");
  Serial.println(1024 - v);
  if (digitalRead(A5) == HIGH) return;
  delay(10);
  // wait for release of button:
  while (digitalRead(A5) == LOW);
  if (++speed > 6) speed = 0;
  // adjust speed:
  switch (speed) {
    case 0: DDRB = 0; break; // all off
    case 6: pinMode(13, OUTPUT);
    case 5: pinMode(12, OUTPUT);
    case 4: pinMode(11, OUTPUT);
    case 3: pinMode(10, OUTPUT);
    case 2: pinMode(9, OUTPUT);
    case 1: pinMode(8, OUTPUT);
  }
  // display speed (binary):
  PORTD = speed << 2;
  //PORTD = DDRB << 1;
}



contact: nji(at)gmx.de