Help Needed

I am a mechanical design engineer and I design custom machines. I have been doing that for over 30 years.
I have been designing machines for other people to program, but I would like to start doing some of that on my own. The problem is that I know nothing about electronics or controllers programing. I have a project that I am working on, and I am in desperate need of some help, as I am not able to figure out what to do to make it work. So, please if you can and are willing to help me, it will be very much appreciated.
I purchase a closed loop stepper motor (Stepperonline 23E1K-12, Bipolar, 1.8 deg step angle), a driver for it, a power supply, an Arduino Uno and the other accessories that I think were necessary. The information about this parts is on the attached schematic. Below, is a description of what I need the motor to do:

  • toggle switch in "on(1)" position (this is an on(1)-off-on(2) toggle switch)
  • motor starts running CW
  • motor runs 1200 steps in 750 ms
  • motor stops for 50 ms
    -motor starts runing CCW
  • motor runs 1200 steps in 500 ms
  • motor stops for 50 ms
  • loop starts again and keeps runing until: (1) the toggle switch is moved to "off" position or (2) the toggle switch is moved to the "on(2)" position
  • moving the toggle switch to the "on(2)" position will reverse the motor operation. Everything still the same, except the CW becomes CCW and the CCW becomes CW.
    --The project is to make a product feeder that operates on a ball screw slide. It needs to move forward at a certain speed for a distance of about 60 mm, but needs to move back at a greater speed in order for the product to move forward to the direction determined b y the toggle switch position.
    This particular machine can move the product in both directions as needed. thus the need for the toggle switch.
    I attached a schematic of what I have done so far, but I am getting discouraged, as I have been looking for information for days, and unfortunately I was not able to find something that look like what I need to do.
    Again, if somebody can help in getting the sketch done or provide any other help, it will be greatly appreciated.
    Thank you

Do you have you sketch so that it can read the toggle switch and report the status on the serial monitor. Examples are part of the IDE program samples.

You can probably find some code that reads buttons and drives steppers but you're unlikely to find something that does precisely what you need. It looks fairly simple though so you should be able to build up to it.

Have you managed to get the Arduino to run the stepper?

The wiring of the switch is not right. The way that it is, when one side is closed the other is open and that pin is floating (of indeterminate state). Put pull down resistors from the input pins to ground or, better yet, wire common to ground and set the pinModes of the input pins to INPUT_PULLUP to enable the internal pullup resistors. You will have to adjust the logic in the program to reflect that an open switch reads HIGH and a closed switch reads LOW.

thank you for your reply. Yes I made the stepper run, but I am having trouble in programing everything the way I wanted to work.
Thanks for the help

Actually the toggle switch is on off on (2 “no” contacts and “off” on the center)

I stand by what I said.

In light of what you said, though, I have to correct myself. If the switch is in the off position both of the input pins connected to the switch are floating. Floating means that the state of those pins is not to be determined. They could be low or high, at random.

I appreciate your help. Can you offer a sugestion on how I should go about it? Should I use a momentary push button switchs instead. For the application I will need the machine to be able to reverse.
Thank you again

You can use your toggle. Connect it to ground instead of 5V and use input_pullup as the pinmode on the two Arduino pins.

How will you assure, that your stepper is in the starting position if you switch your equipment on? I think you will need a reference switch to move the stepper to the right positon at the very beginning.

I told you how to fix the switch in reply #4. The answer is the same even though the switch is on-off-on. If you wire the switch as I suggested both the switch inputs will read HIGH when the switch is off and if the switch is in one of the on positions that input will read LOW.


I appreciate the help.

Thank you



You have a system that is going to involve the unholy intersection of C++, electronics, electrical and mechanicals. It will be important to iterate towards a solution. Try and do the smallest thing possible and then add to it.

You said that you can move the stepper. Now move it the other way. Read the toggle. Make one side of the switch drive CW, the other CCW. Then more small things.

I and others here could likely write code to do what you need in one sitting, but we wouldn’t. We would use the same incremental approach I’m suggesting, because that code wouldn’t work first time and debugging it would be harder than a slow and steady approach.

This may be a base sketch for starting. It at least lacks the ‘homing’ of the stepper.
It uses a simple finite-state machine to control the operations.

#include <MobaTools.h>    // can be installed by means of the library manager

// Stepper connections - Please adapt them to your own needs.
const byte stepPin = 10;
const byte dirPin = 11;
const byte cwPin  = 9;    // Button pin clockwise
const byte ccPin  = 8;    // Button pin counterclockwise

// Stepper speed
const int pushSpeed  = 16000;  // Steps per 10 sec when running to targetpos
const int pullSpeed  = 20000;  // Steps per 10 sec when running back to startpos
const int rampLen    = 20;     // Length of acceleration/deceleration ramp ( in steps )
const long targetPos = 1200;   // Steppositon to go to

const int stepsPerRev = 200;    // Steps per revolution - may need to be adjusted
                                // This is only important if speed is given in rpm

const long delayTime  = 50;     // 50ms delaytime 

MoToStepper myStepper( stepsPerRev, STEPDIR );

MoToTimer waitTimer;  // Timer for waiting time at start and endpositon

//Possible states of finite state machine
byte fsmState = IDLE;

void setup() {
  // put your setup code here, to run once:
  pinMode( cwPin, INPUT_PULLUP );   // switches must be connected between pin and Gnd
  pinMode( ccPin, INPUT_PULLUP );
  myStepper.attach( stepPin, dirPin );
  myStepper.setRampLen( rampLen );

  // !!!!! HOMING should be inserted here !!!!!


void loop() {
  // put your main code here, to run repeatedly:
  // finite state machine
  switch ( fsmState ) {
    case IDLE: // ------------- waiting far a switch ( cc or CW ) ----------------------
      // debouncing aof switches is not necessary here
      if ( digitalRead( cwPin) == LOW ) {
        // run clockwise 
        myStepper.setSpeedSteps( pushSpeed );
        myStepper.writeSteps( targetPos );
        fsmState = PUSHING;
      if ( digitalRead( ccPin) == LOW ) {
        // run counter clockwise 
        myStepper.setSpeedSteps( pushSpeed );
        myStepper.writeSteps( -targetPos );
        fsmState = PUSHING;
    case PUSHING: // --------------Stepper is running to target position -----------------
      if ( not myStepper.moving() ) {
        // stepper has reached target position, wait bevor running backwards
        waitTimer.setTime( delayTime );  // start Timer
        fsmState = WAITEND;
    case WAITEND: // -----------------------wait till timer expired  ----------------------
      if ( waitTimer.expired() ) {
        // go back to starting point
        myStepper.setSpeedSteps( pullSpeed );
        myStepper.writeSteps( 0 );
        fsmState = PULLING;
    case PULLING: // --------------Stepper is running back to start position -----------------
      if ( not myStepper.moving() ) {
        // stepper has reached start position, wait bevor checking switches again
        waitTimer.setTime( delayTime );  // start Timer
        fsmState = WAITSTART;
    case WAITSTART: // -----------------------wait till timer expired  ----------------------
      if ( waitTimer.expired() ) {
        // check for switches again
        fsmState = IDLE;
      // =============end of fsm==========================


This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.