SIK Experiment Guide for the Arduino 101/Genuino 101 Board

Contributors: D___Run___
Favorited Favorite 6

Experiment 13: Motor Driver with Inputs


In Experiment 12 you used the Motor Driver board to control a motor's direction and speed. The issue is that you had to hard code the direction and speed of your motor. Most applications that make use of a motor allow the user to control the speed and direction of the motor, much as you would your own car. In this experiment we will add two inputs and use them to control the direction and speed of your motor.

Are you ready to get your motor running? Let's go!

Parts Needed

You will need the following parts:

  • 1x Breadboard
  • 1x Arduino 101 or Genuino 101 board
  • 1x SPDT switch
  • 1x 10K potentiometer
  • 1x SparkFun Motor Driver Board
  • 1x 48:1 ratio Gearmotor
  • 20x Jumper Wires

Didn't Get the SIK?

If you are conducting this experiment and didn't get the SIK, we suggest using these parts:

Hobby Gearmotor - 140 RPM (Pair)

Hobby Gearmotor - 140 RPM (Pair)

Breadboard - Self-Adhesive (White)

Breadboard - Self-Adhesive (White)

Jumper Wires - Connected 6" (M/M, 20 pack)

Jumper Wires - Connected 6" (M/M, 20 pack)

Trimpot 10K Ohm with Knob

Trimpot 10K Ohm with Knob

Mini Power Switch - SPDT

Mini Power Switch - SPDT


SparkFun Motor Driver - Dual TB6612FNG (1A)

18 Retired

You will also need either an Arduino 101 OR Genuino 101 board.

Arduino 101

9 Retired

Genuino 101


Hardware Hookup

Ready to start hooking everything up? Check out the wiring diagram below to see how everything is connected.

Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction.

Wiring Diagram for the Experiment

alt text

Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.

Open the Sketch

Open the Arduino IDE software on your computer. Coding in the Arduino language will control your circuit. Open the code for Circuit 13 by accessing the “SIK Guide Code” you downloaded and placed into your “Examples” folder earlier.

To open the code go to: File > Examples > SIK Guide Code > Circuit_13

You can also copy and paste the following code into the Arduino IDE. Hit upload, and see what happens!

SparkFun Inventor's Kit 
Example sketch 13

SparkFun Motor Controller with Inputs

  Use the inputs to manually set the direction and speed of a motor.

This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit for SIK information.
Visit to learn more about Arduino.
 //define the two direction logic pins and the speed / PWM pin
 const int DIR_A = 5;
 const int DIR_B = 4;
 const int PWM = 6;

 //define the input pins
 const int switchPin = 10;
 const int potPin = 0;

 void setup()
  //set all pins as output
  pinMode(DIR_A, OUTPUT);
  pinMode(DIR_B, OUTPUT);
  pinMode(PWM, OUTPUT);
  //set the switchPin as INPUT
  pinMode(switchPin, INPUT);

void loop()
//read the value from the potentiometer and divide
//it by 4 to get a 0-255 range. Store the value in
//the speed variable
int speed = analogRead(potPin) / 4;

 //read the value of the switch and store it in the
 //direction variable.

//if the value of direction is HIGH drive forward at
//a speed set by the speed variable, else drive reverse
//at a speed set by the speed variable.
if (digitalRead(switchPin) == HIGH)

//create a custom function that defines moving forward
//the forward() function accepts one parameter and that is
//the speed at which you want to drive forward (0-255)
 void forward(int spd)
 //motor contoller direction pins set to forward
 digitalWrite(DIR_A, HIGH);
 digitalWrite(DIR_B, LOW);

  //write the speed by using the parameter of spd
 analogWrite(PWM, spd);

//create a custom function that defines moving in reverse
//the reverse() function accepts one parameter and that is
//the speed at which you want to drive in reverse (0-255)
void reverse(int spd)
//set motor controller pins to reverse
digitalWrite(DIR_A, LOW);
digitalWrite(DIR_B, HIGH);

 //write the speed by using the parameter of spd
 analogWrite(PWM, spd);

These big, scary functions take a single value as a parameter: speed. Each function then accepts that value and applies it to the analogWrite() function inside of the custom function. Custom functions are a great way to clean up your code and also make it more modular and useful in other applications. Watch out! You are halfway to writing your own library.

What You Should See

You should be able to control the motor's direction by flipping the SPDT switch and then the speed through the potentiometer. Go ahead and play with both inputs to make sure they both work and the motor is responding to those inputs.

alt text


Motor Only Spins in One Direction

Double check the wiring of your switch, but also double check your if() statement to make sure there isn't a semicolon after the statement.

Also, double check to make sure that you have the standby pin wired correctly (to 3.3V).