NEMA 17 Stepper Motor - Using Encoder for Controlling Speed & Direction With Arduino

by RonFrtek in Circuits > Arduino

1115 Views, 4 Favorites, 0 Comments

NEMA 17 Stepper Motor - Using Encoder for Controlling Speed & Direction With Arduino

NEMA 17 Stepper Motor - Using Encoder for Controlling Speed & Direction With Arduino

In this tutorial we will learn how to use encoder to control the speed and direction of the stepper motor Nema 17 using arduino.

Watch the video!

What You Will Need

NEMA17.jpg
AD267-2.jpg
A4988.png
A4988 expansion board drv 8825 stepper motor.jpeg
VisuinoAdvrtisemenESPNowESP32C3-1.jpg
power-supply.png
breadboard.png
F5OFEUQJJ4ZYFKY.LARGE.jpg
FZEK4WBK3XGYAP0.LARGE.jpg
  • Arduino UNO (Or any other Arduino)
  • Stepper motor NEMA 17
  • A4988 Stepper Motor Driver or (DRV8825)
  • DRV8825/A4988 Stepper Driver Expansion Module
  • Encoder module
  • Jumper wires
  • Breadboard
  • Power Supply 12V
  • Visuino program: Download Visuino


Brought by PCBWay

FHUQHJ6LF9Y8DUL.png
2022-08-26_13-41-18.jpg

Thank you PCBWay for supporting this tutorial and helping users learn more about electronics.

NEW! Now you can get Aluminum PCB & FLEX PCB in their Special Offer!

What I like about the PCBWay is that you can get 10 boards for approximately $5 which is really cost effective for professional made boards, not to mention how much time you save!

Go check them out here. They also offer a lot of other stuff in case you might need it like assembly3D printingCNC machining and a lot more.

The Circuit

2023-05-10_16-22-54.png
2023-05-10_16-26-58.png
  • Arduino Digital Pin 2 will be used for Steps
  • Arduino Digital Pin 3 will be used for Motor Direction

If using a Stepper Motor Driver Shield:

  • Connect Motor Shield GND pin to Arduino negative pin [GND]
  • Connect Motor Shield [5V] pin to Arduino positive pin [5V]
  • Connect Motor Shield GND pin to Power Supply negative pin [GND]
  • Connect Motor Shield [9V] pin to Power Supply positive pin [+]
  • Connect Motor Shield pin[S] to Arduino digital pin [2]
  • Connect Motor Shield pin[D] to Arduino digital pin [3]
  • Connect stepper motor as shown on the picture.


If using a Stepper Motor Driver 8825:

  • Connect DRV8825 GND pin to Arduino negative pin [GND]
  • Connect DRV8825 DIR pin to Arduino digital pin [3]
  • Connect DRV8825 STEP pin to Arduino digital pin [2]
  • Connect Power Supply for the motor to DRV8825 VMOT and GND
  • Connect Capacitor across VMOT and GND
  • Connect stepper motor as shown on the picture.

Connecting Encoder module:

  • Connect Encoder module pin [CLK] to Arduino digital pin [5]
  • Connect Encoder module pin [DT] to Arduino digital pin [6]
  • Connect Encoder module pin [SW] to Arduino digital pin [7]

Start Visuino, and Select the Arduino UNO Board Type

select-board-uno.jpg
Visuino-Select-Board-UNO.jpg

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

In Visuino Add Components

2023-05-08_20-12-47.png
2023-05-08_20-14-15.png
2023-05-08_20-16-50.png
2023-05-08_20-17-33.png
2023-05-08_20-21-32.png
  • Add "Multiply Integer By Value" component
  • Add "Rotary Encoder Sensor" component
  • Add "Pulse Generator" component
  • Add "Toggle(T) Flip-Flop" component
  • Add "Debounce Button" component

In Visuino Set Components

2023-05-08_20-23-17.png
2023-05-08_20-24-34.png
2023-05-08_20-24-49.png
  • Select "MultiplyByValue1" and in the Properties window set "Value" to 50
  • Select "PulseGenerator1" and in the properties window set "Frequency" to 2000 << this will be the default starting speed of the stepper motor, you can adjust it to your needs
  • select "Frequency" and click on the Pin icon and select "Float SinkPin"


In Visuino Connect Components

2023-05-08_20-32-18.png
2023-05-08_20-32-36.png
stepper-encoder.png
  • Connect Arduino digital pin [6] to "RotaryEncoderSensor1" pin [Direction]
  • Connect Arduino digital pin [5] to "RotaryEncoderSensor1" pin [Clock]
  • Connect "RotaryEncoderSensor1" pin [Out] to "MultiplyByValue1" pin [In]
  • Connect "MultiplyByValue1" pin [Out] to "PulseGenerator1" pin [In]
  • Connect "PulseGenerator1" pin [Out] to "TFlipFlop1" pin [Clock]
  • Connect "TFlipFlop1" pin [Out] to Arduino digital pin [2]
  • Connect "Button1" pin [Out] to "TFlipFlop2" pin [Clock]
  • Connect "TFlipFlop2" pin [Out] to Arduino digital pin [3]

Generate, Compile, and Upload the Arduino Code

FBR42I2KXBU01YW.jpg

In Visuino, at the bottom click on the "Build" Tab, make sure the correct port is selected, then click on the "Compile/Build and Upload" button.

Play

If you power the Arduino module the motor will do will start to run and if you rotate the Encoder module the speed of the motor will change and if you press a button on the Encoder module the motor will change the direction.

Congratulations! You have completed your project with Visuino. Also attached is the Visuino project, that I created for this Instructable, you can download it here and open it in Visuino: https://www.visuino.eu