TB6600 code with Arduino

Hi everyone,

I have a device (vorrichtung) that should slide a (Lamp) back and forth. I already have an Arduino with a 12v stepper motor and a TB6600. I need code that I can slide the device every 1 minute 2cm front and after 30cm come back to the first position, at the same time, if I want, with the joystick can also slide back and forth,

@ahmad007, your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project :wink: See About the Installation & Troubleshooting category.

What is vorrichtung?

Usually your write code, show it to us and tell us that it does not work (how does the behaviour differ from what you expect).

Start small; my steps would be

  1. Try to turn the stepper forwards and backwards (not connected to the device).
  2. Next you need to determine how you know that your device (please provide a link) has moved a certain distance and implement code for that.
  3. Next you can figure out how to work with a joystick and write associated code
  4. Lastly you combine the result of (2) with the result of (3).

You can try my MobaTools library to do this. It contains classes to drive a stepper, and classes for timing purposes. There are also many examples for steppers and timers.

Vorrichtung means contraption
i have a Lamp and its fixed on a contraption and i want to move it every 30sec in a distance of 2cm and at the end back again

How many motor steps is 2cm?

it dosent matter how many motor steps. i just want to move it 2cm and if its 22mm or 18mm its okay.

Of course it matters, because you drive your motor with steps and not with distance.
Here is a small example based on MobaTools. You could use it as a base. It lacks moving to a startposition after reset ( homing ).

#include <MobaTools.h>

const int stepsPer2cm = 1000; // adjust to your needs
const int stepsPerRev = 200;  // steps per revolution ( depends on microstepping )
const byte stepPin  = 6;
const byte dirPin = 5;

MoToStepper stepper1 ( stepsPerRev, STEPDIR );  // create a stepper instance
MoToTimer myTimer;

const byte maxIntervall = 15; // 15 times 2cm
byte intervallCount;
unsigned long intervallTime = 60000; // 1 minute

void setup() {
  // setup stepper
  stepper1.attach( stepPin, dirPin );
  stepper1.setSpeed( 3000 );       // 300 rev/min (if stepsPerRev is set correctly)
  stepper1.setRampLen( stepsPerRev / 2); // Ramp length is 1/2 revolution
  intervallCount = 1;
  myTimer.setTime( 10) ;          // start steppermove when timer expired )

void loop() {
  // move stepper when intervalltime expires
  if ( myTimer.expired() ) {
    // move stepper to next position
    stepper1.writeSteps( intervallCount * stepsPer2cm );
    // if max>Intervall is reached, stepper will move back to startposition0
    if ( intervallCount++ > maxIntervall ) intervallCount = 0;
    myTimer.setTime( intervallTime );


Edit: Error corrected

thank you so much for code. its really nice of you, :slightly_smiling_face:

I hope it works for you. It's also just a base for you to build on. As you can see, it's pretty simple.
But the homing is missing, and so is the joystick control. I don't know how you're going to integrate the joystick control either. You'll need something to switch between joystick and automatic.

Can you provide links to your components and the power source? A 12V stepper is not really the best choice for a TB6600. Did you already get it running?

i will run the code when i reach home, tonight,
actually i need to have the joystick or bottun with because if anytime i want to push the target back to the first position i need joystick or bottun

HI, i checked, it works, the only thing i need is joystick or bottum

Yes, it was a base but can of course be extended.
First of all, you need to specify exactly how you want this to work with the joystick or the button. After that, you can start programming.

the problem is. if the arduino because of any reason stop and we have to reset it, it will not go back to the first position and satrt the steps from there where it is, i need the joystick as to bring it back to the first position if i need.

Well, that's a problem of homing. You need a limit switch, which defines the 'first position' . When the arduino starts, it will move the slider to this home position. And you can add a button to start this homing at any time. I don't see that you need a joystick for this. A joystick may be useful if you want to position the slider to an arbitrary position manually.

its a great idea,. therefor i need the code,

Usually you don't get 'ready to go' code in this forum. You will get hints and code snippets to help you. But you also have to code yourself. You can post your efford here, and will get tips and assistance how to get it running if it doesn't work as expected.

But it's not a programming service for free.

The MobaTools lib contains examples for homing/reference.

thank you so much, :slightly_smiling_face: