Arduino Nano and Visuino: Control 2 Stepper Motors With Joystick

by BoianM in Circuits > Arduino

26802 Views, 68 Favorites, 0 Comments

Arduino Nano and Visuino: Control 2 Stepper Motors With Joystick

Arduino Nano and Visuino: Control 2 Stepper Motors with Joystick

When building Arduino projects with Stepper motors, such as CNC machine, plotter, or Animated Art, there comes a moment when the steppers need to be controlled manually. Analog Joysticks are cheap and easy modules for manual control, with variable speed. They seem to be a good choice to control the steppers. Solving the problem with traditional Arduino code however is not trivial.

In this Instructable, I will show you how easy it is to program Arduino Nano with Visuino to control 2 stepper motors with Joystick.

Components

57674cef4fbadeab3e000132.jpeg
56761f204fbade6071000ac3.jpeg
  1. One Arduino compatible board (I use Arduino Nano, because I have one, but any other will be just fine)
  2. One Joystick that I got from this cheap 37 sensors set
  3. Two 5V Stepper Motors with Driver Boards (I used 28BYJ-48 stepper with ULN2003 driver board )
  4. One K2 Breadboard power supply with adapter (Picture 2), or other 5V Power source for the Steppers
  5. 17 Female-Female jumper wires

Connect Power and Ground the Stepper Motors

DSC02315.JPG
57683a2f67400cb3b2001298.jpeg
5768336bdeafa4a16e0000f2.jpeg
576838fe67400ccb7b000141.jpeg
576833a750e1b655080000bf.jpeg
ArduinoNanoGround.jpg
  1. Connect Ground(Black wire), and Power(Red wire), to each of the Stepper Driver modules (Picture 1)
  2. Connect another Ground(Black wire) to Ground pin of the Arduino board (Picture 2)
  3. Connect the other end of the Power (Red wires) from the Motor Driver modules to the "+" Power pins of the K2 Power supply (Pictures 3, and 4)
  4. Connect the other end of the Ground (Black wires) from the Motor Driver modules, and the Arduino to the "-" Ground pins of the K2 Power supply (Pictures 3, and 4)
  5. Make sure the Power Selection Switch of the K2 power supply is set to 5V (Picture 5)
  6. Picture 6 shows where is the Ground pin of the Arduino Nano

Connect the Stepper Motors to the Arduino

575c728515be4dbfeb0005c1.jpeg
57684dd8deafa4a212000059.jpeg
576834fc45bcebe6ac0007a4.jpeg
5768c1d445bcebe6ac00099d.jpeg
  1. If not already connected, plug the Stepper Motor connectors into the Driver Boards
  2. Connect one end of Female-Female jumper wires (Blue, Green, Yellow and Orange wires) to the IN1 to IN4 pins of the Stepper Driver for the First Motor (Picture 1)
  3. Connect one end of Female-Female jumper wires (Blue, Green, Yellow and Orange wires) to the IN1 to IN4 pins of the Stepper Driver for the Second Motor (Picture 1)
  4. Connect the IN1 wire from the First Motor (Blue wire) to the Digital pin 2 of the Arduino board (Picture 2)
  5. Connect the IN2 wire from the First Motor (Green wire) to the Digital pin 3 of the Arduino board (Picture 2)
  6. Connect the IN3 wire from the First Motor (Yellow wire) to the Digital pin 4 of the Arduino board (Picture 2)
  7. Connect the IN4 wire from the First Motor (Orange wire) to the Digital pin 5 of the Arduino board (Picture 2)
  8. Connect the IN1 wire from the Second Motor (Blue wire) to the Digital pin 6 of the Arduino board (Picture 3)
  9. Connect the IN2 wire from the Second Motor (Green wire) to the Digital pin 7 of the Arduino board (Picture 3)
  10. Connect the IN3 wire from the Second Motor (Yellow wire) to the Digital pin 8 of the Arduino board (Picture 3)
  11. Connect the IN4 wire from the Second Motor (Orange wire) to the Digital pin 9 of the Arduino board (Picture 3)
  12. Picture 4 shows in Red where are the Digital 2 to Digital 9 pins of the Arduino Nano . In Blue is shown the connection done in the previous step

Connect the Joystick to the Arduino

5768345f4936d45efe000087.jpeg
576833d72e7fb6e3370000ee.jpeg
5768340650e1b63443000066.jpeg
ArduinoNanoJoystick.jpg
  1. Connect Female-Female wires to the Ground(Black wire), Power(Red wire), VRx(Purple wire), and VRy(Gray wire) of the Joystick as shown in Picture 1
  2. Connect the Ground wire(Black wire) to the Ground pin of the Arduino board (Picture 2)
  3. Connect the Power wire(Red wire) to the 5V Power pin of the Arduino (Picture 2)
  4. Connect the VRx wire(Brown wire) the the Analog 1 pin of the Arduino board (Picture 3)
  5. Connect the VRy wire(Gray wire) the the Analog 0 pin of the Arduino board (Picture 3)
  6. Picture 4 shows in Red where are the Ground, 5V Power, Analog 0, and Analog 1 pins of the Arduino Nano . In Blue is shown the connection done in the previous steps

Start Visuino, and Select the Arduino Board Type

SelectBoardTolsEdited.png
Select BoardNano.png

To start programming the Arduino, you will need to have the Arduino IDE installed from here: http://www.arduino.cc/ .

Please be aware that there are some critical bugs in Arduino IDE 1.6.6.

