Arduino 101 meetup prep – day 8 – motor driving part 1

21May2009 – old stuff that may still be useful

Arduino_motor_driver 027

Motor drivers have arrived from Solarbotics and Adafruit Industries.  Rejoice!  Just in time for some midnight soldering (had an “oh-yeah…” moment when I remembered these are kits of the all-assembly-required type) before my Arduino 201 – Get Motorin’ meetup on Saturday.  That’s the L298 Compact Motor Driver (CMD) shown above, generously donated to support our meetup by the good folks at Solarbotics (and there’s another waiting on the bench to be assembled).

Arduino_motor_driver 018

The L298 CMD kit comes in a bag with straight-forward printed instructions that walk you through assembly step by step.  What is it about a shiny new PCB that makes it feel like Christmas in May?

Arduino_motor_driver 020

If you’ve ever worked with the L298 h-bridge chip itself sans-PCB then you know it’s got a whacky pinout that doesn’t want to work with your solderless breadboard.  The pins are sort of staggered between the two rows.  I think they’re on 0.10″ centres, but that doesn’t help for this chip, so you end up with some abominable apex-of-personal-shame kludge like this:

Arduino_motor_driver 036

These are LMD18200’s (for those zooming in), the single-bridge-per-chip big brother to the L298, but similarly pinout-challenged.  You can definitely not take this type of thing to the airport.  And even your co-workers shoot you concerned looks and keep their distance as this beast reverses current and magnetic flux in its mighty coils… but I digress.  The L298 loves a PCB like the one provided in the CMD kit.

This kit could easily be tackled by a beginner.  No fine-pitched soldering here.  Diodes go this way.  Electrolytic cap goes that way.  Shorter components before taller components and then you’re ready for a bench test.  Presto-changeo, 30 minutes of soldering (YMMV), and: 

Arduino_motor_driver 029

You’re all good, no solder bridges, ready for a bench test:

Arduino_motor_driver 031

as prescribed in the kit instructions.  I’m hooking up to a 12V lead acid gel cell salvaged from a UPS.  You can get 5V off the CMD, as it conveniently includes an LP2937 low-dropout regulator.  You just take the Enable line high (to 5V) for each bridge and then alternate HIGH (5V) and LOW (Ground) to each of the 4 inputs (2 per bridge).  You should see the green and red LEDs light up each in turn indicating which way current is flowing in the bridge.  This all looked good and nothing burned my finger during the touch-test, so…

Arduino_motor_driver 032

  I hooked the CMD up to my Duemilanove Arduino, and wrote a simple sketch to cycle the two bridges through “forward” and “reverse” cycles.  That seemed to work ok (LEDs flashing), so…

Arduino_motor_driver 034

I hooked one of the CMD bridges to this funky motor I pulled out of an ancient VCR.  Forward, backward, disabled, all worked.  I modified my sketch (below) to use PWM to ramp the speed up from  zero to full throttle.  It’s a snap: just set the direction by doing the digitalWrites() to CMD inputs L1 and L2 and then pulse the Enable line for that bridge with repeated calls to analogWrite().  The Arduino’s 500Hz default PWM frequency makes for some great audible whining sounds as the motor spins up.

So a big thanks to Solarbotics for the donation.  Come out to the meetup on Saturday and try this driver out for yourself.  I’ll whack the other kit together so we have a couple of them to share around and also bring in my Big Box of Motors.  Guess I should throw down a challenge (thanks Daryl) to see if anyone can figure out how to use the L298 Compact Motor Driver to drive a bipolar stepper motor I’ll provide.

Make: time.

Arduino code:

Demonstrate Solarbotics L298 Compact Motor Driver board
Author: Darin White
Date: 2009-05-20

int pinEnable1_2 = 11;
int pinL1 = 13;
int pinL2 = 12;

int pinEnable3_4 = 10;
int pinL3 = 8;
int pinL4 = 9;

void setup()                    // run once, when the sketch starts
  pinMode(pinEnable1_2, OUTPUT);      // sets the digital pin as output
  pinMode(pinL1, OUTPUT);
  pinMode(pinL2, OUTPUT);
  pinMode(pinEnable3_4, OUTPUT);
  pinMode(pinL3, OUTPUT);
  pinMode(pinL4, OUTPUT);

  // initialize output pins  
  digitalWrite(pinL1, LOW);
  digitalWrite(pinL2, LOW);
  digitalWrite(pinL3, LOW);
  digitalWrite(pinL4, LOW);
  digitalWrite(pinEnable1_2, HIGH);
  digitalWrite(pinEnable3_4, HIGH);

void loop()                     // run over and over again
    // "forward" both
    digitalWrite(pinL1, HIGH);
    digitalWrite(pinL2, LOW);
    digitalWrite(pinL3, HIGH);
    digitalWrite(pinL4, LOW);

    // "reverse" both
    digitalWrite(pinL1, LOW);
    digitalWrite(pinL2, HIGH);
    digitalWrite(pinL3, LOW);
    digitalWrite(pinL4, HIGH);
    // ramp speed on bridge 1 in forward direction
    digitalWrite(pinEnable1_2, LOW);  // turn off bridge 1
    digitalWrite(pinEnable3_4, LOW);  // turn off bridge 2
    digitalWrite(pinL1, HIGH); // forward
    digitalWrite(pinL2, LOW);  //  "
    for (int i=120;i<255;i++) {
        analogWrite(pinEnable1_2, i);
    // both bridges back to full on
    digitalWrite(pinEnable1_2, HIGH);
    digitalWrite(pinEnable3_4, HIGH);
This entry was posted in arduino and tagged , , . Bookmark the permalink.

One Response to Arduino 101 meetup prep – day 8 – motor driving part 1

  1. chris Bruner says:

    Thanks Darin, this was exactly what I was looking for. Then a familiar name came up.

Comments are closed.