Synchronized 2-Axis Motion With Variable Speed (Arduino + LEDs + 28BYJ-48)
Introduction: Synchronized 2-Axis Motion With Variable Speed (Arduino + LEDs + 28BYJ-48)
This tutorial shows how to move two stepper motors so they will synchronously arrive at defined destination point. There are many sophisticated solutions for doing this such as GRBL or Marlin. Using an Arduino, there are also dedicated “Multi Stepper” libraries that can support you. However, we will use a more basic approach here making it as easy as possible while adding a simple user interface using either three LEDs and some microswitches.
Supplies
* 1 Arduino Nano (or Uno, with 328P CPU)
* 1 Breadboard (830 Holes)
* 14 Jumper Cables (male-to-male, 10...20 cm)
* 12 Jumper Cables (male to-female, 10...20 cm)
* 2 LEDs (5mm, 20mA, around 2V)
* 2 Resistors (220 Ohm)
* 2 Microswitches
* 1 Potentionmeter (5 or 10 kOhm)
* 1 5V Power Supply
* 2 28BYJ-48 Stepper Motors (5V Version) + ULN 2003 Driver Boards
Step 1: Configure Your Breadboard
Set-up your breadboard as shown in the pictures above:
* Connect GND and 5V to the according rails of the breadboard.
* Pins D2 to D5 of the Arduino go to In1 to In4 of the first ULN2003 board.
* Pins D6 to D9 of the Arduino go to In1 to In4 of the second ULN2003 board.
* Pin D10 of the Arduino goes to the + leg (longer leg) of the first LED (red). Connect the shorter leg (-) to a 220 Ω resistor and the other end of this resistor to the ground rail.
* Connect D11 the same way to the second LED and its resistor.
* Connect the Arduino pins A0, A1, and A2 to the microswitches. Connect the other side of the microswitches to GND. We will not use any pull-up resistors to avoid short circuits here, because we use the pull-up that are already integrated in the Arduino.
* Connect the left leg of the potentiometer to ground, the right leg to 5V and the middle leg to A3 of the Arduino.
* The Arduino itself will be powered through the USB port during the first tests. Nevertheless, the motors should not be powered through the USB port. Hence, connect a separated power supply to plus/ground rails on the other side of the breadboard. 611
Connect the + pin and - pins of each ULN2003 board to these plus/ground rails. As a last step, make a bridge between the both ground rails.
Step 2: A First Test
Now we can do a first test of our set-up and the included “user interface”.
Open the Arduino IDE and load the attached “Two_Steppers_-_First_Test.ino” file. It should compile without any additional libraries. Most of the code is pretty self-explanatory.
* The setup() method is only defining which pins will be used for input and out actions and open the serial connection to the PC.
* The loop() method now will continuously perform the following sequence:
1. Read the value of the potentiometer from the analog input, scale for displaying values between 0 and 100%, read the buttons and print the status all input elements to the serial interface.
2. Switch on one LED on and the other off.
3. Using two for-loops, move each motor approximately a quarter of a full rotation (8 * 128 = 1,024 half-steps per quarter rotation; a full rotation is usually 4,076 half-steps for the 28BYJ-48 motors). During the every second run of loop(), the steps will be done in forward direction. During the other runs, the motors will go back to the initial position.
The core concept of moving stepper motors is to switch on and off the coils in the motors using the digital output pins. On the Arduino, they are represented by the PORTB and PORTD registers, where D2…5 (= motor 1) are the bits 2…5 of PORTD, D7…8 (first two wires that go to motor 2) are bits 6…7 of PORTD and D9…10 (second two wires of motor 2) are bits 0…1 of PORTB. 991