Thread Tools
Mar 17, 2019, 08:12 AM
Registered User
Thread OP
Mini-HowTo

Stepper motor driven turret rotation


Hi all, I'm happy to do this and am interested in the subject, but if there isn't much interest I don't want to slow down the RCG website any more with Mbytes of palaver so if you are on board or watching please let me know. I'm going to do this in a series of posts so they aren't too long, skip along as needed.

I'm somewhat of a control freak (turret control freak), I got interested in this when I started building my 1/35 scale destroyer model, that was over 10 years ago, I don't want to resurrect that string but you can see it here:

https://www.rcgroups.com/forums/show...816710-1-35-Dd

Needless to say it's not finished yet.

More recently I switched to a more manageable sized project, a 1/200 scale trumpeter Bismarck battleship.

compassdirector with 4 turrets (3 min 17 sec)


Caps Bismarck compass controlled (1 min 38 sec)


This too is unfinished but it is quite close and will sail this spring.
Sign up now
to remove ads between posts
Mar 17, 2019, 08:28 AM
Registered User
cgbillb's Avatar
Thanks Cap
I am on board for sure, setting up my Arduino board now.
Thanks for taking the time to help us no program crew members. HaHa

Bill
Athens Ga

Building a 144 scale Bismarck
Mar 17, 2019, 09:05 AM
Registered User
Thread OP

part 2 - stepper motors


Warning!! I'm no expert, I'm not an electrician or computer programmer, I'm only describing as best I can how I make the steppers operate and what worked for me, there will be errors and omissions, best to use google to confirm anything that seems tawdry, and you will likely find better ways to do most of this so absolutely point out any of this, I welcome all comments.

The vast majority of stepper motors have 4, 5 or 6 leads. There are two basic types, unipolar and bipolar, the ones I use and the one shown below is a unipolar, it has a minimum of 5 leads, only bipolar can work on 4 leads total but they require reversing the polarity, so needing an H-bridge. Shown below is the 28BYJ-48, the fifth lead (red) is the positive power connection the other 4 are then ends of the coils. The motor operates by providing a sequence of neg connections to the 4 coil ends. Energizing each coil gives the motor a distinct step, no more, no less so it's rotation can be controlled exactly, that's why they are used for CNC machines etc.

The 28BYJ-48 is ideal for this purpose, it is small, cheap and it's geared, roughly 64:1 so each step of the stepper is quite small enough. (they cost less than 2$ each on some websites). I read they were originally developed and produced for use in controlling air flow in ducts or something like that (tawdry comment).

You need to drive the stepper motors, ie provide the series of signals, current to each coil, in succession, with enough current to make it move. I checked and the digital output pins on an arduino are inadequate even for this small of a motor, so you need the marvel of the 20th century, a transistor. I always had the hardest time comprehending what a transistor is, but I have since realized how terribly simple it is. It's simply an on off switch, like a light switch. With a light switch your finger turns the switch on and off, with a transistor a small, low power, electrical signal, turns the switch on and off. Of course there are a near infinite variety of transistors, but for my purposes these are on/off switches and that's what I'll call them, switches.

A cruel creation was the ULN2003 with 7 switches, most confusing and wasteful for steppers but the most often used. Steppers need 4 pins so 7 switches is most inefficient unless you are working on 28 stepper functions. By all means the ULN2003 works, use the premade drivers if you have the space but most elegant is the ULN2803 with no less than 8 switches, a very good choice when needing multiples of 4.

The other thing that you need is an absolutely terrifying micro processor. A computer. I can tell you I do not know how they work but they are not to hard use, with some effort. They are used in almost every device you touch that has electricity in it. Is all they really do is what they are told to do. I'll describe them in the next post.

Ideally, at this point you can decide how many stepper motors you want and where they are. The bismarck has two parallel motors front and two aft, it has three directors and those are optional, my model uses the main director, top center and omits the fore and aft. So for my model I need 5 motors, 3 distinct circuits, 3x4=12 control pins. With boaterdaves Fletcher, if he wants it correct requires 4x4=16 pins because the center turret is unique and requires a distinct function plus 4 more pins for the torp turrets (assuming they operate in unison, otherwise 8 more) for a grand total of 20 pins. No problem with that, two ULN2803 is perfect for it, no waste whatsoever (just more wires). Well adding the torp turrets requires another 4 so another ULN.
Last edited by capricorn; Mar 21, 2019 at 06:57 PM.
Mar 17, 2019, 10:01 AM
Registered User
Thread OP

