Control Stepper Motor Nema17 - How to Use Custom Code in Visuino PRO

by RonFrtek in Circuits > Arduino

171 Views, 2 Favorites, 0 Comments

Control Stepper Motor Nema17 - How to Use Custom Code in Visuino PRO

Control Stepper Motor Nema17 - How to Use Custom Code in Visuino PRO

In this tutorial we are going to control a stepper motor Nema 17 by using a Custom Code component in Visuino Pro.

Custom Code component in Visuino Pro allows us to add any Arduino code to the Visuino project.

We are going to use external library FlexyStepper in our Custom Code component.

The down side of this library is that that when Flexystepper controls the motor, nothing else on Arduino works

(You can't read sensors control displays or anything else - It takes over the controller) so this library is just an example on how you can include any library or Arduino code into Visuino.


Custom Code component in Visuino Pro is very useful if you need to add some sensor that is not yet supported in Visuino but you already have the code for it, or to just experimenting by adding your own code to the project.

Custom code in Visuino PRO is solving nearly 100% of all the missing functionality in Visuino Standard version.

More about Visuino Pro here

Here are also some very good tutorials on how to use a Custom Code component:

Getting started with the Custom Code component

Visuino Custom Code Tutorials

What You Will Need

VisuinoAdvrtisemenNeuroShield-small (1).jpg
A4988 expansion board drv 8825 stepper motor.jpeg
A4988.png
drv8825m.jpg
FNLOD2CKEY93G5N.jpg
NEMA17.jpg
F5OFEUQJJ4ZYFKY.LARGE.jpg
power-supply1.png
  • Arduino UNO (Or any other Arduino)
  • Stepper motor NEMA 17
  • A4988 Stepper Motor Driver or (DRV8825)
  • DRV8825/A4988 Stepper Driver Expansion Module
  • Jumper wires
  • Power Supply 12V
  • Visuino Pro software: Download here


The Circuit

F6YLCTTLF2JUNNQ.png
F9DBATWLF2JUNNR.png
  • Arduino Digital Pin 2 will be used for Steps
  • Arduino Digital Pin 3 will be used for Motor Direction
  • Optional - Arduino Digital Pin 4 will can used for driver pin Enable

If using a Stepper Motor Driver Shield:

  • Connect Motor Shield GND pin to Arduino negative pin [GND]
  • Connect Motor Shield [5V] pin to Arduino positive pin [5V]
  • Connect Motor Shield GND pin to Power Supply negative pin [GND]
  • Connect Motor Shield [9V] pin to Power Supply positive pin [+]
  • Connect Motor Shield pin[S] to Arduino digital pin [2]
  • Connect Motor Shield pin[D] to Arduino digital pin [3]
  • Connect stepper motor as shown on the picture.


If using a Stepper Motor Driver 8825:

  • Connect DRV8825 GND pin to Arduino negative pin [GND]
  • Connect DRV8825 DIR pin to Arduino digital pin [3]
  • Connect DRV8825 STEP pin to Arduino digital pin [2]
  • Connect Power Supply for the motor to DRV8825 VMOT and GND
  • Connect Capacitor across VMOT and GND
  • Connect stepper motor as shown on the picture.

FlexyStepper Arduino Code & Library

FVS8MXJLMJ6LUAQ.jpg
2023-10-16_13-37-40.jpg

Go to https://github.com/Stan-Reifel/FlexyStepper and download FlexyStepper - Arduino Library

Extract the to your Arduino Libraries folder, usually it looks something like this: C:\Users\User\Documents\Arduino\libraries

You can also find the Path if you in Visuino click on the Menu>Arduino>Configure and see it under "Arduino Library Directory" (see attached screenshot)


Start Visuino Pro, and Select the Arduino UNO Board Type

select-board-uno.jpg
Visuino-Select-Board-UNO.jpg

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

In Visuino Add Components

FACJEVALMJ6LUG8.jpg
2023-10-17_23-39-33.jpg
2023-10-17_23-41-03.jpg
  • Add "Custom Code" component
  • Add 3X "Integer Value" component
  • Add "Start" component (Start component will trigger the code when the Arduino will start)


In Visuino Set Components

2023-10-17_23-44-06.jpg
2023-10-17_23-45-12.jpg
2023-10-17_23-45-31.jpg
  • Select "IntegerValue1" and in the properties window under "Value" set the amount of steps in our case we will use 10000
  • Select "IntegerValue2" and in the properties window under "Value" set the Speed of the motor in our case we will use 1200
  • Select "IntegerValue3" and in the properties window under "Value" set the Acceleration Speed & Deceleration Speed of the motor in our case we will use 100

In Visuino Set Custom Code Inputs & Outputs

2023-10-17_23-13-19.jpg
2023-10-17_23-13-50.jpg
2023-10-17_23-14-04.jpg
2023-10-17_23-14-16.jpg
2023-10-17_23-17-12.jpg
2023-10-17_23-22-26.jpg
2023-10-17_23-21-12.jpg
2023-10-17_23-21-28.jpg
2023-10-17_23-26-06.jpg
2023-10-17_23-26-23.jpg
2023-10-17_23-28-26.jpg
2023-10-17_23-29-10.jpg
2023-10-17_23-30-09.jpg
2023-10-17_23-31-33.jpg
2023-10-17_23-32-42.jpg
2023-10-17_23-33-41.jpg
  • Double click on the "CustomCode1" and in the "Outputs" window drag "Integer" to the left side and in the properties window set "Name" to "StepsDone1"
  • in the "Outputs" window drag another "Integer" to the left side and in the properties window set "Name" to "CurrentSpeed1"
  • in the "Outputs" window drag another "Digital" to the left side and in the properties window set "Name" to "Finished1"
  • close the "Outputs" window

Right mouse click on the "CustomCode1" and in the menu click on the "Edit Inputs"

  • in the "Inputs" window drag "Integer" to the left side and in the properties window set "Name" to Steps1 & click on the "On Data" 3 dots button and in the "On Data" window add this:
TotalSteps = AValue;
  • Close the "On Data" window
  • in the "Inputs" window drag another "Integer" to the left side and in the properties window set "Name" to Speed1 & click on the "On Data" 3 dots button and in the "On Data" window add this:
Speed = AValue;
  • Close the "On Data" window
  • in the "Inputs" window drag another "Integer" to the left side and in the properties window set "Name" to AccelSpeed1 & click on the "On Data" 3 dots button and in the "On Data" window add this:
AccelSpeed = AValue;
  • Close the "On Data" window
  • in the "Inputs" window drag "Clock" to the left side and in the properties window set "Name" to Start1 & click on the "On Data" 3 dots button and in the "On Data" window add this:
stepper.setCurrentPositionInSteps(0);
stepper.setTargetPositionInSteps(TotalSteps);

stepper.setSpeedInStepsPerSecond(Speed);
stepper.processMovement();
  • Close the "On Data" window
  • in the "Inputs" window drag another "Clock" to the left side and in the properties window set "Name" to Stop1 & click on the "On Data" 3 dots button and in the "On Data" window add this:
stepper.setTargetPositionToStop();

Close the "On Data" window








Arduino Code

You can find documentation here: https://github.com/Stan-Reifel/FlexyStepper/blob/master/Documentation.pdf

for the Example code we are going to use this BasicStepper code:

//      ******************************************************************
//      *                                                                *
//      *         Simple example for controlling a stepper motor         *
//      *                                                                *
//      *            S. Reifel & Co.                6/24/2018            *
//      *                                                                *
//      ******************************************************************


// This is the simplest example of how to run a stepper motor.  
//
// Documentation for this library can be found at:
//    https://github.com/Stan-Reifel/FlexyStepper
//
//
// This library requires that your stepper motor be connected to the Arduino 
// using drive electronics that has a "Step and Direction" interface.  
// Examples of these are:
//
//    Pololu's DRV8825 Stepper Motor Driver Carrier:
//        https://www.pololu.com/product/2133
//
//    Pololu's A4988 Stepper Motor Driver Carrier:
//        https://www.pololu.com/product/2980
//
//    Sparkfun's Big Easy Driver:
//        https://www.sparkfun.com/products/12859
//
//    GeckoDrive G203V industrial controller:
//        https://www.geckodrive.com/g203v.html
//
// For all driver boards, it is VERY important that you set the motor 
// current before running the example.  This is typically done by adjusting
// a potentiometer on the board.  Read the driver board's documentation to 
// learn how.
//
//
// Note: FlexyStepper's companion library that supports faster step rates can 
// be found here:
//    https://github.com/Stan-Reifel/SpeedyStepper


// ***********************************************************************


#include <FlexyStepper.h>


//
// pin assignments
//
const int LED_PIN = 13;
const int MOTOR_STEP_PIN = 3;
const int MOTOR_DIRECTION_PIN = 4;


//
// create the stepper motor object
//
FlexyStepper stepper;


void setup() 
{
  //
  // setup the LED pin and enable print statements
  //
  pinMode(LED_PIN, OUTPUT);   
  Serial.begin(9600);


  //
  // connect and configure the stepper motor to its IO pins
  //
  stepper.connectToPins(MOTOR_STEP_PIN, MOTOR_DIRECTION_PIN);
}


void loop() 
{
  //
  // Note 1: It is assumed that you are using a stepper motor with a 
  // 1.8 degree step angle (which is 200 steps/revolution). This is the
  // most common type of stepper.
  //
  // Note 2: It is also assumed that your stepper driver board is  
  // configured for 1x microstepping.
  //
  // It is OK if these assumptions are not correct, your motor will just
  // turn less than a full rotation when commanded to. 
  //
  // Note 3: This example uses "relative" motions.  This means that each
  // command will move the number of steps given, starting from it's 
  // current position.
  //


  //
  // set the speed and acceleration rates for the stepper motor
  //
  stepper.setSpeedInStepsPerSecond(100);
  stepper.setAccelerationInStepsPerSecondPerSecond(100);


  //
  // Rotate the motor in the forward direction one revolution (200 steps). 
  // This function call will not return until the motion is complete.
  //
  stepper.moveRelativeInSteps(200);


  //
  // now that the rotation has finished, delay 1 second before starting 
  // the next move
  //
  delay(1000);


  //
  // rotate backward 1 rotation, then wait 1 second
  //
  stepper.moveRelativeInSteps(-200);
  delay(1000);


  //
  // This time speedup the motor, turning 10 revolutions.  Note if you
  // tell a stepper motor to go faster than it can, it just stops.
  //
  stepper.setSpeedInStepsPerSecond(800);
  stepper.setAccelerationInStepsPerSecondPerSecond(800);
  stepper.moveRelativeInSteps(200 * 10);
  delay(2000);
}

In Visuino Set Custom Code Component

FDWETXHLMJ6LVCJ.jpg
2023-10-17_23-04-13.jpg
2023-10-17_23-06-27.jpg
2023-10-17_23-07-45.jpg
2023-10-17_23-10-19.jpg
2023-10-17_23-11-32.jpg

Select "CustomCode1" and in the properties window select "Defines" and Click on the 3dots button

In the "Defines" window add this code:

const int LED_PIN = 13;
const int MOTOR_STEP_PIN = 2;
const int MOTOR_DIRECTION_PIN = 3;
const int STOP_BUTTON_PIN = 9;

int32_t TotalSteps;
int32_t Speed;
int32_t AccelSpeed;

And Close the "Defines" window


Select "CustomCode1" and in the properties window select "Global Declarations" and Click on the 3dots button

In the "Global Declarations" window add this code:

FlexyStepper stepper;

And Close the "Global Declarations" window


In the "Includes" window add this code:

#include <FlexyStepper.h>

And Close the "Includes" window


Select "CustomCode1" and in the properties window select "On Execute" and Click on the 3dots button

In the "On Execute" window add this code:

stepper.processMovement();  
stepper.setSpeedInStepsPerSecond(Speed);
stepper.setAccelerationInStepsPerSecondPerSecond(AccelSpeed);


   long currentPosition = stepper.getCurrentPositionInSteps();
   StepsDone1.Send(currentPosition);
   CurrentSpeed1.Send(Speed);


stepper.setAccelerationInStepsPerSecondPerSecond(AccelSpeed);

stepper.processMovement();
  if(!stepper.motionComplete())
{bool Finished = false;
 Finished1.Send(Finished);
}
else
{
bool Finished = true;
  
Finished1.Send(Finished);
}

And Close the "On Execute" window


Select "CustomCode1" and in the properties window select "On Init" and Click on the 3dots button

In the "On Init" window add this code:

pinMode(LED_PIN, OUTPUT);   
pinMode(STOP_BUTTON_PIN, INPUT_PULLUP);


stepper.connectToPins(MOTOR_STEP_PIN, MOTOR_DIRECTION_PIN);


bool stopFlag = false;
bool Finished = false;
stepper.setStepsPerRevolution(400);


stepper.setAccelerationInStepsPerSecondPerSecond(AccelSpeed);

And Close the "On Init" window

In Visuino Connect Components

2023-10-17_23-35-15.jpg
custom-stepper.png
  • Connect "IntegerValue1" pin [Out] to "CustomCode1" > "Inputs" > "Steps1" pin [In]
  • Connect "IntegerValue2" pin [Out] to "CustomCode1" > "Inputs" > "Speed1" pin [In]
  • Connect "IntegerValue3" pin [Out] to "CustomCode1" > "Inputs" > "AccelSpeed1" pin [In]
  • Connect "Start1" pin [Out] to "CustomCode1" > "Inputs" > "Start1" pin [In]

Generate, Compile, and Upload the Code

Visuino-Compile-UNO.jpg

In Visuino, at the bottom click on the "Build" Tab, make sure the correct port is selected, then click on the "Compile/Build and Upload" button.

Play

If you power the Arduino module the motor will do will start to run and when it reaches a certain amount of steps in our case 10000 it will slowly accelerate and decelerate.

Congratulations! You have completed your project with Visuino. Also attached is the Visuino project, that I created for this Instructable, you can download it here and open it in Visuino: https://www.visuino.eu