Arduino + Stepper Motor + IR

Using an Arduino & infrared receiver to control a stepper motor

Introduction

Stepper motors are useful when you need to precisely control the amount of rotation in a motor. In this tutorial, we'll use an Arduino to control a stepper motor, and we'll use an infrared recevier to remotely send control signals to the Arduino.

(IR) Infrared: a type of signal. The same signal used in typical television remote controls.


Arduino Code

What This Tutorial Covers

1. We'll be connecting an Arduino, Stepper Motor, Stepper Motor Driver, & IR Reciever.

2. We'll upload code to control the Arduino.

What You Need For Just The Tutorial

I bought everything from SparkFun cause they're down the block from me and they have some good tutorials.

Arduino Board $25. I'll be using an Arduino Uno.

Stepper Motor $7. I'll be using a small stepper motor.

Easy Driver $15. This is another controller that helps driver the stepper motor.

IR Reciever $2. This is the diode that will recieve infrared signals.

IR Remote $2. This is the infrared remote for sending signals. It's the same as a typical television remote control.

There are other ways to connect everything, but I'll be using the following for connections:

Jumper Wires $2. I'll be using jumper wires, which means we'll be doing some soldering.

Breadboard $6. I'll be using a breadboard to help with connections.

USB A to B Cable $25. The USB cable is to connect the Arduino Uno to a computer. You might need a different cable if you're using a different Arduino.

Total Cost: $120

What You Should Already Know

You should know how to solder. The Easy Driver requires soldering. I use this one: Cordless Soldering Iron

You should understand how a breadboard works.

You should be familiar with the Arduino IDE.


Easy Driver

Stepper motors require a lot of extra stuff for precise control (check this explanation out). The Easy Driver handles all that extra stuff for us. You can find information about the Easy Driver here: Easy Driver

box image

You can connect the Easy Driver however you want. I soldered jumper wires to it.


Easy Driver → Stepper Motor

Connecting the stepper motor to the easy driver is pretty straight forward. Just connect them in the same order from left to right.

Easy Driver Pin Stepper Motor Pin
A (Left) Outer Left (Green)
A (Right) Inner Left (Red)
B (Left) Inner Right (Orange)
B (Right) Outer Right (Yellow)

Easy Driver → Arduino

Connect the following Easy Driver pins to the Arduino pins.

Easy Driver Pin Arduino Pin
Enable 6
MS2 5
MS1 4
STEP 2
DIR 3
GND (BOTTOM RIGHT) GND (ANY)

IR Receiver → Arduino

I'll be referring to the IR Recevier as though facing the rounded side. You can connect the IR receiver using a breadboard and jumper wires.

box image

Connect the following IR pins to the Arduino.

IR Receiver Pin Arduino Pin
Left (facing the rounded side) 11

IR Receiver → Easy Driver

We'll be using the voltage regulator on the Easy Driver to power the IR receiver. This is because the IR receiver can only handle up to 6 volts, but we'll be powering the Easy Driver with 12 volts.

IR Receiver Pin Easy Driver Pin
Middle (facing the rounded side) GND (BOTTOM LEFT)
Right (facing the rounded side) +5V

Power

The Easy Driver will be sending power to the stepper motor and the IR receiver, so we only need to figure out power for the Easy Driver and the Arduino. For the sake of simplicity, I'll just be powering them both through the usb power connection in the Arduino.

Arduino Pin Breadboard
5V Inner Bus strip
GND Outer Bus strip
Easy Driver Pin Breadboard
M+ Inner Bus strip
GND (TOP) Outer Bus strip

Finally, connect the A to B USB cable from your computer to the Arduino. Everything should power up and you should LEDs light up on the Arduino and Easy Driver.


Arduino Libraries

To get the IR receiver to work, we need to import one library. Here is a copy of the IR Library that I use. You may want to download the most recent version at their GitHub site. If you don't know how to install a library, read this link: Installing Arduino Library.


Arduino Code

Open your Arduino IDE.

From the menu, select "Tools" and then "Board". Then choose your Arduino board. In my case, it is "Arduino/Genuino Uno".

From the menu, select "Port". Then choose the port to your board. I'm on a Mac so mine looks something like "/dev/cu.usbmodemFA131". I've heard that it looks different on other systems.

From the menu, create a new project by selecting "File" and then "New".

Finally, just paste in the code below into your IDE and then push arrow button (→) in the top left to upload the code.