Make sure that you install 1.6.7 higher, otherwise this Instructable will not work!

The Visuino: https://www.visuino.com also needs to be installed.

  1. Start Visuino as shown in the first picture
  2. Click on the "Tools" button on the Arduino component (Picture 1) in Visuino
  3. When the dialog appears, select Arduino Nano as shown in Picture 2

In Visuino: Add and Connect Stepper Motor Components

ComponentStepper.png
ComponentStepperConnect1.png
ComponentStepperConnect2.png

First we need to add components to control the stepper motors:

  1. Type "step" in the Filter box of the Component Toolbox then select the "4 Wire Stepper Motor" component (Picture 1), and drop two of them it in the design area
  2. Click in the "Out" box containing the pins of the Stepper1 component to start connecting all the Out pins at once (Picture 2)
  3. Move the mouse over the "Digital" input pin of the "Digital[ 2 ]" channel of the Arduino component. The Visuino will automatically spread the wires so they will connect correctly to the rest of the pins (Picture 2)
  4. Click in the "Out" box containing the pins of the Stepper2 component to start connecting all the Out pins at once (Picture 3)
  5. Move the mouse over the "Digital" input pin of the "Digital[ 6 ]" channel of the Arduino component. The Visuino will automatically spread the wires so they will connect correctly to the rest of the pins (Picture 3)

In Visuino: Set the Stepper Motor Component Properties, and Add Pins to Control the Speed

ComponentStepper1SetProperty.png
ComponentStepper1AddPin.png
ComponentStepper2SetProperty.png
ComponentStepper2AddPin.png

Since we want to control the speed of the steppers, we need to add pins to the "Steps Per Second" property:

  1. Select the Stepper1 component (Picture 1)
  2. In the Object Inspector, set the value of the "Steps Per Second" property to "0" (Picture 1)
  3. In the Object Inspector click on the Pin button at front of the "Steps Per Second" property, and select "Float SinkPin" (Picture 2)
  4. Select the Stepper2 component (Picture 3)
  5. In the Object Inspector, set the value of the "Steps Per Second" property to "0" (Picture 3)
  6. In the Object Inspector click on the Pin button at front of the "Steps Per Second" property, and select "Float SinkPin" (Picture 4)

In Visuino: Add and Connect Dead Zone Scaled Analog Components

ComponentZone.png
ComponentZoneConnect1.png
ComponentZoneConnect2.png
ComponentZoneConnect3.png
ComponentZoneConnect4.png

The Analog Pins where the Joystick is connected generate normalized values between 0.0 and 1.0. We need to convert them to +/-300 steps per second. Since the Joystick is not very precise when in released center position, we want a small zone around the center to be considered 0.5 so we need to introduce "Dead Zone":

  1. Type "zone" in the Filter box of the Component Toolbox then select the "Dead Zone Scaled Analog" component (Picture 1), and drop two of them it in the design area
  2. Connect the "Out" output pin of the DeadZoneScaled1 component to the "StepsPerSecond" input pin of the Stepper1 component (Picture 2)
  3. Connect the "Out" output pin of the "Digital[ 14 ]/Analog[ 0 ]" channel of the Arduino component to the "In" input pin of the DeadZoneScaled1 component (Picture 3)
  4. Connect the "Out" output pin of the DeadZoneScaled2 component to the "StepsPerSecond" input pin of the Stepper2 component (Picture 4)
  5. Connect the "Out" output pin of the "Digital[ 15 ]/Analog[ 1 ]" channel of the Arduino component to the "In" input pin of the DeadZoneScaled2 component (Picture 5)

In Visuino: Set the Properties of the Dead Zone Scaled Analog Components

ComponentZone1Prop1.png
ComponentZone1Prop2.png
ComponentZone2Prop1.png
ComponentZone2Prop2.png
  1. In the Design Area, select the DeadZoneScaled1 component (Picture 1)
  2. In the Object Inspector, expand the "Output Range" property (Picture 1)
  3. In the Object Inspector, set the value of the "Max" sub-property of the "Output Range" property to "300" (Picture 1)
  4. In the Object Inspector, set the value of the "Min" sub-property of the "Output Range" property to "-300" (Picture 2)
  5. In the Design Area, select the DeadZoneScaled2 component (Picture 3)
  6. In the Object Inspector, expand the "Output Range" property (Picture 3)
  7. In the Object Inspector, set the value of the "Max" sub-property of the "Output Range" property to "300" (Picture 3)
  8. In the Object Inspector, set the value of the "Min" sub-property of the "Output Range" property to "-300" (Picture 4)

Generate, Compile, and Upload the Arduino Code

GenerateCtopped.png
ArduinoIDE.png
  1. In Visuino, Press F9 or click on the button shown on Picture 1 to generate the Arduino code, and open the Arduino IDE
  2. In the Arduino IDE, click on the Upload button, to compile and upload the code (Picture 2)

And Play...

57682edfdeafa4a212000035.jpeg
VisuinoDiagramCrop.png
Arduino Nano and Visuino: Control 2 Stepper Motors with Joystick

Congratulations! You have completed the project.

Picture 1 and the Video show the connected and powered up project.

You can control the Steppers with the Joystick as seen the Video. Moving the Joystick Up and Down will control the Forward and Backward rotation of one of the Steppers. Moving the Joystick Left and Right will control the other Stepper.

On Picture 2 you can see the complete Visuino diagram.

Also attached is the Visuino project, that I created for this Instructable. You can download and open it in Visuino: https://www.visuino.com