Sam Ade
Sam Ade

Reputation: 21

Stepper Motor Control with DM320T, Raspberry Pi 3B and MATLAB

I need help troubleshooting my stepper motor control setup. I am trying to rotate a stepper motor a certain angle at a given speed. I connected and program the setup as follows but I can't get the stepper motor to move. I suspect it is my code, however, I found little coding resource online about this. I would appreciate any help and/or suggestions.

Connections and Wiring:

Connections and Wiring

MATLAB Program:

clear all;  clc;
MainRasp = raspi('192.168.1.134', 'pi','raspberry');
ENA = 4;
DIR = 17;
PUL = 18;

configurePin(MainRasp, PUL, 'DigitalOutput');
configurePin(MainRasp, DIR, 'DigitalOutput');
configurePin(MainRasp, ENA, 'DigitalOutput');

writeDigitalPin(MainRasp, PUL, 1);
writeDigitalPin(MainRasp, DIR, 0);
writeDigitalPin(MainRasp, ENA, 1);

angle = 40;
steps_per_rev = 100;
micro_step = 8;

angle_per_step = 360/(steps_per_rev*micro_step);
numSteps = floor(angle/angle_per_step);

for step = 1:numSteps
    writeDigitalPin(MainRasp, PUL, 1);
    pause(0.001);
    writeDigitalPin(MainRasp, PUL, 0);
    pause(0.001);    
end

UPDATE: My connections and program are right. I found out that the stepper motor driver (DM320T) I used requires a digital signal voltage of 4-5v (for High) for PUL. However, the raspberry pi digital pin doles out a 0-3.3v. In effect, it was still read by the DM320T as a Low.

As a result, I switched to an Arduino Micro which has a digital pin voltage of 0-5v and capable of providing the requirement for PUL.

Thanks to everyone that commented.

Upvotes: 2

Views: 1347

Answers (1)

rpdrewes
rpdrewes

Reputation: 147

Quick summary: this would work if you connected the Pi's 3.3V power rail to the OPTO pin on the 320T, instead of the Pi's 5V power to the OPTO pin on the 320T. Furthermore, connecting the 5V pin in the way you did could damage the Pi because it could lead to > 3.3V on the Pi GPIO pins. When you had OPTO connected to the Pi's 5V power rail, the Pi's GPIO "high" output of about 3.2V was apparently not high enough to stop current flow through the DM320T's opto-isolator (5V - 3.2V is enough to power the light emitting diode) and therefore there was no way for the Pi's GPIO to turn the opto-isolator off even with a high output. But when OPTO is only 3.3V, the high GPIO output prevents current flow through the opto-isolator, but the low GPIO output allows it!

Long answer:

I have used a Pi Zero W with nominal 3.3V GPIO outputs to control a stepper motor with a DM320T board (sold by StepperOnline) in two ways. The first way involves no external components, the second way involves an additional opto-isolator.

Initial comment: I do not recommend connecting the grounds between the motor power side and the opto-isolated logic side. This is not needed and could defeat the purpose of the opto-isolation, depending on how your power supplies are set up. So get rid of that. If the motor power supply and Pi power supply already share a ground, then this caution does not apply, but in general there is no reason to share this ground.

Second comment: The Pi can be damaged if inputs to GPIOs go above 3.3V. Connecting the +5 to OPTO in the way you have shown could cause damage to the Pi because the voltage seen on PUL, ENA, and DIR, which are connected to Pi GPIO pins, is basically 5V minus the voltage drop across the internal opto-isolator diodes in the 320T, and this is over 4V in my tests. Once current flows into the GPIOs the voltage will drop due to the internal resistors in the 320T, but this is still not a safe situation and could damage the Pi.

FIRST METHOD, NO ADDITIONAL COMPONENTS: Make sure no ground is shared between logic side and motor power side on the 320T. Connect the 320T OPTO to the Pi 3.3V supply (e.g. pin 1). Connect the 320T PUL to a GPIO on the Pi, and pulse that output on the Pi at some reasonable rate. The motor should spin even with DIR and ENA on the 320T unconnected since ENA defaults to enabled. Test this first and verify it is working. NOTE: a low output on the Pi GPIO causes current flow through the 320T's opto-isolator and is considered "on" by the 320T. So essentially the sense of the pulse is reversed. That should not be a problem but be aware of it when constructing your pulses. Though the 320T wants a 5V power on the logic power input OPTO, the samples I have tried do work with the Pi's 3.3V power input. It is quite possible that some other 320T you find will not work in this configuration, and you will have to do the alternate method, below. But based on my examination of the circuit, it should generally work reliably with the Pi's 3.3V input to the OPTO on the 320T.

SECOND METHOD, WITH EXTERNAL COMPONENT: With additional 817 opto-isolaters, it is simple to power the 320T logic side power input (OPTO) with +5 from the Pi and sink the PUL, ENA, and DIR lines of the 320T to the output side of separate 817's (pin 4 to the PUL, ENA, or DIR, and pin 3 to logic side ground). Connect separate GPIO outputs of the Pi to the input sides of the 817s through a resistor (say 330 Ohm). When the GPIO goes high, the opto-isolator will fire and turn on the output, pulling the 320T signal down. NOTE: it is critically important that the +5 from the Pi only go to the output side of the 817s. If somehow +5V was put onto a GPIO input, you could damage that input on the Pi, or maybe the entire Pi.

Both methods work reliably in my test.

Reminder: there is no reason to connect the grounds from the opto-isolated logic input side of the 320T to the motor-control side.

Upvotes: 4

Related Questions