Part 3: Micro processors


A microprocessor is a relatively simple vital organ, like a brain in a simple animal, it just does what it's programmed to do, eat and sh__, sorry I hope that's not going to be censored.

Input/output.

I use arduino, Italian, who'd have guessed. Italians aren't my first guess for someone to develop something rationally functional, pretty maybe, but functional and useful, no. Sorry to Italians, I mean no disrespect, the proof is in the pudding, arduino is Italian and it is, I believe, the most common and widely used hobby microprocessor.

So, we need to tell the stepper motors what to do, so we need a brain. The brain is turned on and it runs a loop of commands. It first checks what info it has and then processes the info and sends commands. (this is crucial, you can't proceed without knowing what you want to do)

PS it was censored, well good job RCG, I had to exchange "it" with __. Censor that!

You must first decide how you want to control the turrets. There are many, endless, options. My bismarck is controlled by a compass, as the boat turns the gun bearing maintains a preset direction, that direction can be reset with a re-aim. Compass control may be the most hazy, direct control is preferable. A dial knob on the radio would be suitable, even a joystick.

Once you have determined what motors you have and how you want to control them you can proceed.
Last edited by capricorn; Mar 17, 2019 at 07:14 PM.
Mar 17, 2019, 11:34 AM
Registered User
Thread OP

Part 4: arduino programmng


If you are new to this download the arduino ide and go to the examples folder and work through them. You'll see the general layout of what they call sketches, more precisely programs. Turn a light on and off, serial print output, etc. You can take any one of these sketches and build on it, add stuff. Eventually you'll want to operate the stepper motor, stepper libraries are most useful for this because they have pre-written commands, if you fulfill the input, the output will follow without fail.

I believe there are three major components to an arduino sketch. Declaration, setup and loop. If you use a library or other standard in your program you must declare it at the beginning, you will have some of these, at least one or more.

Setup, you must assign pin names and whether they are input, output, digital or analog. Steppers require digital output pins, the most common and simple, on or off as directed. Analog pins can be used as digital pins very simply so count them active if needed. You also declare variable types here, set norms etc.

Main loop. You all know what a loop is I hope. In this case it's a constantly recurring set of questions and answers, extraordinarily simple generally. A series of if's and's or but's and logical responses. Extremely rigorous the microprocessor demands precise and exact commands, it will not tolerate junk input. Remember that it does only what it is told to do, if you are incapable of telling it what to do you will fail at this. So you absolutely must know what you want, if you do know what you want this thing is your friend. Clarification, you probably can't start writing a program and know exactly how it's going to operate, I can't, you have to start with the core loop, then add stuff and change it so don't worry if you don't know, you just need a good idea of what you want it to do.
Last edited by capricorn; Mar 21, 2019 at 07:03 PM.
Mar 17, 2019, 11:51 AM
Registered User
cgbillb's Avatar
Cap
OK got my Arduino working via the lessons.
Now I want to get the 28BYJ-48 with the driver working on the test bench.
I like to use the Mega 2560 to drive the stepping motor 90 degrees left 0 and then 90 degrees right. When I get Anton working I can and Bruno, Casar and Dora one at a time.

I will be using my DX 18 and use switch C and F

Bill
Mar 17, 2019, 12:32 PM
Registered User
cgbillb's Avatar
Cap
I found this code for
The source code for stepper motor 28BYJ-48 with ULN2003 for Arduino
But can not down load it , even through it says you can, Is there a secret to downloading codes?
http://robojax.com/learn/arduino/?vid=robojax-0293 is where I found it.
Thanks
Bill
Mar 17, 2019, 04:45 PM
Registered User
Thread OP
Bill, you can download the code by highlighting it and using copy paste. Are you familiar with copy paste?

Here it is:


//original source is http://www.geeetech.com/wiki/index.p...rd_for_Arduino
// Update by Ahmad Shamshiri for RoboJax.com
// Published on March 27, 2017 in Aajx, ON, Canada.

int Pin1 = 10;
int Pin2 = 11;
int Pin3 = 12;
int Pin4 = 13;
int _step = 0;
boolean dir = true;// false=clockwise, true=counter clockwise
int count=0;
void setup()
{
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
pinMode(Pin4, OUTPUT);
}
void loop()
{
switch(_step){
case 0:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
case 1:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, HIGH);
break;
case 2:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 3:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, HIGH);
digitalWrite(Pin4, LOW);
break;
case 4:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 5:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, HIGH);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 6:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
case 7:
digitalWrite(Pin1, HIGH);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, HIGH);
break;
default:
digitalWrite(Pin1, LOW);
digitalWrite(Pin2, LOW);
digitalWrite(Pin3, LOW);
digitalWrite(Pin4, LOW);
break;
}
if(dir){
_step++;
}else{
_step--;
}
if(_step>7){
_step=0;
}
if(_step<0){
_step=7;
}
delay(1);

}