copy
#include <IRremote.h>

/* define how long motor should run for */
#define STEPCOUNT  1000

/* define whether debugging will be on */
#define DEBUG 0
#define PRINTCODES 0

/* define pins */
#define STP   2   // alternate to make spin
#define DIR   3   // set direction of spin
#define MS1   4   // sets rotation degree
#define MS2   5   // sets rotation degree
#define EN    6   // enables or dsiables motor
#define RECV  11  // ir receiver pin receives code

/* define ir remote button codes */
#define btn_PWR     284153895
#define btn_A       284162055
#define btn_B       284129415
#define btn_C       284121255
#define btn_UP      284139615
#define btn_DOWN    284098815
#define btn_RIGHT   284131455
#define btn_LEFT    284102895
#define btn_CENTER  284106975

/* stepper motor variables */
int x;

/* ir variables */
IRrecv irrecv(RECV);
decode_results irsignal;

void DebugPrint(String str)
{
  if(DEBUG) {
    Serial.println(str);
  }
}

void ClockWise()
{
  DebugPrint("ClockWise");
  
  digitalWrite(DIR, LOW);
  for(x = 1; x < STEPCOUNT; x++)
  {
    digitalWrite(STP,HIGH);
    delay(1);
    digitalWrite(STP,LOW);
    delay(1);
  }
}

void CounterClockWise()
{
  DebugPrint("CounterClockWise");
  
  digitalWrite(DIR, HIGH);
  for(x = 1; x < STEPCOUNT; x++)
  {
    digitalWrite(STP,HIGH);
    delay(1);
    digitalWrite(STP,LOW);
    delay(1);
  }
}

void SetEighthStep()
{
  DebugPrint("SetEighthStep");
  
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, HIGH);
}

void SetQuarterStep()
{
  DebugPrint("SetQuarterStep");
  
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, HIGH);
}

void SetHalfStep()
{
  DebugPrint("StepHalfStep");
  
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, LOW);
}

void SetFullStep()
{
  DebugPrint("StepFullStep");
  
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, LOW);
}

void resetEDPins()
{
  DebugPrint("resetEDPins");
  
  digitalWrite(STP, LOW);
  digitalWrite(DIR, LOW);
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(EN, HIGH);
}

void setup() {
  pinMode(STP, OUTPUT);
  pinMode(DIR, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(EN, OUTPUT);
  resetEDPins(); // Set step, direction, microstep and enable pins to default states

  if(DEBUG) {
    Serial.begin(9600); // Open Serial connection for debugging
  }

  irrecv.enableIRIn();
}

void loop() {  
  if(irrecv.decode(&irsignal)) {

    if(PRINTCODES) {
      Serial.println(irsignal.value);
    }
      
    digitalWrite(EN, LOW); // enables motor
    
    if (irsignal.value == btn_A)
    {
      SetEighthStep();
    }
    else if(irsignal.value == btn_B)
    {
      SetQuarterStep();
    }
    else if(irsignal.value == btn_C)
    {
      SetHalfStep();
    }
    else if(irsignal.value == btn_PWR)
    {
      SetFullStep();
    }
    else if(irsignal.value == btn_RIGHT)
    {
      ClockWise();
    }
    else if(irsignal.value == btn_LEFT)
    {
      CounterClockWise();
    }
    else
    { /* do nothing */ }

    digitalWrite(EN, HIGH); // disables motor

    irrecv.resume(); // Receive the next value
  }

  delay(100);
}

I think the code is fairly self explanatory. You should edit the following variables if needed.

STEPCOUNT: The number of steps the motor should rotate per button click.

DEBUG: If set to 1, the serial monitor in the Arduino IDE will print messages.

PRINTCODES: If set to 1, the serial monitor in the Arduino IDE will any IR codes received. Could be useful if you're using a different remote and you need to see what codes the button on your remote is sending.


Time To Play!

If everything is successful, your set up should be ready and you can click the buttons on the IR remote to make the stepper motor spin.

IR Button What It Does
Top Power Sets Motor To Full Step
A Sets Motor To Eighth Step
B Sets Motor To Quarter Step
C Sets Motor To Half Step
LEFT Spin Motor Counter Clockwise
RIGHT Spin Motor Clockwise
UP nothing...
DOWN nothing...
CENTER nothing...

Here's a video of it working. It'll blow your mind, lol.