Since I have 2 little stepper motors and a joystick I thought it would be nice to see if I could write a sketch that would let me control them with the joystick. I first connected the little stepper motors and ran an example sketch I found here to see if they were working. To power the motors I connected them to my lab power supply. That was easy and it didn’t take me long to see the motors turning from left to right. But something was clearly not right, the 2 little stepper motors that I have are off the 28YBJ-48 type. According to the specifications I found online they have a step angle of 5.625°. So that is 64 steps for one rotation. By running the example sketch I found out that I needed to edit the sketch to more then 2000 steps for one rotation.
I played with the speed settings, rechecked the connections but it still needed +2000 steps for one rotation? Very strange, trying to find an answer for this proofed fruitless. I did found some posts on the Arduino forum of people having the same problem. I measured the current they draw and 1 little motor draws 220mA, although the specs I found said only 92mA. Strange things these little steppers.
The sketch to control the motors with the joystick is very basic, it doesn’t have speed control because the my little motors didn’t react very well to this. They are quite slow and slowing them more down didn’t work.
So here is the sketch:
// http://www.bajdi.com // 2 stepper motors controlled by a joystick #include <Stepper.h> const int stepsPerRevolution = 64; // change this to fit the number of steps per revolution // for your motor // initialize the stepper library for both steppers: Stepper small_stepper(stepsPerRevolution, 8,10,9,11); Stepper small_stepper2(stepsPerRevolution, 3,5,4,6); void setup() { // set the speed of the motors small_stepper.setSpeed(200); // set first stepper speed small_stepper2.setSpeed(200); // set second stepper speed } void loop() { int sensorReading = analogRead(A0); // read value from joystick X-axis if (sensorReading < 490) { small_stepper.step(1); } // step left if (sensorReading > 540) { small_stepper.step(-1); } // step right int sensorReading2 = analogRead(A1); // read value from joystick Y-axis if (sensorReading2 < 490) { small_stepper2.step(1); } // step forward if (sensorReading2 > 540) { small_stepper2.step(-1); } // step backward }
[…] month ago I wrote a sketch to control 2 little stepper motors with an analog joystick. After getting the Nrf24L01 modules to […]
Hi, Your sketch is excellent, one quick question would it need modiying much if my steppers are running through an ADAfruit Motor Shield?
Thanks
Joe
Hmm, the Adafruit motor shield uses L293D chips to control the motors. I don’t know if the L293D works with the stepper library, I don’t think so.
Hi!
Regarding the >2000 steps: I think the motor itself has 48 steps/turn plus a 1/64 reduction gear. This results in 48×64=3072 steps per turn.
And 92 mA is the current per wire, not for the whole motor…
Thanks for the info 🙂
Awesome but wont work for me and I cant tell why, would there be anyway to incorporate buttons for left right?
Sure, just modify the sketch…
This is just what I am looking for to do some pan and tilt. Any chance I can get a quick diagram. I am trying to figure out how the power is hooked up from the joystick-it is an older style analog with two pots.
Thanks
John
The analog joystick has 2 analog potentiometers.
Can I ask for a plain , easiest Wiring diagram of the kit ??
Hello, thank you for the helpful videos.
I am complete novice and’d eden Remote head of my self-built camera crane control as follows. With Joystick X; Y;. Speed and a soft start via potentiometer. Can you provide me an appropriate Skatch available. I would be very grateful.
Hello,
You can add some lines in the original code.
I use also a LCD display 1602 I2C.
Code:
// http://www.bajdi.com
// 2 stepper motors controlled by a joystick
//moteurs 28YBJ-48 Driver ULN2003
#include
#include
LiquidCrystal_I2C lcd(0x27,16,2);
#include
const int stepsPerRevolution = 64; // change this to fit the number of steps per revolution
int vitesse;
// for your motor
// initialize the stepper library for both steppers:
Stepper small_stepper(stepsPerRevolution, 8,10,9,11);
Stepper small_stepper2(stepsPerRevolution, 3,5,4,6);
void setup() {
lcd.init();
lcd.backlight();
lcd.print(“Deux moteurs”);
lcd.setCursor(0, 1);
lcd.print(“Vit: “);
lcd.setCursor(7,1);
lcd.print(vitesse);
// set the speed of the motors
small_stepper.setSpeed(200); // set first stepper speed
small_stepper2.setSpeed(200); // set second stepper speed
}
void loop() {
int potar = analogRead(A2);//read valuefrom potentiomètre
//change scale for compatibility motor step
vitesse = map(potar,0,1023,100,300);
small_stepper.setSpeed(vitesse); // set first stepper speed
small_stepper2.setSpeed(vitesse); // set second stepper speed
int sensorReading = analogRead(A0); // read value from joystick X-axis
if (sensorReading 540) { small_stepper.step(-1); } // step right
int sensorReading2 = analogRead(A1); // read value from joystick Y-axis
if (sensorReading2 540) { small_stepper2.step(-1); } // step backward
lcd.setCursor(7,1);
lcd.print(vitesse);
}
how add 2 endstop button please?