press control key along with c to copy, then switch to your arduino sketch and press control key along with p to paste it.
Mar 17, 2019, 04:54 PM
Registered User
cgbillb's Avatar
Cap
Thanks I was just trying to copy a sketch to run a servo with my Radio and Arduino
Will try your way
Thanks
Bill
Mar 17, 2019, 05:29 PM
Registered User
cgbillb's Avatar
Cap
Have it working , all most.
But I am beginning to understand Arduino and sketch better.
I understand how the Radio and the Mega work together now.
Will try something a little easier later or in the am.
Thanks for all your help
Bill
Mar 17, 2019, 05:44 PM
Registered User
Thread OP
That's great Bill, if you can run a servo through your radio receiver through the arduino Mega you are getting the hang of it. One of the handy aspects of using the arduino is that you can modify the servo (or motor) motion within the program, slow it down, change acceleration and rotation speeds etc. It does what it's told to do without fail (within the limits)
Mar 17, 2019, 06:56 PM
Registered User
Thread OP

Part 5: running steppers from arduino


There are basically two ways of running stepper motors from your microprocessor. Raw commands like Bills code that he found, or using a stepper library. The stepper library is simply some prewritten code with new defined commands not in the basic arduino program. You can make your own library or use one that someone else made. I started by using customstepper.h library by Igor Campos, he deserves much credit for this turret system working because I use that library and it works for me. I believe he said he created it because the libraries he tried did not work well for him and so he created his own and to me it works well too. I believe it contains some features that ensure that the steppers do as intended, if you attempt raw control you are going down a path of continuous troubleshooting. I have not experimented with any other libraries because I have not needed to, I hear/read, the accel stepper library works well, you may try that too. I do recommend using a library, much as I'd rather not.

From here on I'm describing an arduino sketch that uses the customstepper.h library, deviations from that you'll have to handle on your own, it should be similar in any case.

At the top of the sketch, the beginning, you declare your libraries:

#include <customstepper.h>
#include <wire.h>
#include <LSM303.h>

You probably don't have customstepper.h on your computer, so you have to download it and put it in your arduino library folder. The LSM303.h is for the compass I use, when you purchase a compass module it will have a library and you have to download it to your arduino library folder so the program can find it, it does not search internet for you. wire.h is a very common library you will likely need for serial communication I believe it will come with the initial arduino IDE download. Backtracking slightly, I'm assuming you've downloaded the arduino IDE, it's the heart and soul of this endeavor, it's free, download from arduino.cc. I'm hoping that you use some other resources to get started and become familiar with arduino, the arduino website is ripe with info on how to get started, use that.

the customstepper library requires you to declare your motor circuits before proceeding:

