kl3m3n's blog

Community Browser
cancel
Showing results for 
Search instead for 
Did you mean: 

Mini laser scanner - part 1 (controlling stepper motor with LabVIEW)

Klemen
Active Participant

The first part is dedicated to controlling a stepper motor via RS232 port using LabVIEW. I am not using LIFA (LabVIEW Interface for Arduino) to program Arduino, but C++ with basic Arduino programming environment (for example you could alternatively also use Atmel Studio for debugging the code).

In my opinion this approach is better, because the code is more portable (i.e., you do not need LIFA installed on every computer when porting the code).

I disassembled an old document scanner with six wire stepper motor. To determine the correct wiring search the web. There is a lot of documentation on this.

The wires of my stepper were already paired accordingly, so I discarded the two unnecessary wires and used the other four to connect them to Arduino through SN754410 Quad Half H-Bridge driver. The wiring is based on the diagram in Figure 1, which was taken from http://www.hobbytronics.co.uk/stepper-motor-sn754410.

stepper wiring.jpg

Figure 1. Wiring diagram for connecting the stepper motor to Arduino.

Figure 2 shows the experimental setup. Ignore the other side of the protoboard, since that is a 24V to 12V regulator from previous testing and is irrelevant in this case. It could be used to supply the motor but I found an old 12V adapter, which I used instead.


setup1.jpg

setup2.jpg

Figure 2. Experimental setup.

The Arduino code is really simple:


#include <Stepper.h>

#define steps_per_revolution 220 // 14 steps from min to max position
#define motor_speed 60

Stepper motor(steps_per_revolution,8,9,10,11); // init stepper library
int received_data[2]; // allocate memory for data over serial

void setup()
{
Serial.begin(9600); // init serial port
motor.setSpeed(motor_speed); // set the speed of the motor
}

void loop()
{
while(Serial.available() < 2) {} // wait until 2 bytes are available on serial port

for(int i=0;i<2;i++) {
received_data = Serial.read(); // read two bytes
}

int steps = received_data[1]; // the second byte determines how many steps in either direction

if(received_data[0] == 1) // clockwise revolution
{
for(int i=0;i<=steps;i++) // number of steps equals to the second received byte (0-255)
{
motor.step(steps_per_revolution); // step
delay(5);
}
Serial.print("DONE stepping in CW direction for ");
Serial.print(steps);
Serial.print(" steps!");
}

else if(received_data[0] == 2) // counter clockwise revolution
{
for(int i=0;i<=steps;i++) // number of steps equals to the second received byte (0-255)
{
motor.step(-steps_per_revolution); // step
delay(5);
}
Serial.print("DONE stepping in CCW direction for ");
Serial.print(steps);
Serial.print(" steps!");   
}
else Serial.print("ERROR!")
;
}

Arduino waits for 2 bytes over the serial port and acts according to the logical statements. In my case I always send 2 bytes, where the first gives the stepper motor direction (CW, CCW) and the second the number of steps. After the stepping is over, a message is sent to the serial port.

I am also attaching the code in LabVIEW. Note that both algorithms are just simple drafts and I will have to modify them according to the end application.

Also a note of caution – in my case there is no feedback on the motor position and no limit/end switches, so don't go too far. Some sort of limit/end switches could be easily added and their state monitored during the motor stepping. 

Thanks for reading.

Be creative.


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
Download All