Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Help modifying code for my project.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Help modifying code for my project.

by Chemdawgds on Fri Mar 13, 2020 8:20 pm

Hello everyone. I am an avid model railroad enthusiast, and have been thinking about something for a while, so I decided to dig into it. I have a handmade turntable that I want to modify to be turned by a stepper motor. I've been doing quite a bit of research on this, and have found several people who have accomplished exactly what I'm trying to do. However, I have ran into some issues that I need help with. I am attaching the code I am currently using, and a picture of the schematic that sends the instructions to the controller.

What I am using:

Arduino Uno
Adafruit Stepper/motor/servo shield
Stepperonline Nema 17 Stepper Motor

The problem I have is that I feel I'm pushing the Adafruit shield to it's max. I do have 12 volts that I can supply for the stepper, but I noticed it runs much smoother on other setups using a TB6600 Driver. I can actually raise the voltage up to 16 volts and it's very smooth and nothing gets hot. When I run the setup using my adafruit shield, it starts getting hot, and the stepper motor gets hot.

I was thinking of trying this SAME project with and without the adafruit shield, and seeing how it will run with the TB6600 driver. However, I have NO idea how to change out the adafruit with the TB6600 in the program I have.

Code: Select all | TOGGLE FULL SIZE
////////////////////////////////////////////////////////////////////////////////
//
// DCC Turntable Control Test Routines

#include <DCC_Decoder.h>
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Defines and structures
//
#define kDCC_INTERRUPT            0

typedef struct
{
    int               address;                // Address to respond to

} DCCAccessoryAddress;

DCCAccessoryAddress gAddresses[8];

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Adafruit Setup

Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers

// Connect stepper with 200 steps per revolution (1.8 degree)
// to the M3, M4 terminals (blue,yellow,green,red)

Adafruit_StepperMotor *myStepper2 = AFMStop.getStepper(200, 2);

// you can change these to SINGLE, DOUBLE, INTERLEAVE or MICROSTEP!

// wrapper for the motor! (3200 Microsteps/revolution)
void forwardstep2() { 
  myStepper2->onestep(FORWARD, MICROSTEP);
}
void backwardstep2() { 
  myStepper2->onestep(BACKWARD, MICROSTEP);
}

// Now we'll wrap the stepper in an AccelStepper object

