In order to cut the dough into shapes, my project uses an XY plotter type of set up. The platform will move back and forth as the Y axis and the cutting head will sit above the platform and move across the platform perpendicular to the Y axis motion. These both need to be able to move to exact positions. Typically in XY plotter projects like mine, stepper motors are used to get this precision.


Stepper motors come in a few different varieties; I chose to use a four wire bipolar motor.


Nema17 Stepper Motor


As I understand it, these motors have different coil windings that create electromagnetic fields. My motor has 4 wires, which means two coils. Each time a coil is turned on, the motor rotates to align with that coil. So by alternating how each coil is turned on, the motor will end up going in a circle one step at a time. Wikipedia has a great animation of this. Using some tricky powering, these motors can turn in smaller increments than one step. This is called micro-stepping.


In looking up how to control stepper motors with a PSoC, I found project #045 which used 3 PWM blocks to control one motor. I thought that was more complicated than it needed to be, especially since I wanted to control 3 motors independently. I figured that there's a more convenient way, probably using a stepper motor driver chip. After searching around I decided to buy some A4988 Stepstick stepper motor driver breakout boards to control my motors. This article from How to Mechatronics was a great help in understanding how to use them.


A4988 stepstick stepper motor driver breakout board

A4988 stepstick stepper motor driver breakout board bottom side

These boards use Step and Direction input pins to control the motor. The direction can be changed by setting the direction pin high or low, and the step pin moves the stepper motor one step for each pulse sent to it. The sleep and reset pins are activated with a low signal. In order to have the board enabled, these pins have to be high. The sleep pin has a pull up resistor (which makes its default setting high), so connecting them together allows the board to be enabled. The MS1, MS2, and MS3 pins control if the motor moves in full step or micro stepping mode. It uses a binary code to choose the stepping mode. Since I'm going to do full steps I don't need to connect these pins. The enable pin is also active low and by setting it high I can choose when there is power on the motors, but for the first test, I just left it enabled. Vmot and the gnd pin next to it are the connections for the motor's power supply. This power supply will need a capacitor in between the voltage and ground in order to protect the board against power spikes. I'm using a 10uF capacitor. Since my capacitor is electrolytic, I've properly connected the negative side to the ground pin. 2B, 2A, 1B, 1A are the pins that actually connect to the motor's wires. As I learned from this website, my motors have this color scheme for the wires: red and blue connect to one coil (A) and green and black connect to the other (B).  If the wires are not connected properly, the motor may get damaged. VDD and gnd are the pins that connect to the micro-controller's power (3.3V or 5V).

Here's my set up.


A4988 stepstick driver connected to PSoC4


Using the A4988 board I can set the direction and then send a PWM signal to the step pin to move the motor. My motors are NEMA17 and take 200 steps to complete a full resolution. By measuring how many pulses I send I can stop the motor when it's at the correct position. This requires only one PWM block per motor. I felt like that simplified things considerably.


In trying to set up the control from the PSoC I couldn't find any helpful solutions online, so I had to work on creating my own. I wanted to set the direction and number of pulses, turn on the PWM, and then turn off the PWM when the pulses are done. I first tried implementing this in software, using an interrupt to signal when the pulses were done, but it's been years since I wrote an interrupt service routing (ISR) and I'm better at figuring out hardware. So I decided to try a hardware solution instead. This is what I came up with.

PSoC4 Motor Control Circuit

In software, the direction pin is first set, and then the control register is given the number of pulses that will be sent. The PWM is also enabled in software and each pulse sends an ouput into the clock of the 8-bit counter. This counter is always enabled, so it just counts on each "clock" signal. When that count reaches the same as the control register value, the compare sends a high signal, which then becomes inverted and subsequently disables the PWM, effectively allowing the motor to run for an exact number of steps. Since the control register is only  8 bits, this only works with steps less than 256. (The code is attached.)


PSoC CY8CKIT-049-42xx stepper motor set up


I hooked everything up, booted the program onto my PSoC and tested it out!




so I tried again.


It smelled a little like something was burning, but I couldn't see anything wrong... Then I couldn't get the computer to recognize my PSoC. And after some puzzling I realized that I had accidentally connected the motor's 12v power supply to the same side of the breadboard as the PSoC power to the A4988! I completely burned up my microcontroller! And probably my A4988 too. sheesh.

I guess I learned my lesson. Be extremely careful about where you put the power!