Running stepper with Arduino Uno and A4988: Less torque, random rotation, switch signal always high

Hello everyone,

This is my first post and my first project with Arduino.

Project objective:
To run a stepper motor (42HB34F08AB) when a switch (mechanical limit switch) is pressed for a certain angle and then bring shaft back to its original position.

Tutorial followed:

First I did everything as per the above tutorial and things worked as explained. I have set ref voltage as given in the tutorial. The problem begins when I added a switch here:->

Modified Circuit Diagram:

The code used (took help of a friend who is in coding):

// defines pins numbers
const int stepPin = 3;
const int dirPin = 4;
const int sensorpin = 2;

bool IS_Input_On = false;
void setup() {
  // set the sensorpin as input
  pinMode(sensorpin, INPUT);
  // Sets the two pins as Outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  Serial.begin(9600);   
}
void loop() {      
  if(digitalRead(sensorpin) == HIGH)
  {
    Serial.println("I Am HIGH");
  }
  else
  {
    Serial.println("I Am LOW");
  }
  if (digitalRead(sensorpin) == HIGH && IS_Input_On == false)
  {
    // read sensor input
    digitalWrite(dirPin, LOW); // Enables the motor to move in a particular direction
    // Makes 200 pulses for making one full cycle rotation
    for (int x = 0; x < 100; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    delay(1000); // One second delay

    digitalWrite(dirPin, HIGH); //Changes the rotations direction
    // Makes 400 pulses for making two full cycle rotation
    for (int x = 0; x < 40; x++) {
      digitalWrite(stepPin, HIGH);
      delayMicroseconds(500);
      digitalWrite(stepPin, LOW);
      delayMicroseconds(500);
    }
    delay(1000);
    IS_Input_On = true;
  }
  else if (digitalRead(sensorpin) == LOW)
  {
    IS_Input_On = false;
  }
}

Output:

  1. Motors rotates in the beginning in random direction, random distance.
  2. The loop starts immediately after powering up the system, irrespective of whether the limit switch is closed or open.
  3. If I remove the pin 2 from Arduino then only it waits for signal and once inserted the continous loop begins.
  4. We added readouts to check the signal at pin2, the signal comes randomly HIGH or LOW, it is mostly HIGH.
  5. I measured potential difference across GND pin and COM part of the switch when limit switch is in the open position, it shows voltage of .05V, I guess that is why Arduino is taking it as a false signal. A friend suggest to put SSR to avoid and noise signal before PIN2. First I added a resistor of 1Kohm but no improvement, I am yet to try with SSR. Do you have any suggestions if this will work?
  6. Torque of the motor is very low, I am using 12V adopter with 2A current output. I read in few posts that I should go with 24V which I will try next. Is it safe to use 24V?

Queries:

  1. SSR in point no 5 above
  2. Why is there random start?
  3. Is there any problem with the code?

Thank you in advance.
HT

    // Makes 400 pulses for making two full cycle rotation
    for (int x = 0; x < 40; x++) {

40? 400?

Connect the switch between GND and the digital pin reading the switch. Declare that pin as INPUT_PULLUP. Reading a LOW at the sensing pin tells the switch is activate.

1 Like

I was trying different numbers, it was intentional.

Ok thank you, I will try that.

Do that! I promise, no circuit like in Your drawing will work reliably.
The reason is that an open circuit, no switch being closed, Will float, pick up any electrical noise present and flip flop between LOW and HIGH.
A LOW must be a connection to GND, can be via a pull down resistor, or via a resistor , to Vcc, reading a HIGH when not switched. There we have the built in resistor activated by the declaration INPUT_PULLUP.

2 Likes

I Tried INPUT_PULLUP and it worked like a charm. Thank you so much, made my day.
Now next issue is with lower torque and hoping to solve that with 24V system.

Thanks again,
HT

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