Introduction: Arduino Nano and Visuino: Control Stepper Motor With Rotary Encoder

Sometimes it is necessary to have a Stepper Motor follow the rotation of a Rotary Encoder for precise positioning. I have been planning Instructable on this for a long time, but finally couple of days ago after yet another question by Visuino user, on how to do this, I decided that the time is now.

In this Instructable, I will show you how easy it is to connect a Stepper Motor and Rotary Encoder to Arduino and program with Visuino the Stepper Motor to follow the rotations of the Rotary Encoder.

Step 1: Components

  1. One Arduino compatible board (I use Arduino Nano, because I have one, but any other will be just fine)
  2. One Rotary Encoder module I got from this cheap 37 sensors set
  3. One 5V Stepper Motor with Driver Board (I used 28BYJ-48 stepper with ULN2003 driver board )
  4. One small Breadboard (Any breadboard can be used, or any other way to connect 3 wires together)
  5. 8 Female-Female jumper wires
  6. 3 Male-Female jumper wires (Red)

Step 2: Connect the Stepper Motor to Arduino

  1. If not already connected, plug the Stepper Motor connector into the Driver Board
  2. Connect one end of Female-Female jumper wires(Blue, Green, Yellow and Orange wires) to the IN1 to IN4pins of the Stepper Driver(Picture 1)
  3. Connect Female-Female Ground(Black wire), to the Ground(-) pin the Stepper Driver Module (Picture 2)
  4. Connect the Female end of a Female-MalePower(Red wire), to the Power(+) pin of the Stepper Driver Module (Picture 2)
  5. Connect the Female End of another Female-MalePower wire(Red wire) to the 5V power pin of the Arduino board (Picture 3)
  6. Connect the other end of the Ground wire(Black wire) to Ground pin of the Arduino board (Picture 3)
  7. Connect the IN1 wire(Blue wire) to the Digital pin 2 of the Arduino board (Picture 4)
  8. Connect the IN2 wire(Green wire) to the Digital pin 3 of the Arduino board (Picture 4)
  9. Connect the IN3 wire(Yellow wire) to the Digital pin 4 of the Arduino board (Picture 4)
  10. Connect the IN4 wire(Orange wire) to the Digital pin 5 of the Arduino board (Picture 4)
  11. Picture 5 shows where are the Ground, 5V Power, and Digital 2 to Digital 5 pins of the Arduino Nano

Step 3: Connect the Rotary Encoder to the Arduino

  1. Connect the Female end of a Female-MalePower(Red wire) to the power pin of the Rotary Encoder Module (Picture 1)
  2. Connect Ground(Black wire), Direction(Purtle wire), and Clock(Gray wire) to the Rotary Encoder Module (Picture 1)
  3. Connect the other end of the Power wire(Red wire) to the 5V power pin of the Arduino board(Picture 2)
  4. Connect the other end of the Ground wire(Black wire) to Ground pin of the Arduino board(Picture 2)
  5. Connect the Clock wire(Gray wire) to Digital pin 6 of the Arduino board(Picture 2)
  6. Connect the Direction wire(Purple wire) to Digital pin 7 of the Arduino board(Picture 2)
  7. Connect the Male ends of the 3 Power wires(Red wires) - from the Rotary Encoder Module, the Stepper Motor Driver, and the Arduino together as example with the help of a Breadboard (Picture 3) - In my case I used a small Breadboard
  8. Picture 4 shows in Red where are the Ground, Digital 6 and Digital 7 pins of the Arduino Nano. In Blue are shown the pins connected in the previous step.

Step 4: Start Visuino, and Select the Arduino Board Type

To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/.

Please be aware that there are some critical bugs in Arduino IDE 1.6.6.

Make sure that you install 1.6.7 or higher, otherwise this Instructable will not work!

The Visuino: https://www.visuino.com also needs to be installed.

  1. Start Visuino as shown in the first picture
  2. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino
  3. When the dialog appears, select "Arduino Nano" as shown in Picture 2

Step 5: In Visuino: Add Stepper Motor Component

  1. Type "step" in the Filter box of the Component Toolbox then select the "4 Wire Stepper Motor" component (Picture 1), and drop it in the design area
  2. In the Object Inspector set the value of the "Enabled" property to "False" (Picture 2)

Step 6: In Visuino: Add Reversed Pin to the Stepper Motor Component

  1. In the Object Inspector click on the Pin button at front of the Reversed property(Picture 1), and select "Boolean SinkPin" (Picture 2)
  2. A new "Reversed" Pin will appear on the component (Picture 3)

Step 7: In Visuino: Connect the Stepper Motor Component

  1. Click in the "Out" box containing the pins of the Stepper component to start connecting all the Out pins at once (Picture 1)
  2. Move the mouse over the "Digital" input pin of the "Digital[ 2 ]" channel of the Arduino component. The Visuino will automatically spread the wires so they will connect correctly to the rest of the pins(Picture 1)
  3. Connect the "Out" output pin of the "Digital[ 6 ]" channel of the Arduino component to the "Reversed" input pin of the Stepper1 component (Picture 2)
  4. Connect the "Out" output pin of the "Digital[ 7 ]" channel of the Arduino component to the "Step" input pin of the Stepper1 component (Picture 2)

Step 8: Generate, Compile, and Upload the Arduino Code

  1. In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
  2. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)

Step 9: And Play...

Congratulations! You have completed the project.

Picture 1 and the Video show the connected and powered up project.

If you rotate the Rotary Encoder, the Stepper Motor will rotate in the same direction as the encoder as you can see from the Video.

On Picture 2 you can see the complete Visuino diagram.

Also attached is the Visuino project, that I created for this Instructable. You can download and open it in Visuino: https://www.visuino.com