Introduction: How to Control a Stepper Motor With an FPGA
This instructable will guide you through the process and some background knowledge on how to get a stepper motor working on an FPGA. Specifically a Nema Stepper Motor, with the Digilent Basys 3 and Digilent PmodSTEP.
Currently I'm deep in the process of making a claw game with the brain as an FPGA. The first step in this process was to figure out how to interface the FPGA with the stepper motor. Getting the stepper motor signals with the FPGA I knew how to do, but the Basys 3 output pins couldn't supply enough current for the stepper motor, so I chose to interface with the PmodSTEP, which, for all intents and purposes, is a power amplifier with a stepper motor connector.
In this process I learned that there isn't much out there on using the PmodSTEP, or using a stepper motor with an FPGA, at least that I could find. So I figured I would share what I learned.
Step 1: Gather Your Materials
Step 2: Put a Piece of Tape on the Motor
I find that it is much more satisfying and easier to detect if there is something moving on my motor. So I put a piece of tape on the shaft of my motor. You can do this if you want to, I highly suggest it, if nothing else because it made me feel better about my progress.
Step 3: Add Female Connectors to the Motor Wires - Optional
This step is another optional step, although highly suggested! wrapping the wires around pins can be a dubugging nightmare, so I put female headers on mine.
I tried to cut single male to female headers from a larger set, but ended up making a mess so I gave up. What was easier for me was to cut the ends off of MTE cables, solder them to the wires, and then shrink wrap the solder in the color of the wire. The shrink wrap is both so that the solder is secure, and so that the wires are the correct color at the end.
Step 4: Learn About How Stepper Motors Work - Optional
Previous to writing this I had really no knowledge of stepper motors, there was quite a steep learning curve.
This step is optional because to use this code you really don't need to know how stepper motors work, so if you just want to use it that's okay with me, but I wouldn't suggest it. If you want to modify it you'll need to understand how they work.
I found this video extremely helpful by Kevin Darrah. In it he controls the stepper motor with some buttons. This demonstrates the signals that need to be sent to move the motor in either direction.
Essentially stepper motors are controlled by two coils. You send a high and low signal to each side of the coils to activate them. To make the motor move you alternate which coil is activated and with which orientation.
To use the servo motor in full step mode, you activate one coil at a time, i.e. send a high signal to only one wire at a time.
Step 5: The Basic Idea Behind the Code
I find in FPGA design the easiest way for me to collect my thoughts and figure out how I'm going to control something is with a state diagram.
I knew from my research, that in order to drive the stepper motor in full step mode, I needed to activate one coil at a time, this meant one wire should be driven high at a time. So, in each of my active states, Sig4, Sig3, Sig2, and Sig1, the output is Signal, which contains the signal to each of the wires. In each of these states only one signal is driven. This means that one of the coils will be active.
Going between these active states depends on the direction. If I want to stay going the same direction, i.e. dir was equal to 1 and still is equal to 1, I go one direction through the states, if I want to change direction, i.e, dir changes from 1 to 0 I go the opposite direction in the states.
If I am in any of these active states and en, or enable turns off, I move to the Sig0 state where there is no signal high, so no coils are on, this will stop the motor. If enable is turned back on I return back to the active states.
The speed that the motor steps is the speed at which you move through these states, or the clock signal sent to this state machine.
Step 6: Download and Open the Vivado Project
Download the zip file attached to this step.
Unzip the file and open the vivado project. You can do this either by double clicking on the file or opening vivado and selecting open project.
Note: I used Vivado 2015.4 to create this project. If you have a different version installed I would suggest creating a new project in your version, then adding each of the source files.
You would need to add, pmod_step_interface.v, clk_div.v, pmod_step_driver.v, and claw_constraints.xdc.
Step 7: Generate the Bit File and Program Your Board
Once the file is open you can generate the bit file and program the board.
To generate the bit file double click generate bitstream.
To program the board open the hardware manager, open target and program the board.
Step 8: Plug the PmodSTEP Into the Basys3
Next you'll need to plug the Pmod step into the Basys3, I used Pmod header JC in my code so it will need to be plugged into that header.
Step 9: Plug the Motor Into the PmodSTEP
The motor I have is a bipolar stepper motor so it has four leads, like the one in the image on the left. So I will be using the 4 pin connector.
Plug in the leads to the 4 pin header in the order, black, red, green, blue. Your set up should look like the image in this step.
Flip SW1 to enable the motor, and you should see it begin to move.
Flip SW0 to change direction.