CustomStepper turret12(2,3,4,5, (byte[] {8,B1000,B1100,B0100,B0110,B0010,B0011,B0001,B1001 } , 4096, 3, CCW);

"turret12" has become your designation you will refer to it in future commands.

"2,3,4,5," are your digital output pins you use and where the program will send the signals, get this correct and you are good. It could be 6,7,8,9, could be 1,6,4,9 whatever you want, just make sure these correspond to the wires you plug into the pins on the board. Technically they are d2,d3,d4,d5 but they know this they are Italian, the d is for digital.

I'd have to look back to see what the rest of the items are, the B1000 series is the step sequence, 0's and 1's, binary palaver you can forget after doing this step. 4096 is the steps per revolution. Oddly the 28BYJ-48 has an actual gear ratio of 4075.8, it lost a tooth or two at some point, you can use either number, the 4075.773 will result in the most accurate rotation, but the difference is not noticeable.

"3" has to do with the speed of rotation, you'll have to experiment with this, I don't get response under 3, and a very high number exceeds the motors ability.

"CCW" rotation direction to start with. counterclockwise. If you want to start CW use CW instead of CCW. Later, when you want to change direction of rotation you will redefine variable to CW or CCW. Fairly simple. You can also change the rotation speed from 3 to something else as desired and when needed.

Note that I avoided using d0 and d1, they scare me, they have dual designations Rx and Tx that are used for other things, but you can use them if you want, I skip them thinking I may need them in the future. So not wanting to reassign all the pin numbers in the future I left them open. It's purely superstition but that does figure into my programming method.
Last edited by capricorn; Mar 17, 2019 at 07:43 PM. Reason: I can't count well
Mar 17, 2019, 07:19 PM
Registered User
Thread OP
I'd like to claim credit for the copy paste method but that might be bit of a stretch.

Quote:
Originally Posted by cgbillb
Cap
Thanks I was just trying to copy a sketch to run a servo with my Radio and Arduino
Will try your way
Thanks
Bill
Mar 17, 2019, 07:32 PM
Registered User
Thread OP
You all aren't very talkative, I see over 80 views but only Bill is on board. Come on and at least add a popcorn eating dude.
Mar 17, 2019, 09:06 PM
Registered User
Thread OP

Part 6: stepper commands


You must learn if and if else commands in arduino , if you can't do this, it's just not possible to continue. This is what a computer does. You pose a question and based on the answer a procedure is followed, a single command or series of commands. The customstepper library uses these commands

if (turret12.isDone)

"turret12" is your name for the motor circuit, can be anything. ".isDone" is in the library.
This question is posed before you proceed with moving the motor to ensure that it has completed it's last mission. Exclusion of this feature ensures that you will interrupt the sequence and lose steps and leave the turret idle not knowing what to do. Including the isDone is crucial, if in doubt put this before all other commands.

Within the program you can use these additional items

turret12.setdirection(CW);

and/or

turret12.setRPM(10); (to speed up rotation or slow down)

the key command is this:

turret12.rotateDegrees(x);
And then
turret12.run();

Back to the beginning, the if turret12.isDone checks if turret12.run is finished if it is finished it can except a new command if not it waits. Very simple, very rational. This would require extensive effort to program with raw pin output, thus suggestion to use library, near mandatory. It's all just additional code so do as you please but don't make it harder than you have to.

If the stepper doesn't move, just hums, don't fret, troubleshoot, best on google. The wire sequence is likely misdone, no problem, no harm done, just rearrange the wires, but don't leave it humming overnight. Can be done rationally or by multiple exchange of wires until it works, there are four inputs so 4! factorial is 24 possible arrangements, good luck, best to be rational. Billions of online directions on how to get the dumb thing to rotate. It's not the 28BYJ-48's fault if you feed it an incorrect sequence of power it's your fault, own up and fix it, don't throw the motor across the room and blame it for its insolence. I've thrown things in the past and found it ultimately unproductive.
Last edited by capricorn; Mar 21, 2019 at 07:09 PM.


Quick Reply
Message:

Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Discussion Turret rotation admiralbb62 Scale Boats 22 Feb 22, 2019 10:10 PM
New Product ULN2003 Stepper Motor Control Board + 5V Stepper Motor ICStation Power Systems 0 May 29, 2014 04:54 AM
Discussion Servo driven turret SwePilot Scale Helicopters 13 Dec 16, 2009 07:55 AM
Discussion Turret Rotation RussCA Dock Talk 2 Jul 24, 2009 10:33 AM
Discussion Heng Long Tiger turret rotation hanging. Rampage RC Tanks 3 Sep 28, 2008 05:35 PM