AccelStepper stepper2(forwardstep2, backwardstep2);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Decoder Init
//
void ConfigureDecoder()
{
    gAddresses[0].address = 200;
    gAddresses[1].address = 201;
    gAddresses[2].address = 202;
    gAddresses[3].address = 203;
    gAddresses[4].address = 204;
    gAddresses[5].address = 205;
    gAddresses[6].address = 206;
    gAddresses[7].address = 207;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Basic accessory packet handler
//
void BasicAccDecoderPacket_Handler(int address, boolean activate, byte data)
{
        // Convert NMRA packet address format to human address
    address -= 1;
    address *= 4;
    address += 1;
    address += (data & 0x06) >> 1;
   
    boolean enable = (data & 0x01) ? 1 : 0;
   
    for(int i=0; i<(int)(sizeof(gAddresses)/sizeof(gAddresses[0])); i++)
    {
        if( address == gAddresses[i].address )
        {
            Serial.print("Basic addr: ");
            Serial.print(address,DEC);
            Serial.print("   activate: ");
            Serial.println(enable,DEC);
           
            if( enable )
            {
                switch (i) {
                  case 1:
                    stepper2.moveTo(200);
                  break;
                  case 2:
                    stepper2.moveTo(400);
                  break;
                  case 3:
                    stepper2.moveTo(600);
                  break;
                  case 4:
                    stepper2.moveTo(800);
                  break;
                  case 5:
                    stepper2.moveTo(1000);
                  break;
                  case 6:
                    stepper2.moveTo(1200);
                  break;
                  case 7:
                    stepper2.moveTo(1400);
                  break;
                }
            }else{
                switch (i) {
                  case 1:
                    stepper2.moveTo(1800);
                  break;
                  case 2:
                    stepper2.moveTo(2000);
                  break;
                  case 3:
                    stepper2.moveTo(2200);
                  break;
                  case 4:
                    stepper2.moveTo(2400);
                  break;
                  case 5:
                    stepper2.moveTo(2600);
                  break;
                  case 6:
                    stepper2.moveTo(2800);
                  break;
                  case 7:
                    stepper2.moveTo(3000);
                  break;
                }
            }
        }
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Setup
//
void setup()
{
   Serial.begin(9600);
   
   AFMStop.begin(); // Start the shield
 
  //configure pin3 as an input and enable the internal pull-up resistor
  pinMode(3, INPUT_PULLUP);
  //read the sensor (open collector type) value into a variable
  int sensorVal = digitalRead(3);
   
  //set stepper motor speed and acceleration
  stepper2.setMaxSpeed(30.0);
  stepper2.setAcceleration(20.0);
//  stepper2.moveTo(800);

// if near reference point move away
  sensorVal = digitalRead(3);
  while (sensorVal == LOW) {
    sensorVal = digitalRead(3);
    forwardstep2();
      delay(50);
  }
 
// step forward to sensor index point
  while (sensorVal == HIGH) {
    sensorVal = digitalRead(3);
    forwardstep2();
      delay(50);
  }
   
   DCC.SetBasicAccessoryDecoderPacketHandler(BasicAccDecoderPacket_Handler, true);
   ConfigureDecoder();
   DCC.SetupDecoder( 0x00, 0x00, kDCC_INTERRUPT );
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Main loop
//
void loop()
{
    static int addr = 0;
   
        ////////////////////////////////////////////////////////////////
        // Loop DCC library
    DCC.loop();
   
        ////////////////////////////////////////////////////////////////
        // Bump to next address to test
    if( ++addr >= (int)(sizeof(gAddresses)/sizeof(gAddresses[0])) )
    {
        addr = 0;
    }
   
    stepper2.run();
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


A little bit more information. The way this project works. Looking at the photo I attached of the schematic, The top left 3 wires is for a hall effect sensor. This is used for identifying a "TRUE ZERO" position. The next two wires below that are for DCC control. This is so I can select an address and the turntable will move to the pre-programmed position. The bottom two wires are for a reset button.

As I said earlier, this works great, but I'm afraid I'm going to damage the adafruit sheild. I would rather use this for my smaller steppers that I also have a use for on my railroad.

I'm posting two pictures. 1 is of the schematic for the interface I'm using, the 2nd is of the TB6600 Driver. I will also post another program that shows how someone defined the pins on the Arduino to work with the TB6600. I would like to keep my original program the same that works really well, but use the TB6600 Driver in place of the Adafruit Sheld.

Code: Select all | TOGGLE FULL SIZE
// test code for
// CNC Single Axis 4A TB6600 Stepper Motor Driver Controller
// use Serial Monitor to control 115200 baud


word  X;
word NX;
int MoveSpeed=600; //step in Microseconds
String     inputString = "help\n"; // a string to hold incoming data
boolean stringComplete = true;     // whether the string is completet
boolean        ComData = false;    // whether com data is on when motors are moving will slow them down

# define X_ENgnd  2 //ENA-(ENA) stepper motor enable , active low     Gray
# define X_EN_5v  3 //ENA+(+5V) stepper motor enable , active low     Orange
# define X_DIRgnd 4 //DIR-(DIR) axis stepper motor direction control  Blue
# define X_DIR_5v 5 //DIR+(+5v) axis stepper motor direction control  Brown
# define X_STPgnd 6 //PUL-(PUL) axis stepper motor step control       Black
# define X_STP_5v 7 //PUL+(+5v) axis stepper motor step control       RED


void setup() {// *************************************************************     setup
pinMode (X_ENgnd ,OUTPUT); //ENA-(ENA)
pinMode (X_EN_5v ,OUTPUT); //ENA+(+5V)
pinMode (X_DIRgnd,OUTPUT); //DIR-(DIR)
pinMode (X_DIR_5v,OUTPUT); //DIR+(+5v)
pinMode (X_STPgnd,OUTPUT); //PUL-(PUL)
pinMode (X_STP_5v,OUTPUT); //PUL+(+5v)
pinMode (13,OUTPUT);
digitalWrite (X_ENgnd,  LOW); //ENA-(ENA)
digitalWrite (X_EN_5v, HIGH); //ENA+(+5V) low=enabled
digitalWrite (X_DIRgnd, LOW); //DIR-(DIR)
digitalWrite (X_DIR_5v, LOW); //DIR+(+5v)
digitalWrite (X_STPgnd, LOW); //PUL-(PUL)
digitalWrite (X_STP_5v, LOW); //PUL+(+5v)

Serial.begin(115200);
}

void serialEvent()// ********************************************************      Serial in
{ while (Serial.available())
  {
    char inChar = (char)Serial.read();            // get the new byte:
    if (inChar > 0)     {inputString += inChar;}  // add it to the inputString:
    if (inChar == '\n') { stringComplete = true;} // if the incoming character is a newline, set a flag so the main loop can do something about it:
  }
}
void Help(){ // **************************************************************   Help
 Serial.println("Commands step by step guide");
 Serial.println("Type hello -sends TB6600 Tester Ready ");
 Serial.println("Type xon  -turns TB6600 on");
 Serial.println("Type x+Number(0-60000) eg x1904 -to set next move steps");
 Serial.println("Type mx -to make motor move to next postion");
 Serial.println("Type cdon -turns on postion data when moving will increase time of step");
 Serial.println("Type x0");
 Serial.println("Type mx");
 Serial.println("Type s+Number(0-2000) eg s500 -to set Microseconds betwean steps");
 Serial.println("Type s2000");
 Serial.println("Type x300");
 Serial.println("Type mx");
 Serial.println("Type xoff -turns TB6600 off");
 Serial.println("Type cdoff -turns off postion data when moving");
 
 

 inputString="";
}
void Hello(){ // **************************************************************   Hello
 Serial.println("TB6600 Tester Ready");
 inputString="";
}
void ENAXON(){ // *************************************************************   ENAXON
 Serial.println("ENAXON");
 digitalWrite (X_EN_5v, LOW);//ENA+(+5V) low=enabled
 inputString="";
}
void ENAXOFF(){  // ***********************************************************   ENAXOFF
 Serial.println("ENAXOFF");
 digitalWrite (X_EN_5v, HIGH);//ENA+(+5V) low=enabled
 inputString="";
}
void MSpeed(){  // ************************************************************   MoveSpeed
 inputString.setCharAt(0,' ');
 MoveSpeed=inputString.toInt();
 Serial.print("Speed=");
 Serial.println(MoveSpeed);
 inputString="";
}
void ComDataON(){  // *********************************************************   ComDataON
 ComData=true;
 Serial.println("ComDataOn");
 inputString="";
}
void ComDataOFF(){  // ********************************************************   ComDataOFF
 ComData=false;
 Serial.println("ComDataOFF");
 inputString="";
}
void NextX(){ // **************************************************************    NextX
 inputString.setCharAt(0,' ');
 NX=inputString.toInt();
 Serial.print("NX=");
 Serial.println(NX);
 inputString="";
}
void MoveX(){ // **************************************************************    Move
int xt;
if (NX>X)
{xt=NX-X; digitalWrite (X_DIR_5v,LOW);xt=1;}
else
{xt=X-NX; digitalWrite (X_DIR_5v,HIGH);xt=-1;}
if (ComData==true)
{for (; X !=NX; X=X+xt)
{    digitalWrite (X_STP_5v, HIGH);
     Serial.print("X=");
     delayMicroseconds (MoveSpeed);
     digitalWrite (X_STP_5v, LOW);
     delayMicroseconds (MoveSpeed);
     Serial.println(X+100000);
}}
else
{for (; X !=NX; X=X+xt)
{    digitalWrite (X_STP_5v, HIGH);
     delayMicroseconds (MoveSpeed);
     digitalWrite (X_STP_5v, LOW);
     delayMicroseconds (MoveSpeed);
}}
Serial.print("X=");
Serial.println(X);
//X=NX;

inputString="";
}
void loop()  // **************************************************************     loop
{
 serialEvent();
 if (stringComplete)
 {
  if (inputString=="help\n")      {Help();}
  if (inputString=="hello\n")     {Hello();} 
  if (inputString=="xon\n")       {ENAXON();}   
  if (inputString=="xoff\n")      {ENAXOFF();}
  if (inputString=="cdon\n")      {ComDataON();}
  if (inputString=="cdoff\n")     {ComDataOFF();}
  if (inputString=="mx\n")        {MoveX();}
  if (inputString.charAt(0)=='s')   {MSpeed();}
  if (inputString.charAt(0)=='x')   {NextX();}
 
  if (inputString !="") {Serial.println("BAD COMMAND="+inputString);}// Serial.print("\n"); }// "\t" tab     
  inputString = ""; stringComplete = false; // clear the string:
 }



I really hope I included enough information. Any and all help would be appreciated.

Capture.PNG
Capture.PNG (199.01 KiB) Viewed 104 times

Capture1.PNG
Capture1.PNG (472.34 KiB) Viewed 104 times

Chemdawgds
 
Posts: 14
Joined: Thu Jan 05, 2017 5:07 pm

Re: Help modifying code for my project.

by adafruit_support_bill on Sat Mar 14, 2020 6:35 am

Stepperonline Nema 17 Stepper Motor

NEMA 17 is a frame-size designation. It only tells you where to drill the mounting holes for the motor. You need to know the electrical specs to select a suitable matching driver.
https://learn.adafruit.com/all-about-st ... he-stepper

I was thinking of trying this SAME project with and without the adafruit shield, and seeing how it will run with the TB6600 driver. However, I have NO idea how to change out the adafruit with the TB6600 in the program I have.

The Adafruit library is very specific to the Adafruit shield. And the TB6612 drivers on the board have a basic 4-pin H-Bridge interface, rather than the 2-pin step-and-direction interface on your other controller.

I would recommend using the AccelStepper library. https://www.airspayce.com/mikem/arduino/AccelStepper/
It can handle 2-pin step-and-direction type controllers. It also implements 'jerk free' acceleration profiles which will give you much smoother operation on your turntable.

adafruit_support_bill
 
Posts: 78972
Joined: Sat Feb 07, 2009 10:11 am

Re: Help modifying code for my project.

by Chemdawgds on Sat Mar 14, 2020 2:24 pm

I apologize for the miss information on my stepper. The one I have is a nema 17 size by steppersonline. It's a 1.8 angle 200 step. Not sure the exact voltage, but 2 amp. I originally used the adafruit shield, but unfortunately dropped 16 volt to the board. Burnt it up. So i bought another board.

The software i posted works well. I just thought I would save damaging my board again by going with the TB6600 driver.

I know the acellstepper library will work. But excuse my lack of understanding. I just dont know how to implement it into my current program.

Chemdawgds
 
Posts: 14
Joined: Thu Jan 05, 2017 5:07 pm

Re: Help modifying code for my project.

by adafruit_support_bill on Sat Mar 14, 2020 2:48 pm

The one I have is a nema 17 size by steppersonline. It's a 1.8 angle 200 step. Not sure the exact voltage, but 2 amp

Steppersonline sells many different 1.8 degree motors in the NEMA 17 size. That is not enough information to determine if the stepper is compatible. 2A is the maximum current rating, to determine the actual current drawn by the motor at a given voltage, you need to know the phase resistance.

To use the AccelStepper library with a step-and-direction driver like the one you have, you need to first decide which 2 pins you will use to control the driver. Then call the constructor with a first parameter of "AccelStepper::DRIVER " and the numbers of your step and direction pins as the second and third parameters.

See the AccelStepper documentation for details and examples:
https://www.airspayce.com/mikem/arduino ... ca81ac39ab

adafruit_support_bill
 
Posts: 78972
Joined: Sat Feb 07, 2009 10:11 am

Re: Help modifying code for my project.

by Chemdawgds on Sat Mar 14, 2020 4:51 pm

I am really beginning to believe the people in the know how really do NOT want to help. No problem. The 2and program I bbn posted above is using my motor and my TB6600 driver. So it is comparable. I feel like I keep providing more and more info, and every response asks for something else. It's all good. A kind individual from the UK has answered my question. This is the third place I have asked questions and have gotten the run around in all 3. No worries.

Chemdawgds
 
Posts: 14
Joined: Thu Jan 05, 2017 5:07 pm

Re: Help modifying code for my project.

by adafruit_support_bill on Sun Mar 15, 2020 6:50 am

I am really beginning to believe the people in the know how really do NOT want to help.

To summarize: You blew up your motor shield by overvolting by 30% while trying to power a motor of unknown electrical properties.

So you come to the Adafruit product support forums expecting someone to re-write your code with a non-Adafruit library for a non-Adafruit motor and a non-Adafruit driver - neither of which you have provided meaningful documentation for.

Instead, all we offer is pertinent questions and links to relevant documentation.

This is the third place I have asked questions and have gotten the run around in all 3. No worries.

That is indeed quite the run around. Definitely no worries on this end.

adafruit_support_bill
 
Posts: 78972
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.