Matha Goram
Published © GPL3+

Rev Me Up

This note illustrates the introductory use of a very simple motor with minimal commands from an Arduino single board microcomputer.

BeginnerFull instructions provided30 minutes674
Rev Me Up

Things used in this project

Hardware components

Arduino Uno Rev3
Arduino Uno Rev3
×1
Elegoo L293D - Dual H-Bridge
×1
Elegoo DC Motor, hobby, small, with propeller
×1
Elegoo DuPont connection wires
×8
Breadboard (generic)
Breadboard (generic)
×1
Elegoo Baseplate
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

DC Motor Assembly Layout

A simple assembly for hobby DC motor driven by Arduino

Schematics

DC Motor Test Schematic

Schematic diagram for hobby DC motor driven by Arduino code.

Code

MotorDC-01.ino

C/C++
A basic set of tests to drive the rotation of hobby DC motors
/*
 * ElegooDCmotor-01.ino
 * A basic set of tests to drive a DC motor using L293D H-bridge interface
 * 2018-10-12
 * armw
 * v0.1
 * © 2018 <[email protected]> All Rights Reserved
 *  
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.  
 * 
 * Reference:
 * http://www.ti.com/lit/ds/symlink/l293.pdf
 * 1,2EN  1A  2A  Operation
 * H      L   H   Turn right
 * H      H   L   Turn left
 * H      L   L   Fast motor stop
 * H      H   H   Fast motor stop
 * L      X   X   Free-running motor stop (i.e. simplest)
 * H=high
 * L=low
 * X=any
 */
#define pinM1_12EN  9                   // enable motor 1 driver channels 1 & 2 (active high input)
#define pinM1_1A    5                   // motor 1 driver input, 1A, non-inverting
#define pinM1_2A    6                   // motor 1 driver input, 2A, non-inverting

const byte MAXSPEED = 255;              // maximum speed setting for motor in any direction
const byte MOTORS = 1;                  // number of motors in current exercise
int weepins[][3] = {                    // store pin configurations each motor in exercise for L293D use
  {pinM1_12EN, pinM1_1A, pinM1_2A},     // motor 1 (indexed as 0 in code)
  {0, 0, 0}                             // motor 2 (unused in current example)
  };                                    // pin definitions for motor(s) - only one in use in this example

void setup()
{
  for (byte i=0; i<MOTORS; i++)         // for each motor under test in this exercise
  {
    pinMode(weepins[i][0], OUTPUT);     // ENABLE pin for motor i
    pinMode(weepins[i][1], OUTPUT);     // A pin iA for motor i
    pinMode(weepins[i][2], OUTPUT);     // B pin iB for motor i
  }
  delay(1000);                          // 1,000 milliseconds
}

// the direction of rotation depends on the pin connections to the motor
void rotate(byte motorNumber, byte motorSpeed)
{
  digitalWrite(weepins[motorNumber][0],HIGH);   // turn right - depends on motor polarity
  digitalWrite(weepins[motorNumber][1], HIGH);  // per L293D truth table
  digitalWrite(weepins[motorNumber][2], LOW);
  analogWrite(weepins[motorNumber][0], motorSpeed);
}

// the direction of rotation depends on the pin connections to the motor
void counterRotate(byte motorNumber, byte motorSpeed)
{
  digitalWrite(weepins[motorNumber][0],HIGH);       // turn left - depends on motor polarity
  digitalWrite(weepins[motorNumber][1], LOW);        // per L293D truth table
  digitalWrite(weepins[motorNumber][2],HIGH);
  analogWrite(weepins[motorNumber][0], motorSpeed);
}

// several methods to stop the motor; this one is the simplest
void motorStop(byte motorNumber)
{
  digitalWrite(weepins[motorNumber][0], LOW);     // free-running motor stop; state of A pins do not matter
}

void loop()
{
  for (byte i=0; i<MOTORS; i++ )             // for future extensions with multiple motors
  {
    for (byte j=0; j<MAXSPEED; j=j+1)        // generic use of PWM range for pin (not needed for simple DC motor use)
    {
      rotate(i, j);                     // rotate motor i at speed j
      delay(500);                       // adjust to capture setting (external feedback needed) when torque overcomes intertia
    }
    delay(1000);                        // run for 1,000 ms
  
    motorStop(i);                       // essentially setting the corresponding ENABLE pin to LOW for motor i
    delay(1000);                        // stop for 1,000 ms
  
    for (byte j=0; j<MAXSPEED; j=j+1)   // generic use of PWM range for pin (not needed for simple DC motor use)
    {
      counterRotate(i, j);              // rotate motor i at speed j
      delay(500);                       // adjust duration to capture setting (external feedback needed) when torque overcomes intertia
    }
    delay(1000);                        // run for 1,000 ms
    motorStop(i);                       // essentially setting the corresponding ENABLE pin to LOW for motor i
    delay(1000);                        // stop for 1,000 ms
  }
}

Credits

Matha Goram
27 projects • 24 followers
Working with discrete electronic components for a very long time but still suffering from the occasional dry soldering results.

Comments