Introduction: ​Driving DC Motors With Inputs Using the SparkFun Motor Driver

This is the thirteenth module in a series of lessons regarding applications and use of the Arduino 101. This project is focused on the use of the SparkFun Motor Driver to drive up to 2 DC Motors and is essential in any basis mobile robotics projects.

This is an alternative way of driving motors without the use of an NPN transistor, but instead using a breakout board designed for driving 2 DC motors

Step 1: Tools and Materials

Step 2: Circuitry

Connecting power from the Arduino to the breadboard

  • We will need two sources, one with 5V and the other at 3.3V, so we will be using both sides of the ground rails of the breadboard in this project. For convention and as an organizational tool, all 5V sources will be marked with red and black jumper wires. While, the 3.3V sources are marked with orange and brown jumper wires.
  • Connect the 5V pin from the Arduino to one of the red power rails on the breadboard using a red jumper wire.
  • Connect the 3.3V pin from the Arduino to the other red power rail on the breadboard using an orange jumper wire.
  • Connect the GND from the Arduino to the 5V black ground rail on the bread board using a black jumper wire.
  • Connect the 5V black ground rail to the 3.3V black ground rail using a brown jumper wire. Ground is ground so it doesn't make a difference which ground we use.

Wiring the Motor driver

  • It is easiest to follow the wiring diagram for the motor driver since the pin names are on the opposite side facing the breadboard.
  • First, connect all the power and ground for the board to 3.3V. Then connect the power and ground designated the motor (on the left side) to 5V.
  • Then, connects pin 4, 5, and 6 from the Arduino to the motor driver board on the breadboard.

Wiring the motor

  • Connect the motor pins to the 2nd last and 3rd last pin on the left side of the board. The order does not matter. This will be supplied 5V by the motor driver board.

Step 3: Code

const int DIR_A = 5;

const int DIR_B = 4; const int PWM = 6;

void setup() { //set all pins as output pinMode(DIR_A, OUTPUT); pinMode(DIR_B, OUTPUT); pinMode(PWM, OUTPUT); }

void loop() { //drive forward at full speed by pulling DIR_A High //and DIR_B low, while writing a full 255 to PWM to //control speed digitalWrite(DIR_A, HIGH); digitalWrite(DIR_B, LOW); analogWrite(PWM, 255);

//wait 1 second delay(1000);

//Brake the motor by pulling both direction pins to //the same state (in this case LOW). PWM doesn't matter //in a brake situation, but set as 0. digitalWrite(DIR_A, LOW); digitalWrite(DIR_B, LOW); analogWrite(PWM, 0);

//wait 1 second delay(1000);

//change direction to reverse by flipping the states //of the direction pins from their forward state digitalWrite(DIR_A, LOW); digitalWrite(DIR_B, HIGH); analogWrite(PWM, 150);

//wait 1 second delay(1000);

//Brake again digitalWrite(DIR_A, LOW); digitalWrite(DIR_B, LOW); analogWrite(PWM, 0);

//wait 1 second delay(1000); }

Step 4: Demo

The motor will spin then stop for a few seconds, and then spin again with similar results and control as an NPN transistor, although more convenient since the board is already made by Spark Fun.

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017