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:
- Motors rotates in the beginning in random direction, random distance.
- The loop starts immediately after powering up the system, irrespective of whether the limit switch is closed or open.
- If I remove the pin 2 from Arduino then only it waits for signal and once inserted the continous loop begins.
- We added readouts to check the signal at pin2, the signal comes randomly HIGH or LOW, it is mostly HIGH.
- 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?
- 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:
- SSR in point no 5 above
- Why is there random start?
- Is there any problem with the code?
Thank you in advance.
HT