Arduino Nano and Visuino: Control Stepper Motor With Rotary Encoder

by BoianM in Circuits > Arduino

10688 Views, 49 Favorites, 0 Comments

Arduino Nano and Visuino: Control Stepper Motor With Rotary Encoder

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.

Components

575b9bb32e7fb600ff0001d6.jpeg
  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)

Connect the Stepper Motor to Arduino

575c728515be4dbfeb0005c1.jpeg
575c6cf34936d4a0430005aa.jpeg
575c6c8315be4d3ddb00028f.jpeg
575c6ce450e1b66706000601.jpeg
575c6f0815be4dbfeb0005ac.jpeg
  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 IN4 pins 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-Male Power(Red wire), to the Power(+) pin of the Stepper Driver Module (Picture 2)
  5. Connect the Female End of another Female-Male Power 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

Connect the Rotary Encoder to the Arduino

575c6d012e7fb600ff000544.jpeg
575c6d2250e1b66706000603.jpeg
5695929050e1b652510007ee.jpeg
575c724415be4dbfeb0005bf.jpeg
  1. Connect the Female end of a Female-Male Power(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.

Start Visuino, and Select the Arduino Board Type

SelectBoardTolsEdited.png
ArduinoNano.png

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

In Visuino: Add Stepper Motor Component

ComponentStepper.png
ComponentStepperProp1.png
  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)

In Visuino: Add Reversed Pin to the Stepper Motor Component

ComponentStepperProp2.png
ComponentStepperProp3.png
ComponentStepperProp4.png
  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)

In Visuino: Connect the Stepper Motor Component

ComponentStepperConnect1.png
ComponentStepperConnect2.png
ComponentStepperConnect3.png
  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)

Generate, Compile, and Upload the Arduino Code

GenerateCtopped.png
ArduinoIDE.png
  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)

And Play...

575c6cc445bcebedce0005d8.jpeg
VisuinoDiagramCrop.png
Arduino Nano and Visuino: Control Stepper Motor with Rotary Encoder

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